CF500B New Year Permutation (贪心+并查集)
来源:互联网 发布:金山软件安全卫士 编辑:程序博客网 时间:2024/05/28 16:08
把能互相交换位置的都合并成一个集合,用并查集维护。每一个集合中的位置都是可以两两互换的,所以贪心地,我们对于每一个集合的位置从小到大排列,这些位置上的数也从小到大排列,一一对应,保证小数尽量在前面,即为最优。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define N 305inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,a[N],fa[N],ans[N];char s[N];vector<int>pos[N];inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}inline void merge(int x,int y){ int xx=find(x),yy=find(y); if(xx!=yy) fa[yy]=xx;}void solve(int x){ vector<int>val; for(int i=0;i<pos[x].size();++i) val.push_back(a[pos[x][i]]); sort(pos[x].begin(),pos[x].end()); sort(val.begin(),val.end()); for(int i=0;i<pos[x].size();++i) ans[pos[x][i]]=val[i];}int main(){// freopen("a.in","r",stdin); n=read();for(int i=1;i<=n;++i) a[i]=read(),fa[i]=i; for(int i=1;i<=n;++i){ scanf("%s",s+1); for(int j=i+1;j<=n;++j) if(s[j]=='1') merge(i,j); } for(int i=1;i<=n;++i) pos[find(i)].push_back(i); for(int i=1;i<=n;++i) if(pos[i].size()!=0) solve(i); for(int i=1;i<=n;++i) printf("%d ",ans[i]); return 0; }
阅读全文
0 0
- CF500B New Year Permutation (贪心+并查集)
- New Year Permutation(Floyd+并查集)
- New Year Permutation(并查集+动态容器)
- Good Bye 2014 B. New Year Permutation(并查集 ||Floyd )
- Good Bye 2014 B. New Year Permutation 并查集 最短路 floyed算法
- Codeforces 500B:New Year Permutation(并查集||Floyd+思维)
- cf 500B New Year Permutation 【并查集】or【floyd】
- Codeforces 500B New Year Permutation [贪心]
- 【CodeForces 500B】【贪心】New Year Permutation
- 【CodeForces500B】【贪心】New Year Permutation 题解
- CodeForces 500 B. New Year Permutation(贪心+Floyd)
- Codeforces 500B New Year Permutation Floyd算法+贪心
- 【Good Bye 2014B】【Floyd or 并查集】New Year Permutation 全排列有位置交换序列 使得字典序尽可能小
- B - New Year Permutation
- B. New Year Permutation
- CF_500B New Year Permutation
- New Year Permutation
- New Year Permutation
- Linux学习(五):shell命令用户系统相关命令
- sublime3添加verilog自动补全代码段
- 单例模式——静态内部类式
- Docker安装步骤
- python -- 文件和IO操作
- CF500B New Year Permutation (贪心+并查集)
- Android 以apk包方式共享资源(动态换肤)的实现方式
- python -- 网络和数据库
- 多因子模型之因子(信号)测试平台----alphalens(四)
- python -- 面向对象编程
- CSS3按钮动画
- 《Angular2入门系列基础》——ActivatedRoute路由
- leetcode 25. Reverse Nodes in k-Group
- 编程练习