#include <bits/stdc++.h>
using namespace std; int main(){
int n;
while(~scanf("%d",&n)){
int x,ans,cnt = 0;
while(n--){
scanf("%d",&x);
if(cnt == 0){
ans = x;
cnt++;
}else{
if(x == ans)
cnt++;
else
cnt--;
}
}
printf("%d\n",ans);
}
return 0;
}
/*

题目要求一个数至少出现(n+1)/2次。用cnt来记录解出现的次数,出现了正确解就令cnt自增1,不是正确解就使cnt自减1。

那么,正确解对应的cnt一定是不小于1的。可以用一个极端的例子来说明下:输入3 3 3 3 3 3 2 1 5 6 8,开始当ans=3时,cnt=6,

那么继续执行num!=3了,cnt开始自减,但最终cnt=1,始终不会进入程序if(cnt==0){}内部执行了。

利用最终的cnt,还可以计算出解出现的次数。*/

  

#include <iostream>
#include <stdlib.h>
using namespace std; int a[1000000]; int cmp(const void * x,const void* y)
{
return (*(int*)x - *(int*)y);
} int main()
{
int n,i,b,j,flag; while(cin >> n)
{
j = b = 0;
for(i = 0;i<n;i++)
{
cin >> a[i];
}
qsort(a,n,sizeof(int),cmp);
flag = a[0];
for(i = 0;i<n;i++)
{
if(a[i] == flag)
b++;
else if(a[i]!=flag)
{
if(b>=(n+1)/2)
{
break;
}
b = 0;
flag = a[i];
}
}
cout << flag << endl;
} return 0;
}

 

#include<stdio.h>
#include<string.h>
int map[500001];
int main()
{
int n,i,a,max;
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
for(i=0;i<n;i++)
{
scanf("%d",&a);
map[a]++;
if(map[a]>=(n+1)/2)
max=a;
}
printf("%d\n",max);
}
return 0;
}

  

#include <iostream>
#include <cstring>
using namespace std;
int a[1000001];
int main()
{
int x, n, i, num;
while(cin >> n)
{
memset(a, 0, sizeof(a));
for(i = 0; i < n; i++)
{
cin >> x;
a[x]++;
if(a[x] == (n+1)/2)//只要成立肯定是最大的了即保存再输出
num = x;
}
cout << num << endl;
}
return 0;
}

  

多元素即在数列中出现次数多于n/2的元素

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

HDU 1029 Ignatius and the Princess IV(数论)的更多相关文章

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

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

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

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

  4. hdu 1029 Ignatius ans the Princess IV

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

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

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

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

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

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

  8. HDU 1029 Ignatius and the Princess IV

    解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...

  9. HDU 1029 Ignatius and the Princess IV DP

    kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...

随机推荐

  1. AngularJS设置文本样式小程序

    做了一个这样的程序通过选择框选择样式,下面的段落样式跟着改变.就是想做这么一个东西. <!DOCTYPE html> <html lang="en"> &l ...

  2. [CF1066C]Books Queries

    题目大意:维护一个数列,要求在左边插入一个数,在右边插入一个数,查询一个数的排名 题解:可以双指针,开个数组存每个数的位置 卡点:无 C++ Code: #include <cstdio> ...

  3. BZOJ1415: [Noi2005]聪聪和可可 最短路 期望概率dp

    首先这道题让我回忆了一下最短路算法,所以我在此做一个总结: 带权: Floyed:O(n3) SPFA:O(n+m),这是平均复杂度实际上为O(玄学) Dijkstra:O(n+2m),堆优化以后 因 ...

  4. 使用setTimeout延时10ms执行onunloadcancel

    在做Web开发时,我们经常用到页面关闭事件onbeforeunload,可以给用户一个选择放弃关闭的机会,就比如这个博客编辑器.如果用户选择了离开,那么onunload事件自然会触发:但若用户选择了取 ...

  5. codeforces 1015B

    B. Obtaining the String time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. 最小k度最小生成树模板

    代码是抄的 题解是瞄的 可我想学习的心是真的嘤嘤嘤 然而 还是上传一份ioi大神的论文吧 链接:https://pan.baidu.com/s/1neIW9QeZEa0hXsUqJTjmeQ 密码:b ...

  7. 复杂的json分析

    在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...

  8. Hibernate 三种状态变化 与 sql 语句的关系

    前言:在Hibernate中有三种状态,对它的深入理解,才能更好的理解hibernate的运行机理,刚开始不太注意这些概念,后来发现它是重要的.对于理解hibernate,JVM和sql的关系有更好的 ...

  9. C++中的各种“神奇”东西

    将光标放到任意的位置 void gotoxy(int x,int y)//位置函数 { COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(Ge ...

  10. RPC-Thrift(二)

    TTransport TTransport负责数据的传输,先看类结构图. 阻塞Server使用TServerSocket,它封装了ServerSocket实例,ServerSocket实例监听到客户端 ...