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

Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

"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?

 
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
 
Output
For each test case, you have to output only one line which contains the special number you have found.
 
Sample Input
5
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
 
Sample Output
3 5 1
题意:给你一个数列,找出里面大于n/2的数;
题解:最简单的方法就是用一个标记数组,然后sort一遍去找,或者直接找中间值就行了;复杂度都是O(nlogn);这里说一下
O(n)的做法;因为存在大于数组大小一半的数,所以你可以用一个cnt去计数,用一个result来更新值每次遇到不同的就减一,
相同就加一,比如下面的数列
5
1 1 1 2 2
cnt=1、2、3、2、1 最后result记录的就是1了
 
9
1 3 1 4 1 5 1 6 1
result每次都更新为一
代码:
  1. /*
  2. 多元素即在数列中出现次数多于n/2的元素
  3.  
  4. 我们很容易的看出来,在一个序列中如果去掉2个不同的元素,
  5. 那么原序列中的多元素,在新的序列中还是多元素,
  6. 因此我们只要按照序列依次扫描,先把t赋值给result,
  7. 增加个计数器,cnt = 1;然后向右扫描,
  8. 如果跟result相同,则cnt++,不同,那么cnt --,
  9. 这个真是我们从上面那个结论里得出的,一旦cnt == 0了,
  10. 那么必定c不是多元素,这个时候把t赋值为result,cnt = 1;,
  11. 重复该过程,知道结束,这个时候,result就是多元素,
  12. 这个的时间复杂度为n,该题本来可以用数组保存每个元素,
  13. 然后递归上述过程,可是,用数组超内存,
  14. 因此我们可以直接按照上述过程计算
  15.  
  16. */
  17.  
  18. //用cin,cout会超时
  19. #include<iostream>
  20. #include<string.h>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. int n,i;
  26. int t;
  27. int cnt;
  28. int result;
  29. while(scanf("%d",&n)!=EOF)
  30. {
  31. cnt=;
  32. for(i=;i<n;i++)
  33. {
  34. scanf("%d",&t);
  35.  
  36. if(cnt==)
  37. {
  38. cnt=;
  39. result=t;
  40. }
  41. else
  42. {
  43. if(t==result)cnt++;
  44. else cnt--;cout<<result<<cnt<<endl;
  45. }
  46.  
  47. }
  48. printf("%d\n",result);
  49. }
  50. return ;
  51. }

参考博客:https://www.cnblogs.com/kuangbin/archive/2011/07/30/2122217.html

HDU 1029Ignatius and the Princess IV的更多相关文章

  1. 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 ...

  2. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  3. 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 ...

  4. 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 ...

  5. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  6. [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 ...

  7. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. 【HDU - 1029】Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV  先搬中文 Descriptions:   给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...

  9. (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 ...

随机推荐

  1. vuex的配置使用

    为什么使用vuex? vuex主要是是做数据交互,父子组件传值可以很容易办到,但是兄弟组件间传值(兄弟组件下又有父子组件),或者大型spa单页面框架项目,页面多并且一层嵌套一层的传值,异常麻烦,用vu ...

  2. [AtCoder] NIKKEI Programming Contest 2019 (暂缺F)

    [AtCoder] NIKKEI Programming Contest 2019   本来看见这一场的排名的画风比较正常就来补一下题,但是完全没有发现后两题的AC人数远少于我补的上一份AtCoder ...

  3. github ssh秘钥配置

    1.本地生产ssh密钥对 ssh-keygen -t rsa -C "your_email@example.com" 2. 进入~/.ssh 拷贝公钥进入github里面

  4. centos7安装nginx并配置web前端环境。

    1.安装nginx -sudo yum install nginx 2.启动nginx - systemctl start nginx 3.修改nginx路径配置,/etc/nginx/nginx.c ...

  5. Using If/Truth Statements with pandas

    pandas follows the numpy convention of raising an error when you try to convert something to a bool. ...

  6. 基因id

    每个物种都有一个对应的Taxonomy ID: 9606 :人类 10090 :小鼠

  7. 【Java】定义Logger为什么要用static和final?

    private static final Logger logger= LoggerFactory.getLogger(ShiroConfig.class); (1)出于资源利用的考虑,Logger的 ...

  8. operator函数操作符

    函数操作数() 可以实现将对象当函数使用. class Square{ public: double operator()(double x)const{return x*x;} };

  9. webBrowser强制在本窗口打开,禁止在新窗口打开

    有时需要用WebBrowser加载URL,来实现某些功能.而这时,我们就不希望所打开的页面中的链接,在新窗口中打开,因为这样的话,实际上是用系统默认的浏览器打开了,从而脱离了你的WebBrowser, ...

  10. Sqlachemy的警告SAWarning: The IN-predicate on "sns_object.BIZ_ID" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.

    我在使用db_session.query,查询的时候idlist是个空值时候,执行下面的语句就会出现警告.其中后面delete(synchronize_session=False)是删除前面的一堆查询 ...