#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. 【bzoj2326】[HNOI2011]数学作业 矩阵乘法

    题目描述 题解 矩阵乘法 考虑把相同位数的数放到一起处理: 设有$k$位的数为$[l,r]$,那么枚举从大到小的第$i$个数(即枚举$r-i+1$),考虑其对$Concatenate(l..r)$的贡 ...

  2. Div+Css中transparent制作奥运五环

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. hdu 1598 find the most comfortable road (并查集)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. 2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)

    题解: (并查集处理往上跳的时候,一定要先让u,v往上跳到并查集的祖先,不然会wa掉) 代码如下: #include <iostream> #include <algorithm&g ...

  5. 【题解】APIO2007动物园

    首先一眼感受到这题特别的性质……5个?这么小的,感觉就像是状压.脑补了一下,如果没有环的话应该很好做吧……有环怎么办?5真的很小的,随便乱搞肯定也可以.那就放在外面暴力枚举吧.然后正解就出来了. 然而 ...

  6. [NOIP2018 TG D2T1]旅行

    题目大意:$NOIP\;TG\;D2T1$ 题解:一棵树的很简单,第一个点一定是$1$,只需要对每个节点,找最小的没有访问过的节点访问即可,我写的是$O(n\log_2n)$. 考虑基环树的部分,一个 ...

  7. js保存用户名与密码

    <script>   window.onload = function(){     var oForm = document.getElementById('loginForm');   ...

  8. BZOJ1051:受欢迎的牛(并查集 / Tarjan)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8161  Solved: 4460 Description ...

  9. 获得edittext的图片大小

    1.在布局文件中编写控件,有2张图片 <EditText android:id="@+id/edit" android:background="@drawable/ ...

  10. Linux Top 命令参数解析

    转载自:http://www.jb51.net/LINUXjishu/34604.html TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户 ...