StonesTime Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3700 Accepted Submission(s): 2417 Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time.
Input In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.
Output Just output one line for one test case, as described in the Description.
Sample Input 2 2 1 5 2 4 2 1 5 6 6
Sample Output 11 12
Author Sempr|CrazyBird|hust07p43
|
题意:一条路上面有许多的石头,我从路的起点开始走会遇见石头,遇见第奇数块石头,就把它往前扔,扔到它能扔到最大的距离;遇见第偶数块石头,就不管,若果同一个地点有两个及以上的石头,我先遇见的是能够扔的距离较近的石头,问我从起点到
最后一个石头扔到到位置的距离。
思路:遇见石头,把石头扔出去,可以联想到队列的入队与出队,针对同一个地点有两个及以上的石头,我先遇见的是能够扔的距离较近的石头,可以使用优先队列,扔的较近的石头的优先级就越高。
AC代码:
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int,int> P;
struct cmp
{
bool operator ()(P &a,P &b)
{
if(a.first==b.first){
return a.second>b.second;
}
return a.first>b.first;//最小值优先
}
};
priority_queue<P,vector<P>,cmp > q;
int main(){
int T;scanf("%d",&T);
while(T--){
int m;scanf("%d",&m);
while(m--){
int p,d;scanf("%d%d",&p,&d);
q.push(P(p,d));
}
int stone=0;
int ans=0;
while(!q.empty()){
stone++;
P tmp=q.top();
ans=max(ans,tmp.first);
q.pop();
if(stone%2){
int p=tmp.first;
int d=tmp.second;
q.push(P(p+d,d));
}
}
printf("%d\n",ans);
}
return 0;
}