HDU 1029
Title: Given the number n (n <= 999999 and n is odd) and the number of N, find at least (n+1)/2 times
How to solve the problem: N number traversal in the past, you can use a map (you can also use an array) to record the number of times each occurrence,
If the number of times is reached (n+1)/2, i.e. output A[I]
Note can appear (n+1)/2 times the number of up to only one
/*HDU 1029 *ignatius and the Princess IV---dp*/#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespacestd;Const intMAXN =1000005;intA[maxn];map<int,int>m;intMain () {#ifdef _local freopen ("D:\\input.txt","R", stdin);#endif intN; intans; while(SCANF ("%d", &n) = =1) {m.clear (); //Remember to empty the original stuff. for(inti =1; I <= N; ++i) {scanf ("%d", A +i); ++M[a[i]]; if(M[a[i]] = = (n +1) /2) {ans=A[i]; } }//For (i)printf"%d\n", ans); } return 0;}
View Code
HDU 1029 Ignatius and the Princess IV---water problem