A. Lala Land and Apple Trees
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/558/problem/A
Description
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?
Input
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
Output the maximum number of apples Amr can collect.
Sample Input
2
-1 5
1 5
Sample Output
10
HINT
题意
有一个人,一开始会往左边走或者往右边走,碰到苹果树就会拾起这棵苹果树的所有果子,然后再交换方向,重复这个过程
问你这个人最多能拿多少个果子
题解:
先左右按着坐标排一个序,很显然只有两种走法,挨个判一下谁最大就好了
代码
#include<stdio.h> #include<iostream> #include<math.h> #include<algorithm> using namespace std; struct node { int x,y; }; node a[101]; node b[101]; bool cmp(node aa,node bb) { return aa.x<bb.x; } int main() { int n; scanf("%d",&n); int num1=0,num2=0; for(int i=0;i<n;i++) { int c,d; cin>>c>>d; if(c<0) a[num1].x=-c,a[num1++].y=d; else b[num2].x=c,b[num2++].y=d; } sort(a,a+num1,cmp); sort(b,b+num2,cmp); int num=min(num1,num2); int ans=0; for(int i=0;i<num;i++) ans+=(a[i].y+b[i].y); if(num1>num2) ans+=a[num2].y; else if(num2>num1) ans+=b[num1].y; cout<<ans<<endl; }