Dracula and Ethan
Time Limit: 1 Sec Memory Limit: 256 MB
Description
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
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 (a 1,a 2,a 3,...,a n).a i(<=1000000) means the score of i-th competitor.
Output
Sample Input
1 3 2 3 4
Sample Output
HINT
题意:
题解:
用优先队列,每次尽量使结果保持1,1;
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef __int64 ll;
using namespace std;
const int inf = (int)1E9+;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
} //******************************* struct ss
{
int x;
friend bool operator < (ss s1,ss s2)
{
return s1.x<s2.x;
}
};
priority_queue< ss >q;
int main()
{ int T;
cin>>T;
int n;
int oo=;
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x=read();
ss xx;
xx.x=x;
q.push(xx);
}
ll ans=;
while(!q.empty())
{
ss a,b;
a=q.top();
q.pop();
b=q.top();
q.pop();
ans+=(b.x);
b.x=(a.x-b.x);
//printf("%d\n",b.x);
if(b.x!=)q.push(b);
if(q.size()<=)
{
b=q.top();
q.pop();
// printf("%d %d\n",b.x,ans);
ans+=(b.x);
break;
}
}
while(!q.empty())q.pop();
printf("Case #%d: ",oo++);
printf("%I64d\n",ans);
} return ;
}