牛客网国庆集训派对Day6 题目 2018年
链接:https://www.nowcoder.com/acm/contest/206/A
来源:牛客网
Birthday
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld
题目描述
正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为m个区域。因为某种原因,他必须把第i根蜡烛插在第ai个区域或第bi个区域。区域之间是不相交的。宇扬在一个区域内同时摆放x支蜡烛就要花费x2的时间。宇扬布置蛋糕所用的总时间是他在每个区域花的时间的和。
宇扬想快些见到恬恬,你能告诉他布置蛋糕最少需要多少时间吗?
输入描述:
- 第一行包含两个整数n,m(1 ≤ n ≤ 50, 2≤ m≤ 50)。
接下来n行,每行两个整数a
i
- ,b
i
- (1 ≤ a
i
- , b
i
- ≤ m)。
输出描述:
- 一个整数表示答案。
输出
- 3
思路:最小费用最大流。
代码:
- #include <cstdio>
- #include <cstring>
- #include <vector>
- #include <queue>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int MAXN = ;
- struct Edge{
- int value,flow,to,rev;
- Edge(){}
- Edge(int a,int b,int c,int d):to(a),value(b),flow(c),rev(d){}
- };
- vector<Edge> E[MAXN];
- inline void Add(int from,int to,int flow,int value){
- E[from].push_back(Edge(to,value,flow,E[to].size()));
- E[to].push_back(Edge(from,-value,,E[from].size()-));
- }
- bool book[MAXN];//用于SPFA中标记是否在queue中
- int cost[MAXN];//存费用的最短路径
- int pre[MAXN];//存前节点
- int pree[MAXN];//存在前节点的vector中的下标
- bool Spfa(int from,int to){
- memset(book,false,sizeof book);
- memset(cost,INF,sizeof cost);
- book[from] = true;
- cost[from] = ;
- queue<int> Q;
- Q.push(from);
- while(!Q.empty()){
- int t = Q.front();
- book[t] = false;
- Q.pop();
- for(int i= ; i<E[t].size() ; ++i){
- Edge& e = E[t][i];
- if(e.flow > && cost[e.to] > cost[t] + e.value){
- cost[e.to] = cost[t] + e.value;
- pre[e.to] = t;
- pree[e.to] = i;
- if(book[e.to] == false){
- Q.push(e.to);
- book[e.to] = true;
- }
- }
- }
- }
- return cost[to] != INF;
- }
- int Work(int from,int to){
- int sum = ;
- while(Spfa(from,to)){
- int mflow = INF;//SPFA找到的最短路径的最小容量
- int flag = to;
- while(flag != from){
- mflow = min(mflow,E[pre[flag]][pree[flag]].flow);
- flag = pre[flag];
- }
- flag = to;
- while(flag != from){
- sum += E[pre[flag]][pree[flag]].value * mflow;
- E[pre[flag]][pree[flag]].flow -= mflow;
- E[flag][E[pre[flag]][pree[flag]].rev].flow += mflow;
- flag = pre[flag];
- }
- }
- return sum;
- }
- int main(){
- int N,M;
- while(scanf("%d %d",&N,&M) == ){
- int a,b;
- for(int i= ; i<=N ; ++i){
- scanf("%d %d",&a,&b);
- Add(i,a+N,,);
- Add(i,b+N,,);
- Add(,i,,);
- }
- for(int i= ; i<=M ; ++i){
- for(int j= ; j<= ; j+=){//99 = 2*50-1;
- Add(i+N,M+N+,,j);
- }
- }
- printf("%d\n",Work(,M+N+));
- for(int i= ; i<=M+N+ ; ++i)E[i].clear();
- }
- return ;
- }
链接:https://www.nowcoder.com/acm/contest/206/B
来源:牛客网
Board
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld
题目描述
开始时,数组中每一个元素都是0。
恬恬会做某些操作。在一次操作中,她可以将某一行的所有元素同时加上一个值,也可以将某一列的所有元素同时加上一个值。
在几次操作后,一个元素被隐藏了。你能帮助她回忆隐藏的数是几吗?
输入描述:
- 第一行一个整数n(1≤ n≤ 1000)。
接下来n行每行n个整数表示数组a。
第(i+1)行的第j个元素表示a
ij
- (a
ij
- =-1或0≤ a
ij
- ≤ 10000)。-1表示隐藏的元素。
输出
- 仅一个整数表示答案。
输出
- 1
- 思路:先把每一行先减去每一行的最小值,然后找到“-1”所在的行和列(行和列的各自的最小值的和即为所求)
代码:
- #include<bits/stdc++.h>
- using namespace std;
- const int inf=1e9+;
- int a[][];
- int main()
- {
- int n;
- int x,y;
- scanf("%d",&n);
- for(int i=; i<=n; i++)
- {
- for(int j=; j<=n; j++)
- {
- scanf("%d",&a[i][j]);
- if(a[i][j]==-)
- {
- x=i,y=j;
- }
- }
- }
- int sum;
- for(int i=; i<=n; i++)
- {
- sum==inf;
- if(i==x)
- continue;
- for(int j=;j<=n;j++)
- {
- sum=min(sum,a[i][j]);
- }
- for(int j=;j<=n;j++)
- {
- a[i][j]-=sum;
- }
- }
- for(int i=; i<=n; i++)
- {
- sum==inf;
- if(i==y)
- continue;
- for(int j=;j<=n;j++)
- {
- sum=min(sum,a[j][i]);
- }
- for(int j=;j<=n;j++)
- {
- a[j][i]-=sum;
- }
- }
- int tmp1=inf,tmp2=inf;
- for(int i=;i<=n;i++)
- {
- if(i==x)
- {
- continue;
- }
- tmp1=min(tmp1,a[i][y]);
- }
- for(int i=;i<=n;i++)
- {
- if(i==y)
- {
- continue;
- }
- tmp2=min(tmp2,a[x][i]);
- }
- printf("%d\n",tmp1+tmp2);
- return ;
- }
链接:https://www.nowcoder.com/acm/contest/206/C
来源:牛客网
Circle
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld
题目描述
思路:输入n,输出n,此时的排序方式为1,n,n-1,n-2,n-3,,,,,5,4,3,2
- 代码:
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n;
- scanf("%d",&n);
- printf("%d\n",n);
- return ;
- }
Mountain
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld
题目描述
除了简单的爬上爬下,还有一种特殊操作。
如果弱弱目前在第i座山右面的海拔x的位置,且第j ( i < j )座山的海拔大于等于x,且第
弱弱想找到一种方式,使得他在行程中海拔变化的幅度最小。请输出最小幅度。
输入描述:
- 第一行一个整数n(1≤ n≤ 1000)。
接下来一行n个整数a
i
- (1≤ a
i
- ≤ 1000)表示每座山的高度。
输出描述:
- 一行一个整数表示答案。
- 思路:最高点的两倍。
代码:
- #include<bits/stdc++.h>
- using namespace std;
- int a[];
- int main()
- {
- int n;
- scanf("%d",&n);
- for(int i=;i<n;i++)
- {
- scanf("%d",&a[i]);
- }
- sort(a,a+n);
- printf("%d\n",*a[n-]);
- return ;
- }
Growth
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld
题目描述
为了激励弱弱努力学习,我们共有n种奖励,第i种奖励有xi,yi,zi三种属性,若a≥ xi且b≥ yi,则弱弱在接下来的每一天都可以得到zi的分数。
问m天以后弱弱最多能得到多少分数。
输入描述:
- 第一行一个两个整数n和m(1≤ n≤ 1000,1≤ m≤ 2000000000)。
接下来n行,每行三个整数x
i
- ,y
i
- ,z
i
- (1≤ x
i
- ,y
i
- ≤ 1000000000,1≤ z
i
- ≤ 1000000)。
输出描述:
- 一行一个整数表示答案。
备注:
- 在样例中,弱弱可以这样规划:第一天a涨1,第二天b涨1,第三天b涨1,第四天a涨1。
共获得0+0+20+30=50分。- 思路:离散+DP
代码:
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e3 + ;
- map<pair<int, int>, int> p;
- set<int> sx, sy;
- int x[maxn], y[maxn];
- ll dp[maxn][maxn], v[maxn][maxn];
- int main()
- {
- int n, m;
- int cnt1 = , cnt2 = ;
- scanf("%d %d", &n, &m);
- for(int i = ; i < n; i++) {
- int tx, ty, tz;
- scanf("%d %d %d", &tx, &ty, &tz);
- p[pair<int, int>(tx, ty)] += tz;
- if(!sx.count(tx)) sx.insert(tx), x[cnt1++] = tx;
- if(!sy.count(ty)) sy.insert(ty), y[cnt2++] = ty;
- }
- sort(x + , x + cnt1);
- sort(y + , y + cnt2);
- for(int i = ; i < cnt1; i++) {
- for(int j = ; j < cnt2; j++) {
- if(!p[pair<int, int>(x[i], y[j])]) v[i][j] = v[i - ][j] + v[i][j - ] - v[i - ][j - ];
- else v[i][j] = v[i - ][j] + v[i][j - ] - v[i - ][j - ] + p[pair<int, int>(x[i], y[j])];
- }
- }
- for(int i = ; i < cnt1; i++) {
- for(int j = ; j < cnt2; j++) {
- dp[i + ][j] = max(dp[i + ][j], dp[i][j] + (x[i + ] - x[i] - ) * v[i][j] + v[i + ][j]);
- dp[i][j + ] = max(dp[i][j + ], dp[i][j] + (y[j + ] - y[j] - ) * v[i][j] + v[i][j + ]);
- }
- }
- ll ans = ;
- for(int i = ; i < cnt1; i++) {
- for(int j = ; j < cnt2; j++) {
- ll t = dp[i][j] + (m - x[i] - y[j]) * v[i][j];
- ans = max(ans, t);
- }
- }
- printf("%lld\n", ans);
- }
牛客网国庆集训派对Day6 题目 2018年的更多相关文章
- 牛客网国庆集训派对Day5 题目 2018年
链接:https://www.nowcoder.com/acm/contest/205/L来源:牛客网参考博客:https://blog.csdn.net/HTallperson/article/de ...
- 牛客网国庆集训派对Day4题目 2018年
链接:https://www.nowcoder.com/acm/contest/204/A来源:牛客网 深度学习 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他 ...
- 牛客网国庆集训派对Day3题目 2018年
链接:https://www.nowcoder.com/acm/contest/203/D来源:牛客网 Shopping 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...
- 牛客国庆集训派对Day6 A Birthday 费用流
牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...
- 牛客国庆集训派对Day6 && CCPC-WannaFly-Camp #1 F. kingdom(DP)
题目链接:https://www.nowcoder.com/acm/contest/206/F 题意:一棵 n 个点的树,根为 1,重儿子到父亲的费用为 0,其余为 1,问所有点到 1 的最大总费用是 ...
- 牛客国庆集训派对Day6 B.Board
链接 [https://www.nowcoder.com/acm/contest/206/B] 分析 只要在n*n范围内随便找一个斜对角的一个格子去计算就知道了 具体看代码体会吧 代码 #includ ...
- 牛客国庆集训派对Day6 Solution
A Birthday 思路:设置一个源点,一个汇点,每次对$源点对a_i, b_i , a_i 对 b_i 连一条流为1,费用为0的边$ 每个点都再连一条 1, 3, 5, 7, ....的边到 ...
- 减2或减3(很搞的贪心)2019牛客国庆集训派对day6
题意:https://ac.nowcoder.com/acm/contest/1111/D 问你先减二x次的情况下,最少减几次3. 思路: %3不为0的要先减2,然后%3为0的要先减大的(比如9 3 ...
- MySQL基础练习---牛客网的数据以及典型题目
1 部门表departments 部门no和部门名称 2 部门员工表 dept_emp 每个部门对应的员工信息 3 部门经理表 dept_manager 每个部门的经理信息 4 员工表 employe ...
随机推荐
- JavaWeb基础—dbutils的简单入门
简明入门教程,参考:https://www.cnblogs.com/CQY1183344265/p/5854418.html 进行此章节之前,介绍一个JdbcUtils的再次的简单封装 (例如后面需要 ...
- python列表,元组,字典和字符串之间的相互转换
元组转换成列表 >>> mytuple = (1,2,3) >>> print list(mytuple) [1, 2, 3] 列表转换成元组 >>&g ...
- mfc 类对象的引用
类对象引用 自写复制构造函数 一. 类对象引用 在第4课的时候,我们已经讨论过C++引用特性.类变量的引用呢,实际上也是类似的. Tdate d1; Tdate &d2=d1; 二.自写复制构 ...
- 正则表达式简介及在C++11中的简单使用
正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex.regexp.RE.regexps.regexes.regexen. 正则表达式是一种 ...
- 【HNOI2015】亚瑟王
题面 题解 考虑进行\(dp\). 设\(f[i][j]\)表示前\(i\)张卡中有\(j\)张被触发的概率. 我们可以知道第\(i\)张卡不被触发的概率为\((1 - p_i) ^ {r - j}\ ...
- 4710: [Jsoi2011]分特产
4710: [Jsoi2011]分特产 链接 分析: 容斥原理+隔板法. 代码: #include<cstdio> #include<algorithm> #include&l ...
- 微信小程序:text元素中加入空格
在text标签中加入 decode = "{{true}}" ,然后字啊需要加入空格的地方使用 即可加入一个空格,可以连续用多个例如: <text decode = &q ...
- P2939 [USACO09FEB]改造路Revamping Trails
P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...
- Linux命令应用大词典-第1章 登录、退出、关机和重启
1.1 login:用户登录系统 1.2 logout:退出登录shell 1.3 nologin:限制用户登录 1.4 exit:退出shell 1.5 sulogin:单用户登录(single u ...
- LinuxMint 18.3禁用ipv6
编辑/etc/sysctl.conf文件,添加如下内容 net.ipv6.conf.all.disable_all = 1 保存后执行 sudo sysctl -p 即可生效