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< ...
随机推荐
- [洛谷P2657][SCOI2009]windy数
题目大意:不含前导零且相邻两个数字之差至少为$2$的正整数被称为$windy$数.问$[A, B]$内有多少个$windy$数? 题解:$f_{i, j}$表示数有$i$位,最高位为$j$(可能为$0 ...
- 使用google api material icons在网页中插入图标
在<head></head>中加入这一句: <link rel='stylesheet' href='http://fonts.googleapis.com/icon?f ...
- 获取oracle当前系统设置了哪些事件
ALTER SESSION SET EVENTS '10046 trace name context forever,level 12' 会话已更改. DECLARE EVENT_LEVEL NUMB ...
- Python代码规范
一:背景 用于规范化ocp python开发,对于使用python开发的程序使用统一的风格,便于代码的维护 二:python风格规范 分号:不要在行尾加分号,也不要用分号将两条命令放在同一行 括号:宁 ...
- Struts2 利用拦截器 interceptor 控制登陆和访问权限
最近学习了Struts2的登录和权限控制用到的是拦截器,需要在struts.xml中配置,每个action都默认的继承defaultStack,如果你用了别的拦截器,还需要手动引入defaultSta ...
- struts2学习问题(一)
一.struts2 Unknown tag (s:property). 解释:不识别标签 解决:这是sturts2的标签,导入相应的包<%@taglib prefix="s" ...
- 动态规划:DAG-嵌套矩形
据说DAG是动态规划的基础,想一想还真的是这样的,动态规划的所有状态和转移都可以归约成DAG DAG有两个典型模型,一个是嵌套矩形问题一个是硬币问题,这里仅介绍一个嵌套矩形问题 等二轮复习的时候再补上 ...
- shell之流程控制
一.if语句.while.for循环各种小例题 1.用户验证 [root@bogon ~]# cat buer.sh #!/bin/bash #user='buer' #password='1234. ...
- 使用state模块部署lamp架构
install_httpd: pkg.installed: - name: httpd httpd_running: service.running: - name: httpd - enable: ...
- [BZOJ3238][Ahoi2013]差异解题报告|后缀数组
Description 先分析一下题目,我们显然可以直接算出sigma(len[Ti]+len[Tj])的值=(n-1)*n*(n+1)/2 接着就要去算这个字符串中所有后缀的两两最长公共前缀总和 首 ...