HDU 1029Ignatius and the Princess IV
Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 6518 Accepted Submission(s): 2325
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
/*
多元素即在数列中出现次数多于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=;
for(i=;i<n;i++)
{
scanf("%d",&t); if(cnt==)
{
cnt=;
result=t;
}
else
{
if(t==result)cnt++;
else cnt--;cout<<result<<cnt<<endl;
} }
printf("%d\n",result);
}
return ;
}
参考博客:https://www.cnblogs.com/kuangbin/archive/2011/07/30/2122217.html
HDU 1029Ignatius and the Princess IV的更多相关文章
- HDU B-Ignatius and the Princess IV
http://acm.hdu.edu.cn/showproblem.php?pid=1029 Problem Description "OK, you are not too bad, em ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Ja ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- (Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029
Ignatius and the Princess IV 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 借鉴链接:https://blog.csd ...
随机推荐
- pyspider启动错误解决(Python 3.7)
问题一 安装好pyspider之后,在启动的时候,报出上图错误. 原因 async和await从 python3.7 开始已经加入保留关键字中. 参考: What’s New In Python 3. ...
- JavaScript中的柯里化
转载自:https://www.cnblogs.com/zztt/p/4142891.html 何为Curry化/柯里化? curry化来源与数学家 Haskell Curry的名字 (编程语言 Ha ...
- JS-01 书写规范
此部分内容整理自私教指导和自我体会:(持续更新...) 1.运算符左右两边留空格 (webstorm快捷键ctrl+alt+l): 2.判断值是否相等尽量用“===” 严格等于 : 3.编程中,可有可 ...
- Java疯狂讲义笔记——枚举类
枚举类 ——Java5[基础知识]1,定义枚举类——关键字 enum (地位与class.interface相同).2,枚举类是一个特殊的类,可以有成员变量.方法,实现一个或多个接口,定义自己的构造器 ...
- React 和 Vue 到底谁更牛?听听尤雨溪怎么说
React 和 Vue 到底谁更牛?听听尤雨溪怎么说 知乎上近日有人发起了一个 “react 是不是比 vue 牛皮,为什么?” 的问题,再度引发一场关于前端框架谁更牛的口水战,评论里可以说是撕得不可 ...
- HttpClient测试框架
HttpClient是模拟Http协议客户端请求的一种技术,可以发送Get/Post等请求. 所以在学习HttpClient测试框架之前,先来看一下Http协议请求,主要看请求头信息. 如何查看HTT ...
- python 中的eval()函数,称为评估函数
目的:使用BDD的时候,feture中传过来的预期结果是列表字符串:assert_list = "[1,2,3]",我想要的是[1,2,3] 处理方法:使用eval()评估函数 ...
- python 利用subprocess调用cmd命令程序,并正确输出控制台的输出中文
平台Python3.7 1.利用控制台运行程序后在控制台会输出中文提示,但是用python调用subprocess.run函数后返回的输出是乱码,于是,解决方法是用subprocess.check_o ...
- "||" 在sql中有什么用
双竖线表示字符串拼接 比如: 'abc' || 'cba' 结果: 'abccba'
- 【leetcode】1023. Binary String With Substrings Representing 1 To N
题目如下: Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, r ...