HDU 1029 Ignatius and the Princess IV(数论)
#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(数论)的更多相关文章
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- 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 ...
- 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 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [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 ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- 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 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
随机推荐
- Python 类和对象-下
类的常用函数 #issubclass() 检测一个类是否是另外一个或者一组类中的子类 class Father: pass class Mother: pass class LaoWang: pass ...
- java的多构造函数的处理方式
/** * */ package P; import java.awt.List; import java.lang.reflect.Array; import java.util.ArrayList ...
- PowerShell官方文档
PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...
- codevs1063 合并果子 优先队列(小根堆)
题目传送门 这道题很容易想到优先把两堆重量最小的合并比较优 然后乱搞一下就可以啦 #include<cstdio> #include<cstring> #include< ...
- [bzoj4765]普通计算姬——分块
Brief Description 给定一棵n个节点的带权树,节点编号为1到n,以root为根,设sum[p]表示以点p为根的这棵子树中所有节点的权 值和.支持下列两种操作: 1 给定两个整数u,v, ...
- bzoj 1045糖果传递 数学贪心
首先我们假设平均数为ave 那么对于第1个人,我们假设他给第N个人K个糖果,第2个人给1,第3个人给2,第n个人给第n-1个人 那么对于第1个人给完n,第2个人给完1,第一个人不会再改变糖果数了,所以 ...
- Maven的默认中央仓库以及修改默认仓库&配置第三方jar包从私服下载
当构建一个Maven项目时,首先检查pom.xml文件以确定依赖包的下载位置,执行顺序如下: 1.从本地资源库中查找并获得依赖包,如果没有,执行第2步. 2.从Maven默认中央仓库中查找并获得依赖包 ...
- SuSE Linux修改网卡配置
http://blog.chinaunix.net/uid-20765159-id-1850854.html SuSE Linux修改网卡配置 2010 SuSE linux ...
- 用maven创建第一个SpringMVC
首先创建一个maven项目,然后依次完成如下配置: 在pom.xml加入如下基础配置,利用maven加载我们所需要的jar: <project xmlns="http://maven. ...
- linux下卸载Oracle
1.卸载数据库软件--10g[oracle]# cd /u01/app/oracle/product/10.2.0/db_1/oui/bin[oracle]# ./runInstaller -igno ...