HDU 1029 Ignatius and the Princess IV
来源:互联网 发布:湖南有线网络怎样 编辑:程序博客网 时间:2024/06/16 19:23
题意:给你一列数(一共n个,n是奇数),其中有一个特殊的数,它会出现至少n/2次,问你这个数是什么;
两种做法。
最多999999个数,我首先想到的是离散化+hash,然后O(n)跑一遍,计数就行了;
#include<iostream>#include<algorithm>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#define LL long longusing namespace std;int arr[1100000];int _hash[1100000];int num[1100000];int main(){int n;while(~scanf("%d",&n)){for(int i=0;i<n;++i){scanf("%d",&arr[i]);_hash[i]=arr[i];}sort(_hash,_hash+n);int cnt=unique(_hash,_hash+n)-_hash;memset(num,0,sizeof(num));for(int i=0;i<n;++i){int index=lower_bound(_hash,_hash+cnt,arr[i])-_hash;num[index]++;}int pos=-1;for(int i=0;i<cnt;++i){if(num[i]>=n/2){pos=i;}}printf("%d\n",_hash[pos]);}}
还有一种就是算是DP吧;排一下序,再计数;
#include<iostream>#include<algorithm>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#define LL long longusing namespace std;int arr[1100000];int main(){int n;while(~scanf("%d",&n)){for(int i=1;i<=n;++i){scanf("%d",&arr[i]);}sort(arr+1,arr+n+1);arr[0]=arr[1]-1;int temp=0;int tempfin=0;int pos=0;for(int i=1;i<=n;++i){if(arr[i]!=arr[i-1])temp=0;else{temp++;if(temp>tempfin){tempfin=temp;pos=i;if(tempfin>n/2)break;}}}printf("%d\n",arr[pos]);}}
0 0
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- Ignatius and the Princess IV hdu 1029
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU-1029-Ignatius and the Princess IV
- HDU--1029--Ignatius and the Princess IV
- hdu - 1029 -Ignatius and the Princess IV
- 【DSP开发】DSP通用并行端口uPP
- LoadRunner 11 安装及破解
- 在BIOS模式下USB的启动热键是什么?(BIOS设置图解教程)
- PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
- Hibernate配置文件(hibernate.cfg.xml)
- HDU 1029 Ignatius and the Princess IV
- django 个人博客系统开发 - 文章归档
- mysql如何开启远程连接
- 最全的tcpdump使用详解
- contiki学习笔记1:helloworld+process结构分析
- 常用字符编码简介
- 【DSP开发】DSP程序优化
- 如何利用github打造博客专属域名
- java SpringMVC 搭建的步骤