您的位置:首页 > 移动开发

Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees

2018-04-10 00:17 459 查看
time limit per test 1 second

memory limit per test 256 megabytes

inputstandard input

outputstandard output

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 test(s)

input

2

-1 5

1 5

output

10

input

3

-2 2

1 4

-1 3

output

9

input

3

1 9

3 5

7 10

output

9

这道题的意思是输入坐标和坐标上苹果的数量,从0开始走,往左边开始或者从右边开始,遇到一个从未到过的树木就摘完上面的苹果,然后往反方向走,再到一个从未到过的树木,再摘苹果,再往反方向走,如此反复,如果没有遇到从未遇到过的树就不回头。

要节约时间就要判断正轴和负轴上面坐标的数量,如果相等,直接把所有苹果树木加上,如果正轴上的坐标为N,负轴上的坐标为M,且N>M,就加上所有负轴上面坐标的苹果以及和负轴同等数量的坐标上的苹果,再往后加多一个坐标的苹果,另一种情况的也差不多。

#include<iostream>
#include<algorithm>
using namespace std;
struct tree
{
int position;
int number;
};
bool cmp(tree a,tree b)
{
return a.position<b.position;
}
int main()
{
int n;
while(cin>>n)
{
tree num[100010];
int positive=0;
int nagetive=0;
int ans=100010;
int kk=-100010;
int nut=0;
int sum=0;
int kkk=0;
for(int i=0;i<n;i++)
{
cin>>num[i].position;
cin>>num[i].number;
if(num[i].position<0)
{
nagetive++;
kk=num[i].position>kk?num[i].position:kk;
}
else
{
positive++;
ans=num[i].position<ans?num[i].position:ans;
}
}
int k;
if(positive==nagetive)
{
for(int i=0;i<n;i++)  sum+=num[i].number;
}
if(positive!=nagetive)
{
sort(num,num+n,cmp);
for(int i=0;i<n;i++)
{
if(ans==num[i].position)  nut=i;
if(kk==num[i].position)  kkk=i;
}
if(positive>nagetive&&nagetive!=0)
{
k=positive-nagetive;
for(int i=0;i<n-k+1;i++)  sum+=num[i].number;
}
if(positive<nagetive&&positive!=0)
{
k=nagetive-positive;
for(int i=k-1;i<n;i++)  sum+=num[i].number;
}
if(positive==0&&nagetive!=0)  sum+=num[kkk].number;
if(positive!=0&&nagetive==0)  sum+=num[nut].number;
}
cout<<sum<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces
相关文章推荐