方法一:    直接进行排序,输出第(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. vue 的反向代理

    情景描述: 原本的vue打包文件是放在.net core 项目的www文件夹下去发布的.这样运行没问题,但是公司领导让服务器单独部署vue,前后端要完全分离.然后这样就出问题了,有一个上传接口的地址一 ...

  2. golang实现无限级菜单(beego框架下)

    原文地址  http://www.niu12.com/article/37 golang实现无限级菜单(beego框架下) 数据表如下 -- ---------------------------- ...

  3. jenkins最新版下载安装

    前提:安装配置 jetty 在线下载jetty# wget http://download.eclipse.org/jetty/8.1.17.v20150415/dist/jetty-distribu ...

  4. Linux下的Curses库的源码下载与安装

    curses库是可以在linux终端中写出字符用户界面的一个库,现在较新的版本应该是ncurses库,现在已经很少有人再去使用他了,所以想拥有这个库并且在linux下写出自己用户界面的可以参考一下本博 ...

  5. 任务调度之Quartz.Net基础

    最近公司要求将之前的系统设计文档补上,于是大家就都被分配了不同的任务,紧锣密鼓的写起了文档来.发现之前项目中使用了Quartz.Net来做一些定时任务的调度,比如定时的删除未支付的订单,定时检查支付状 ...

  6. Jenkins在Mac上的安装与维护

    卸载 开篇提前说下, 因为很久之前用安装包装过的, 我要先卸载: /Library/Application\ Support/Jenkins/Uninstall.command 注意:如果没有权限的话 ...

  7. 用 Python 把微信小程序的二维码转化成图片

    官方文档 import cString import requests from tornado.web import authenticated, RequestHandler URL = 'htt ...

  8. eclipse设置text file encoding UTF-8和文件的换行符 Unix 格式

    阿里华山版java开发手册代码格式第10条: 步骤:1.Window - Preferences, 2.左边选择 General - Workspace , 3.右边Text file encodin ...

  9. js复制内容到粘贴板

    点击右边内容:<span onclick="copyContent(this);" title="点击复制">啊,我被复制了</span> ...

  10. Java8新特性 - 方法引用与构造器引用

    方法引用 若Lambda体中的内容有方法已经实现了,我们可以使用"方法应用",可以理解为方法引用是Lambda表达式的另外一种表现形式. 使用操作符"::"将方 ...