Ignatius and the Princess IV (简单DP,排序)
方法一: 直接进行排序,输出第(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,排序)的更多相关文章
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- 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 ...
- kuangbin专题十二 HDU1029 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 --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- (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 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- Ignatius and the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- Ignatius and the Princess IV(乱搞一发竟然过了)
B - Ignatius and the Princess IV Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64 ...
随机推荐
- LeetCode:交替打印【1115】
LeetCode:交替打印[1115] 题目描述 我们提供一个类: class FooBar { public void foo() { for (int i = 0; i < n; i++) ...
- 【CUDA开发】CUDA开发琐碎知识
## 一维矩阵的加 //实现一个一维1*16的小矩阵的加法. //矩阵大小:1*16 //分配一个block,共有16个线程并发. #include <stdio.h> #includ ...
- python 爬虫实例(二)
环境: OS:Window10 python:3.7 描述 打开下面的网址,之后抓取其中的图片 https://music.163.com/#/artist/album?id=101988&l ...
- linux maven环境变量配置
export MAVEN_HOME=/opt/hjyang/soft/maven export MAVEN_HOME export PATH=$PATH:$MAVEN_HOME/bin
- Appium移动自动化测试-----(十二)appium API 之 TouchAction 操作
Appium的辅助类,主要针对手势操作,比如滑动.长按.拖动等. 1.按压控件 方法: press() 开始按压一个元素或坐标点(x,y).通过手指按压手机屏幕的某个位置. press(WebElem ...
- WDL学习笔记
WDL是什么? WDL是由Broad Institute开发的一种流程开发语言,全称workflow description language,官方链接如下:https://software.broa ...
- Visual Studio 2019 离线安装方法
1. 网址 1.1 阅读官方离线安装教程 离线安装官网 仔细阅读离线安装官网,差不多就能学会如何下载. 1.2 工作负荷和组件 ID 进入这个网址,Visual Studio 工作负荷和组件 ID,单 ...
- 用php的for循环输出四边形,各种三角形和菱形【含空心版本】
<?php // 实心版 //四边形 for( $i = 1; $i <=5; $i++ ){ for( $j = 1; $j <=5; $j++ ){ echo '*'; } ec ...
- PAT(B) 1084 外观数列(Java)
题目链接:1084 外观数列 (20 point(s)) 题目描述 外观数列是指具有以下特点的整数序列: d, d1, d111, d113, d11231, d112213111, - 它从不等于 ...
- 第二次用map23333
度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字.例如, ...