hdu 4974

来源:互联网 发布:q版地图制作软件 编辑:程序博客网 时间:2024/05/18 10:31

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974

A simple water problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 68    Accepted Submission(s): 42


Problem Description
Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or 1 for each competitor and add it to the total score of that competitor. The total score begins with zero. Here's an example: four competitors with name James, Victoria, Penghu, and Digo. First goes a competition between Penghu and Digo, and Dragon enjoys the competition and draw both 1 score for them. Then there’s a competition between James and Victoria, but this time Dragon draw 1 for Victoria and 0 for James. Lastly a competition between James and Digo is held, but this time Dragon really dislike the competition and give zeroes for each of them. Finally we know the score for each one: James--0, Victoria--1, Penghu--1, Digo--1. All except James are the Winner!

However, Dragon's mom comes back home again and close the TV, driving Dragon to his homework, and find out the paper with scores of all competitors. Dragon's mom wants to know how many competition Dragon watched, but it's hard through the paper. Here comes the problem for you, given the scores of all competitors, at least how many competitions had Dragon watched?
 

Input
The first line of input contains only one integer T(<=10), the number of test cases. Following T blocks, each block describe one test case.

For each test case, the first line contains only one integers N(<=100000), which means the number of competitors. Then a line contains N integers (a1,a2,a3,...,an).ai(<=1000000) means the score of i-th competitor.
 

Output
Each output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. Then for each case just puts a line with one integer, implying the competition at least should be watched by dragon.
 

Sample Input
132 3 4
 

Sample Output
Case #1: 5
 

Source
2014 Multi-University Training Contest 10
 
=。= 错误代码:因为数据弱了些,所以可以AC,但是有一种情况~~~~如果n为奇数,并且n个数都相同,那么就过不去~比如(2,2,2,2,2)
思路:n个数从小到大排序,从右边向左边遍历一遍即可~签到题;

#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <algorithm>#include <cstdio>const int maxn=101000;using namespace std;int a[maxn];int main(){ int t,n,test=0; cin>>t; while(t--) {  cin>>n;  test++;  if(n==0)  {   printf("Case #%d: %d\n",0);      continue;  }  for(int i=0;i<n;i++)scanf("%d",&a[i]);  sort(a,a+n);  int cnt=0,temp=a[n-1];  for(int i=n-2;i>=0;i--)  {        if(temp>=a[i])        {           cnt+=a[i];           temp=temp-a[i];           a[i]=0;        }        else        {          cnt=cnt+temp;          a[i]=a[i]-temp;          temp=a[i];        }  }  cnt+=temp;  printf("Case #%d: %d\n",test,cnt); } return 0;}

(2)正解:观察这n个数,如果总和的一半都要比这n个数的最大值小,那么结果就为这n个数的最大值;

                     否则,结果为(sum+1)/2;

  

#include <iostream>#include <string.h>#include <string>#include <stdio.h>const int maxn=101000;using namespace std;long long a[maxn];int main(){        int T,n,test=0;        cin>>T;        while(T--)        {           cin>>n;           test++;           long long maxx=0,sum=0;           for(int i=0;i<n;i++)           {              scanf("%I64d",&a[i]);              maxx=max(maxx,a[i]);              sum+=a[i];           }           printf("Case #%d: %I64d\n",test,max((sum+1)/2,maxx));        }  return 0;}

0 1
原创粉丝点击