hdu 1029 Ignatius and the Princess IV(水题,map)
2014-04-03 15:22
531 查看
小记:细节决定成败。 最近又被朋友问及到这样一道题,说如果大数据,如何O(n)实现。实在没想通
思路:题目没说给定的数可能有多大,如果是long long型,就不能用数组来叠加了。这里选择用map将long long 映射到int,进行计数。
因为对于n=2这个是一个特殊情况。我们必须输出这两个数,它们都是special number。
代码:
我们从头一个数一个数的判断,如果某一个数是最多的那个数,那么去掉这个数后剩下的数中,这个数还会是最多的,
我们利用抵消原理,如果碰上两个不相同的数,那么我们就要抵消掉一个,
假设从第一个开始,用cnt来计数,res表示计数的数,当当前的数等于res时,cnt++
不等于时,cnt--,
而若cnt=0的时候,那么说明res这个数肯定就不是最多的那个数,此时我们再将res和cnt初始化为当前的数,即res等于当前的数,cnt=1
这个想法简直是太棒了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8
const int MAX_ = 1000010;
const int N = 100010;
const int INF = 0x7fffffff;
int main(){
int n, cnt, k, res;
while(~scanf("%d",&n)){
cnt = 0;
while(n--){
scanf("%d",&k);
if(cnt == 0)res = k,cnt = 1;
else {
if(k == res)cnt++;
else cnt --;
}
}
printf("%d\n",res);
}
return 0;
}
思路:题目没说给定的数可能有多大,如果是long long型,就不能用数组来叠加了。这里选择用map将long long 映射到int,进行计数。
因为对于n=2这个是一个特殊情况。我们必须输出这两个数,它们都是special number。
代码:
#include <iostream> #include <cstdio> #include <map> using namespace std; map<long long, int>mp; map<long long, int>::iterator q; int main(){ long long n,m, ans, T; bool flag; while(cin>>n){ mp.clear(); flag = 0;T = n; while(n--){ cin>>m; mp[m]++; if(mp[m] == (T+1)/2){ if(flag)cout<<" ";flag=1;cout<<m;} } cout<<endl; } return 0; }
#include <iostream> #include <cstdio> #include <map> using namespace std; const int MAX_ = 130; int p[MAX_]; map<long long, int>mp; map<long long, int>::iterator q; int main(){ //freopen("f:\\out.txt","w",stdout); long long n,m, ans, T; while(cin>>n){ mp.clear(); ans = 0;T = n; while(n--){ cin>>m; mp[m]++; if(mp[m] == (T+1)/2){ p[ans++] = m; } } cout<<p[0]; for(int i = 1; i < ans; ++i) cout<<" "<<p[i]; cout<<endl; } return 0; }
我们从头一个数一个数的判断,如果某一个数是最多的那个数,那么去掉这个数后剩下的数中,这个数还会是最多的,
我们利用抵消原理,如果碰上两个不相同的数,那么我们就要抵消掉一个,
假设从第一个开始,用cnt来计数,res表示计数的数,当当前的数等于res时,cnt++
不等于时,cnt--,
而若cnt=0的时候,那么说明res这个数肯定就不是最多的那个数,此时我们再将res和cnt初始化为当前的数,即res等于当前的数,cnt=1
这个想法简直是太棒了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8
const int MAX_ = 1000010;
const int N = 100010;
const int INF = 0x7fffffff;
int main(){
int n, cnt, k, res;
while(~scanf("%d",&n)){
cnt = 0;
while(n--){
scanf("%d",&k);
if(cnt == 0)res = k,cnt = 1;
else {
if(k == res)cnt++;
else cnt --;
}
}
printf("%d\n",res);
}
return 0;
}
相关文章推荐
- 目标检测的图像特征提取 HOG特征
- 一键快速搭建lamp
- C#实现网页内容正文抓取
- 面向对象技术之需求分析:usecase图
- PHP扩展redis使用手册
- VS2010配置64bit的GSL
- break ,continue ,return 的区别及作用?
- Linux设备驱动程序学习(6)-高级字符驱动程序操作[(3)设备文件的访问控制]
- android调用.net的webservice的例子获取手机的归属地
- Generic detail view DetailView must be called with either an object pk or a slug.
- android:descendantFocusability用法简析
- Apache性能优化、超时设置,linux 重启apache
- java打印九九乘法表
- PHP中try{}catch{}的具体用法详解
- NYOJ14会场的安排问题
- ios 面试题(1)
- 在vi中如何删除一行,复制多行,查找字符?
- Linux设备驱动程序学习(5)-高级字符驱动程序操作[(2)阻塞型I/O和休眠]
- hdu 最大报销额
- logistic回归