codeforces-#471B. MUH and Important Things(Sorting+模拟构造)
来源:互联网 发布:淘宝联盟怎么注册条件 编辑:程序博客网 时间:2024/05/03 07:47
题目大意:给定一列下标值的权重,要求输出三种不同的排序方式,从小到大的排列下标顺序。
解题思路:还是发一下这几天的题解吧,这几天一直在忙一些事情,都没有时间敲code。有些事情太繁琐,每次都弄得那么麻烦,不过还好的是今年终于没有被坑了,而是狠狠地给了某个人一些打击,本来只想安安心心准备比赛的,非要弄得大家都不好相处,平时没有交集,可老是跟我有仇一样。不就是跟他没有关系吗,不就是不想给我吗,已经坑了我一年了,今年我是寸土不让的。不过结果最终还是我的了,算是比较欣慰吧!言归正传,本题要求输出下标,则使用结构体记录值以及下标索引,要求有序直接sort排序输出。但主要是输出三种不同序列,因此先记录值判断一下存不存在可能,如果存在这里就进行分别构造。构造方法为:对于只出现过一次则直接输出下标,然后再优先输出三种相同及以上的数的下标。如果不存在再输出两种相同的下标。构造方法以及输出在code中都给出了详细注释,详见code。
题目来源:http://codeforces.com/problemset/problem/471/B
code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 2000+100;int n,cnt,flag1,flag2,flag3,flag4,tmp,d,k;int h[MAXN];struct node{ int in,data;}p[MAXN];int cmp(node a,node b){ return a.data<b.data;}int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){ cnt=0;tmp=0; //初始化 memset(h,0,sizeof(h)); memset(p,0,sizeof(p)); for(int i=1;i<=n;++i){ scanf("%d",&d); p[i].data=d; //记录值 p[i].in=i; //记录下标 h[d]++; //hash值 if(h[d]>=3) tmp=1; //有三个以上相同的标记 if(h[d]==2) ++cnt; //存在两个的计数 } if(!tmp && cnt<2) {printf("NO\n");continue;} //如果没有三个以上相同或者两个相等的数量小于2,则不满足条件 printf("YES\n"); sort(p+1,p+n+1,cmp); //结构体排序 k=3; //初始化输出次数 while(k--){ flag1=flag4=0;flag2=flag3=1; //各种标记变量 for(int i=1;i<=n;){ if(flag1) printf(" "); //控制空格输出 flag1=1; if(h[p[i].data]==1){printf("%d",p[i].in);++i;} //如果只有一个,则输出 else if(tmp){ //如果存在三个及以上则优先输出 if(flag2 && h[p[i].data]>=3){ //flag2控制只改变其中的一组 flag2=0; if(k==2){ printf("%d %d %d",p[i].in,p[i+1].in,p[i+2].in);i+=3; continue; } else if(k==1){ printf("%d %d %d",p[i].in,p[i+2].in,p[i+1].in);i+=3; continue; } else if(k==0){ printf("%d %d %d",p[i+1].in,p[i].in,p[i+2].in);i+=3; continue; } } else{ //其余的正常输出 printf("%d",p[i].in);++i; continue; } } else if(!tmp){ //如果没有三个以上,只有多个两个的 if(flag3){ //控制第一组两个的输出 flag3=0; if(k==2){ printf("%d %d",p[i].in,p[i+1].in);i+=2; continue; } else if(k==1){ printf("%d %d",p[i+1].in,p[i].in);i+=2; continue; } else if(k==0){ printf("%d %d",p[i].in,p[i+1].in);i+=2; flag4=1; //用于第二组数据输出标记 continue; } } else if(flag4){ //如果标记,此时第二组数据交换一次 flag4=0; printf("%d %d",p[i+1].in,p[i].in);i+=2; continue; } else{ //其余的正常输出 printf("%d",p[i].in);++i; continue; } } } printf("\n"); } } return 0;}
0 0
- codeforces-#471B. MUH and Important Things(Sorting+模拟构造)
- CodeForces 471B MUH and Important Things 模拟 水
- CodeForces 471B MUH and Important Things
- Codeforces 471B MUH and Important Things
- CodeForces 471B MUH and Important Things
- CodeForces 471B MUH and Important Things 要求输出
- B. MUH and Important Things
- B. MUH and Important Things(Codeforces Round #269)
- Codeforces Round #269 (Div. 2) B. MUH and Important Things
- Codeforces #269 (Div. 2) B. MUH and Important Things(水题:麻烦)
- cf471B MUH and Important Things
- codeforces-#471A. MUH and Sticks(模拟)
- CodeForces 471C - MUH and House of Cards(推导)
- CodeForces 471D MUH and Cube Walls(KMP匹配)
- CodeForces 471D MUH and Cube Walls(KMP)
- CodeForces 471D MUH and Cube Walls
- codeforces 471A MUH and Sticks
- 【Codeforces Round 269 (Div 2)B】【简单构造】MUH and Sticks 数值升序输出三种可能编号序
- 软件工程之初体验
- 阿里云Ubuntu部署Java Web(2) - 配置Tomcat
- zookeeper 管理
- HDU-#5054 Alice and Bob
- HDU-#5055 Bob and math problem(模拟)
- codeforces-#471B. MUH and Important Things(Sorting+模拟构造)
- wine下使用mybase
- Linux下开发JNI程序
- [leetcode]Populating Next Right Pointers in Each Node
- 阿里云Ubuntu部署Java Web(3) - 配置Mysql
- android viewPager详解
- 网络安全------网络攻击分类
- 操作系统内存,磁盘调度,进程调度算法
- 红米真机不能调试解决方法