题目链接:A. Lala Land and Apple Trees
题面:
Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.
Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x?=?0 position. At the beginning, he can choose whether to go right or left. He‘ll continue in his direction until he meets an apple tree he didn‘t visit before. He‘ll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn‘t visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn‘t visit in the direction he is facing.
What is the maximum number of apples he can collect?
The first line contains one number n (1?≤?n?≤?100), the number of apple trees in Lala Land.
The following n lines contains two integers each xi, ai (?-?105?≤?xi?≤?105, xi?≠?0, 1?≤?ai?≤?105), representing the position of the i-th tree and number of apples on it.
It‘s guaranteed that there is at most one apple tree at each coordinate. It‘s guaranteed that no tree grows in point 0.
Output the maximum number of apples Amr can collect.
2 -1 5 1 5
10
3 -2 2 1 4 -1 3
9
3 1 9 3 5 7 10
9
In the first sample test it doesn‘t matter if Amr chose at first to go left or right. In both cases he‘ll get all the apples.
In the second sample test the optimal solution is to go left to x?=??-?1, collect apples from there, then the direction will be reversed, Amr has to go to x?=?1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x?=??-?2.
In the third sample test the optimal solution is to go right to x?=?1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.
解题: 如果正负位置数量相同,那么都可取,如果不相同,那么少的一边全取,多的一边取离0近的(少的数量+1个)。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <string> #include <map> #include <vector> #include <set> #include <queue> using namespace std; struct apple_Tree { int amount,pos; }store[100010]; bool cmp(apple_Tree a,apple_Tree b) { return a.pos<b.pos; } int main() { int n,cntminus=0,cntzheng=0; long long ans=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&store[i].pos,&store[i].amount); if(store[i].pos<0)cntminus++; else cntzheng++; } if(cntminus==cntzheng) { for(int i=0;i<n;i++) ans+=store[i].amount; cout<<ans<<endl; } else { sort(store,store+n,cmp); if(cntminus<cntzheng) { for(int i=0;i<2*cntminus+1;i++) { ans+=store[i].amount; } } else { for(int j=n-1;j>=(n-2*cntzheng-1);j--) ans+=store[j].amount; } cout<<ans<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
原文:http://blog.csdn.net/david_jett/article/details/46889909