acm hdoj 1157

Who‘s in the Middle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3136 Accepted Submission(s): 1171
 

Problem Description

FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median‘ cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.


Input

* Line 1: A single integer N

* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.


Output

* Line 1: A single integer that is the median milk output.


Sample Input

5
2
4
1
3
5


Sample Output

3

Hint

INPUT DETAILS: Five cows with milk outputs of 1..5 OUTPUT DETAILS: 1 and 2 are below 3; 4 and 5 are above 3.

第一次:结果错误

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

int comp(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}

int cow[10005];

int main()
{
    //freopen("1.txt","r",stdin);

    int N;
    scanf("%d",&N);
    int i=0;
    for(i=0;i<N;i++)
    {
        scanf("%d",&cow[i]);
    }
    qsort(cow,N,sizeof(int),comp);
    printf("%d\n",cow[N/2]);

    return 0;
}

让后我从网上找了别人的代码看,看看没什么不同

第二次,每次都处理,但是超时

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

int comp(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}

int cow[10005];

int main()
{
    //freopen("1.txt","r",stdin);

    int N;
    while(scanf("%d",&N))
    {

        int i=0;
        for(i=0;i<N;i++)
        {
            scanf("%d",&cow[i]);
        }
        qsort(cow,N,sizeof(int),comp);
        printf("%d\n",cow[N/2]);
    }

    return 0;
}

第三次,加上了eof

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

int comp(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}

int cow[10005];

int main()
{
    //freopen("1.txt","r",stdin);

    int N;
    while(scanf("%d",&N)!=EOF)
    {

        int i=0;
        for(i=0;i<N;i++)
        {
            scanf("%d",&cow[i]);
        }
        qsort(cow,N,sizeof(int),comp);
        printf("%d\n",cow[N/2]);
    }

    return 0;
}
时间: 2024-10-09 13:50:31

acm hdoj 1157的相关文章

[acm]HDOJ 2064 汉诺塔III

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2064 汉诺塔变种,只能从中间专业,递归关系为:f(n)=3*f(n-1)+2. 1 //汉诺塔变种,只能从中间转移 2 //11485816 2014-08-19 08:44:47 Accepted 2064 0MS 368K 307 B G++ 空信高手 3 #include<iostream> 4 #include<cstdio> 5 using namespace std; 6

[acm]HDOJ 2059 龟兔赛跑

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2059 起点和终点,共n+2个点,n+2个状态,简单DP即可. 1 //11512698 2014-08-21 17:11:55 Accepted 2059 2 //62MS 368K 969 B G++ 空信高手 3 //起点和终点,共n+2个点,n+2个状态,简单DP即可 4 #include<iostream> 5 #include<cstdio> 6 using namespa

[acm]HDOJ 3082 Simplify The Circuit

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3082 字符串处理+并联电阻公式 1 //11481261 2014-08-18 16:52:47 Accepted 3082 0MS 384K 733 B G++ 空信高手 2 #include<string> 3 #include<iostream> 4 #include<cstdio> 5 #include<cstdlib> 6 7 using names

[acm]HDOJ 1200 To and Fro

题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1200 简单字符串处理,找规律 1 /* 2 11509672 2014-08-21 11:32:55 Accepted 3 1200 0MS 380K 442 B G++ 空信高手 4 */ 5 #include<iostream> 6 #include<string> 7 #include<cstdio> 8 using namespace std; 9 int mai

[acm]HDOJ 2673 shǎ崽 OrOrOrOrz

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2673 拍两次序,交替输出 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 using namespace std; 5 #define MAX_NUMBER 10000 6 bool myfunction1 (int i,int j) { return (i<j); } 7 bool m

玲珑杯 1157 造物主的戒律

传送门:http://www.ifrog.cc/acm/problem/1157 题意: 给一个数组,n多次询问,每次询问区间[l,r]中小于等于x的第k1小的数,大于x的第k2小的数. 题解: 比较裸的主席树了,多了个限制,对于每个询问的前一部分直接求区间第k1小的数和x比较一下就行了,对于后一部分查询先判断x为第几小的数然后加上k2就行了... 题解说是可持续化字典树(不还是主席树吗)...还没想太明白... 1 #include <bits/stdc++.h> 2 using names

HDOJ ACM题目分类

模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 120

2016 ACM/ICPC Asia Regional Qingdao Online1001 &amp;&amp;hdoj 5878 I Count Two Three

题目链接 打表二分..后来发现这是51nod中的一个原题 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long int ll; 6 const ll INF = 1e9 + 100000; 7 ll a[100005]; 8 int cnt = 0; 9 int x[] = {2,3,5, 7}; 10 voi

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa