hdu 1029 Ignatius and the Princess IV(排序)

题意:求出现次数>=(N+1)/2的数

思路:排序后,输出第(N+1)/2个数

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int a[999999];

int main(){
    int n,i;
    while(~scanf("%d",&n)){
        for(i=0;i<n;++i)
            scanf("%d",&a[i]);
        sort(a,a+n);
        printf("%d\n",a[(n+1)/2-1]);
    }
    return 0;
}

时间: 2024-08-11 09:41:21

hdu 1029 Ignatius and the Princess IV(排序)的相关文章

[2016-03-27][HDU][1029][Ignatius and the Princess IV]

时间:2016-03-30 22:03:01 星期三 题目编号:[2016-03-27][HDU][1029][Ignatius and the Princess IV] 题目大意:给定n个数字,输出其中出现次数超过n/2的数字 #include <algorithm> #include <cstdio> using namespace std; const int maxn = 1E6 + 10; int a[maxn]; int main(){ int n; while(~sc

HDU 1029 Ignatius and the Princess IV --- 水题

HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出现的次数, 若次数一旦达到(n+1)/2,即输出a[i] 注意能出现(n+1)/2次数的最多只有一个 /* HDU 1029 *Ignatius and the Princess IV --- dp */ #include <cstdio> #include <cstring> #in

[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)

笨蛋的难题(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述        笨蛋之所以称为笨蛋,是因为他有点路痴.他一旦不高兴,就必然一个人漫无目的的出去走走.今天下雨了,他又不高兴了,怎么办?那就出去走呗,这不又丢了,这次幸好记下出来时的方向,并且在一张纸上密密麻麻的记下了他拐的弯(他很聪明吧,拐的弯都是90度的弯),0代表左拐,1代表右拐,那么多0.1,他实在看不下去了,正好遇见善良加聪明的你,你能告诉他,他现在面向哪吗? 输入 多组测试数据 第一行 输入:

HDU 1029: Ignatius and the Princess IV

Ignatius and the Princess IV ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n) { map<int, int>v; int t,ans=-1; for (int i = 1;

HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can never pass the next test." feng5166 says. "I will tell you an odd number N, and then N integers. There will be a special integer among them, you hav

HDU 1029 Ignatius and the Princess IV DP

kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8 #include<vector> 9 #include<

杭电 1029 Ignatius and the Princess IV

http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others) Total Submission(s): 16754    Accepted Submission(s): 6730 Problem Description "OK, you ar

Hdu oj 1029 Ignatius and the Princess IV

题目:点击打开链接 代码一: #include<stdio.h> #include<string.h> int b1[1000000]; int main() { int n; while(~scanf("%d",&n)) { int i; int a,t; memset(b1,0,sizeof(b1)); for(i=0;i<n;i++) { scanf("%d",&a); b1[a]++; if(b1[a]>=

【HDU - 1029】Ignatius and the Princess IV (水题)

Ignatius and the Princess IV  先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行为N个用空格隔开的整数. Output 对于每组数据,输出一行,表示要求找到的那个数 Sample Input 5 1 3 2 3 3 11 1 1 1 1 1 5 5 5