Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
来源:互联网 发布:为什么要探索宇宙 知乎 编辑:程序博客网 时间:2024/05/19 22:48
一道明显的贪心题目,优先处理推迟耗费大的航班,使它尽量耗费较少,这样可使总的耗费较少!但是普通遍历明显会TLE,所以我们可以用并查集跳过已经被占用的时间,记录每一个点的下一个点是谁即可。
具体细节代码里说:
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<map>#include<cmath>using namespace std;#define mem(a,b) memset(a,b,sizeof(a))typedef long long ll;const int maxn = 3e5+5;const int ff = 0x3f3f3f3f;struct node{ll c;int pos;}node[maxn];int n,k; int ans[maxn];int ne[maxn*2];//这个数组要开大bool cmp(struct node a,struct node b){return a.c> b.c;}void init(){for(int i = 0;i<= maxn*2;i++)ne[i] = i;return ;}int find(int x){int r = x;while(x!= ne[x])x = ne[x];while(r!= ne[r])//一定要路径压缩,否则很容易超时{int t = ne[r];ne[r] = x;r = t;}return x;}int main(){cin>>n>>k;init();for(int i = 1;i<= n;i++){scanf("%I64d",&node[i].c);node[i].pos = i;}ll sum = 0;sort(node+1,node+n+1,cmp);for(int i = 1;i<= n;i++){int fa = find(max(node[i].pos,k+1));ans[node[i].pos] = fa;sum+= (fa-node[i].pos)*node[i].c;//运算过程中可能会超int,这里注意ne[fa] = find(fa+1);}cout<<sum<<endl;for(int i = 1;i<= n;i++)if(i == n)printf("%d\n",ans[i]);elseprintf("%d ",ans[i]);return 0;}
阅读全文
1 0
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C. Planning(并查集)
- Codeforces Round #433 (Div. 2) C. Planning(贪心)
- Codeforces Round #433 (Div. 2)-贪心-C. Planning
- Codeforces Round #433 (Div. 2) C. Planning
- Codeforces Round #433 (Div. 2) C. Planning
- Codeforces Round #376 (Div. 2) C. Socks 并查集+贪心、图论
- Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
- Codeforces 854C Planning【贪心+并查集】
- 【贪心+并查集】Codeforces 853A Planning
- Codeforces Round #433 (Div. 2, C. Planning (贪心+简单实现)
- Codeforces Round #433 C. Planning(贪心)
- Codeforces Round #254 (Div. 2)(并查集,线段树)
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- Codeforces Round #346 (Div. 2) F bfs+并查集
- Codeforces Round #376 (Div. 2) C 并查集
- Codeforces Round #376 (Div. 2) C dfs+并查集
- HDU
- Thrift 入门例子
- 清晰理解堆排序
- Spring DI依赖注入,自动装配
- iframe跨域
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- cnpm 安装vue
- jdk安装
- 第二十四节:兼容所有浏览器滚动条滚动的js
- μcos-II任务堆栈、任务控制块和任务代码
- 16. Yii 2.0 http缓存
- JavaScript 面向对象(四)call()和apply()
- 诸神之战 | 赛事升级,4.1亿元奖金纳入浙江赛区奖项权益
- 文章标题