【贪心+并查集】Codeforces 853A Planning
来源:互联网 发布:数据使用承诺书 编辑:程序博客网 时间:2024/05/29 12:29
题面在这里
这题其实非常水……
只需要按照
这个贪心很显然吧……
示例程序:
#include<cstdio>#include<algorithm>using namespace std;typedef long long LL;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline int red(){ int res=0,f=1;char ch=nc(); while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();} while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc(); return res*f;}const int maxn=600005;int n,k,fa[maxn],t[maxn];LL ans;struct data{ int c,id; data () {} data (int _c,int _id):c(_c),id(_id) {} bool operator<(const data&b)const{ return c>b.c||c==b.c&&id<b.id; }}a[maxn];int getfa(int x){ return fa[x]==x?x:fa[x]=getfa(fa[x]);}int main(){ n=red(),k=red(); for (int i=1;i<=n;i++) a[i]=data(red(),i); sort(a+1,a+1+n); for (int i=1;i<=k;i++) fa[i]=k+1; for (int i=k+1;i<=k+n;i++) fa[i]=i; for (int i=1;i<=n;i++){ int j=a[i].id,fj=getfa(j); fa[fj]=fj+1;ans+=(LL)(fj-j)*a[i].c;t[j]=fj; } printf("%lld\n",ans); for (int i=1;i<=n;i++) printf("%d ",t[i]); return 0;}
阅读全文
1 0
- 【贪心+并查集】Codeforces 853A Planning
- Codeforces 854C Planning【贪心+并查集】
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- Codeforces 853A Planning 【贪心】
- (codeforces 853A)Planning 贪心
- 【并查集+贪心】 CodeForces
- Codeforces 853A Planning
- Codeforces 366D 贪心+并查集
- codeforces 731C 并查集+贪心
- Codeforces 745C 并查集+贪心
- Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~
- 并查集,贪心:Color A Tree
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C. Planning(并查集)
- CodeForces 123A 并查集
- CodeForces - 744A (并查集)
- codeforces 827A 并查集
- CodeForces 731 C.Socks(贪心+并查集)
- Codeforces Round #385 (Div. 2) C. Hongcow Builds A Nation 并查集+贪心+组合学、图论、dfs
- 现代信息化项目管理中常见的失误有哪些
- 软件测试之jenkins+maven+SVN
- Phalcon路由模式快速配置
- 一个简单的Java web服务器实现
- rdd转换为Set或者Map
- 【贪心+并查集】Codeforces 853A Planning
- 51Nod-1264-线段相交【两线段相交的判定】
- oracle中管理表空间和数据文件
- java 序列化和反序列化代码示例
- MFC窗口前端展示
- js之ajax的一点理解
- img.convertTo(img, CV_32FC3)
- JS中的数组及相关操作
- shell conculate