Hdu 1029 Ignatius and the Princess IV 动态规划
2016-03-11 21:02
351 查看
题意:奇数n个数,存在一个数出现的次数大于(n+1)/2次,求这个数;
这个数出现的次数比其他数出现的次数加起来还多,那么当这个数出现时+1,其他的数出现时-1,最后得到的数为正数。
假定一个数为特殊数,若当前数与特殊数相同则cnt++,若不相同则cnt--,如果这时cnt<0,用当前数替代特殊的数。不管怎么样,由于特殊数次数大于(n+1)/2次,最终保留的数一定是特殊数。
这个数出现的次数比其他数出现的次数加起来还多,那么当这个数出现时+1,其他的数出现时-1,最后得到的数为正数。
假定一个数为特殊数,若当前数与特殊数相同则cnt++,若不相同则cnt--,如果这时cnt<0,用当前数替代特殊的数。不管怎么样,由于特殊数次数大于(n+1)/2次,最终保留的数一定是特殊数。
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main() { int n; while(~scanf("%d",&n)) { int cnt=0,t,ans; while(n--) { scanf("%d",&t); if(ans==t) cnt++; else if(--cnt<0) cnt=0,ans=t; } cout<<ans<<endl; } }
相关文章推荐
- 第 7 章 浏览器对象
- Linux Is Not Matrix——扩容的时间
- 【数据库】SQL查询强化篇
- 关于Hive的练习
- 页面内元素跟随鼠标移动和右键菜单组件功能
- Codeforces Round #345 (Div. 1)
- 杭电Problem-2717
- DDoS攻击 TCP UDP ICMP
- 素数筛模板
- [RxJS] Logging a Stream with do()
- hive 操作(三)——hive 的数据模型
- Atitit.木马病毒的免杀原理---sikuli 的使用
- ios事件处理-
- maven 运行scalatest以及elastic search 自定义id
- iOS动画
- C++中虚函数工作原理和(虚)继承类的内存占用大小计算
- Atitit.木马病毒的免杀原理---sikuli 的使用
- Atitit.木马病毒的免杀原理---sikuli 的使用
- 并发处理方案 个人总结
- cxImage控件使用