[思维题] Ignatius and the Princess IV HDU
来源:互联网 发布:网络在线咨询 编辑:程序博客网 时间:2024/06/11 14:29
[思维题] Ignatius and the Princess IV HDU - 1029
题目大意:在给定的N个数字中,有个数字出现至少(N + 1)/ 2次,让你找出他。
分析:这道题kuangbin大神把他放到了简单DP里,我没看出怎么DP。。。我是这么想的,既然这个数字出现了(N + 1)/ 2次,那么只有它可以和其他数字一一抵消并且最后还有剩余。所以O(N)模拟下就是答案。
代码:
#include<stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<stdio.h>#include<queue>using namespace std;int main(){ int n; while(~scanf("%d",&n)) { int cnt,temp,k; scanf("%d",&temp); k=temp; cnt=1; for(int i=0;i<n-1;i++) { scanf("%d",&temp); if(temp==k) cnt++; else { cnt--; } if(cnt==-1) { cnt=1; k=temp; } } printf("%d\n",k); } return 0;}
阅读全文
0 0
- [思维题] Ignatius and the Princess IV HDU
- 【HDU】1029 - Ignatius and the Princess IV(STL || 思维)
- HDU--1029 Ignatius and the Princess IV (map&&思维&&memset)
- 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
- Flutter学习之旅(五)----网络请求获取数据、解析数据和显示等待动画
- 编写代码实现,模拟用户登录情景,并且只能登录三次,只允许输入三次密码,如果密码正确则提示登录成功,如果三次均输入错误,则退出程序。
- Java HashMap笔记之一:基本原理
- Linux下去掉^M的方法
- Java /封装/包装/必记的小部分内容
- [思维题] Ignatius and the Princess IV HDU
- 如何在 UWP 使用 wpf 的 Trigger
- Launcher 启动 UE4 引擎出现 An Unreal process has crashed 的问题
- BMP图片的显示,本文章侧重于图片的内部数据结构
- ASP SQL语句
- css三角形的制作
- Idea15 常用设置(一):JDK、SVN
- Fire Game
- 1032. 挖掘机技术哪家强(20)