4

HDU1029 Ignatius and the Princess IV

 3 years ago
source link: https://arminli.com/hdu1029/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Armin's Blog

HDU1029 Ignatius and the Princess IV

February 17, 2016

题目链接

题意:给 n(奇数)个数,找到一个数,这个数最少出现(n+1)/ 2 次。

sort 一下输出中间位置的数就可以了。。看讨论版有 map 搞的,有 DP 的。。。根本不需要。

#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int a[1000000];
int main(){
    //freopen("a.txt", "r", stdin);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; i++){
            scanf("%d", &a[i]);
        }
        sort(a, a+n);
        int ans = (n+1)/2;
        cout << a[ans] << endl;
    }
    return 0;
}

Profile picture

Written by Armin Li , a venture capitalist. [Weibo] [Subscribe]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK