方法一:    直接进行排序,输出第(n+1)/2位置上的数即可。 (容易超时,关闭同步后勉强卡过)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int n;
int a[];
int main()
{
ios::sync_with_stdio(false);
while(cin>>n)
{
for(int i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
cout<<a[(n+)/]<<endl;
}
}

方法二:原 https://www.cnblogs.com/kuangbin/archive/2011/07/30/2122217.html

在一个序列中如果去掉2个不同的元素,
那么原序列中的多元素,在新的序列中还是多元素,
因此我们只要按照序列依次扫描,先把t赋值给result,
增加个计数器,cnt = 1;然后向右扫描,
如果跟result相同,则cnt++,不同,那么cnt --,
这个真是我们从上面那个结论里得出的,一旦cnt == 0了,
那么必定c不是多元素,这个时候把t赋值为result,cnt = 1;,
重复该过程,知道结束,这个时候,result就是多元素,
这个的时间复杂度为n。

这个算法的正确性在于因为我们要输出的Ans的次数是大于等于n/2+1的,也就是说它出现的次数是大于其他所有数出现次数之和的

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int n,t,cnt,res; int main()
{
ios::sync_with_stdio(false);
while(cin>>n)
{
cnt=;
for(int i=;i<n;i++)
{
cin>>t; //复杂度为 n
if(cnt==) //这意味着重新一个数
{
res=t; //重新的数 赋给res
cnt++;
}
else
{
if(t==res) cnt++;
else cnt--;
}
}
cout<<res<<endl;
}
}

然而时间上 也是勉强过

Ignatius and the Princess IV (简单DP,排序)的更多相关文章

  1. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  2. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  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. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

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

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

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

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

  7. hdu 1029 Ignatius ans the Princess IV

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

  8. Ignatius and the Princess IV

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

  9. Ignatius and the Princess IV(乱搞一发竟然过了)

    B - Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64 ...

随机推荐

  1. 基于jsplumb插件制作可拖拽、保存流程图、重绘保存后的流程图总结

    1.重点参考博文 https://blog.csdn.net/j_bean/article/details/78092647 2.关键点总结 1)实现可视区域图形画满后,拖动整个画布的效果 a.最好不 ...

  2. Nginx之开启压缩

    参考:https://blog.csdn.net/php12345679/article/details/80843939 https://blog.csdn.net/pf1234321/articl ...

  3. CF1277D Let's Play the Words?

    思路: 字符串其实只有0...0, 0...1, 1...0, 1...1四种. 实现: #include <bits/stdc++.h> using namespace std; ]; ...

  4. 感受typescript定义变量和数据类型的神奇魔力

    变量和数据类型 你的Javascript能力到达瓶颈?那是因为你还不会typescript.掌握TS,让你的开发更加准确简洁. 今天的学习中,我们接着从TS的数据类型和变量入手,感受它们的奇妙魔力. ...

  5. nightwatch对前端做自动化测试

    记录node环境使用nightwatch.selenium-server.chromedriver对部署后的前端页面进行自动化测试的项目搭建过程. 1.目标 能对部署后的前端项目进行自动化测试,能自动 ...

  6. Deepin升级Linux5.0内核(目前最新5.3-rc7)

    copy from:https://bbs.deepin.org/forum.php?mod=viewthread&tid=175411&extra=&mobile=no 以下 ...

  7. [转帖]java架构之路-(面试篇)JVM虚拟机面试大全

    java架构之路-(面试篇)JVM虚拟机面试大全 https://www.cnblogs.com/cxiaocai/p/11634918.html   下文连接比较多啊,都是我过整理的博客,很多答案都 ...

  8. python学习-59 hashlib模块

    hashlib模块 用于加密相关的操作,3.x里代替了md5模块和sha模块 加密功能 import hashlib obj = hashlib.md5() # 如果在md5里加上自己设置的参数,别的 ...

  9. PAT甲级题分类汇编——计算

    本文为PAT甲级分类汇编系列文章. 计算类,指以数学运算为主或为背景的题. 题号 标题 分数 大意 1058 A+B in Hogwarts 20 特殊进制加法 1059 Prime Factors ...

  10. 机器学习-HMM隐马尔可夫模型-笔记

    HMM定义 1)隐马尔科夫模型 (HMM, Hidden Markov Model) 可用标注问题,在语音识别. NLP .生物信息.模式识别等领域被实践证明是有效的算法. 2)HMM 是关于时序的概 ...