HDU 1029 Ignatius and the Princess IV
来源:互联网 发布:日语记单词软件 编辑:程序博客网 时间:2024/05/17 23:58
/*多元素即在数列中出现次数多于n/2的元素 ————采用这个方法的前提我们很容易的看出来,在一个序列中如果去掉2个不同的元素,那么原序列中的多元素,在新的序列中还是多元素,因此我们只要按照序列依次扫描,先把t赋值给result,增加个计数器,cnt = 1;然后向右扫描,如果跟result相同,则cnt++,不同,那么cnt --,这个真是我们从上面那个结论里得出的,一旦cnt == 0了,那么必定c不是多元素,这个时候把t赋值为result,cnt = 1;,重复该过程,知道结束,这个时候,result就是多元素,这个的时间复杂度为n,该题本来可以用数组保存每个元素,然后递归上述过程,可是,用数组超内存,因此我们可以直接按照上述过程计算*///用cin,cout会超时 #include<iostream>#include<string.h>using namespace std;int main(){ int n,i; int t; int cnt; int result; while(scanf("%d",&n)!=EOF) { cnt=0; for(i=0;i<n;i++) { scanf("%d",&t); if(cnt==0) { cnt=1; result=t; } else { if(t==result)cnt++; else cnt--; } } printf("%d\n",result); } return 0; }
//以上转载内容来自_kuangbin的博客
0 0
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- Ignatius and the Princess IV hdu 1029
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU-1029-Ignatius and the Princess IV
- HDU--1029--Ignatius and the Princess IV
- hdu - 1029 -Ignatius and the Princess IV
- 异常日志记录 DDLog
- hdu 2141 Can you find it?(暴力+二分)
- 做菜不好吃,你一定是忽略了这20个小技巧!
- 为解决的问题,应该使用哪种方案
- CSS+DIV详解
- HDU 1029 Ignatius and the Princess IV
- 程序员的内功
- 按钮,边框自定义圆角——shape和selector和layer-list的(详细说明)
- 学生成绩的处理
- CodeForces 600A - Extract Numbers (模拟题)
- autoreleasepool
- Nodejs 0.10.x 之Query String模块
- 欢迎使用CSDN-markdown编辑器
- 简单计算器