链接:https://vjudge.net/problem/HDU-1029#author=0
题意:
给你n个数字,请你找出出现至少(n+1)/2次的数字。
思路:
dp,hash超时了,不知道是不是我的问题。
cnt记录数量,
目标数比其他数加起来还多。
所以当cnt为0时当前数为目标数,相同cnt++,不同cnt--。
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
const int INF = 0x7fffffff;
int main()
{
int n;
while (~scanf("%d", &n))
//while (cin >> n)
{
int cnt = 0,a,vis;
for (int i = 1;i <= n;i++)
{
scanf("%d", &a);
if (cnt == 0)
{
vis = a;
cnt++;
}
else
{
if (a == vis)
cnt++;
else
cnt--;
}
}
printf("%d\n", vis);
}
return 0;
}