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,就加上所有负轴上面坐标的苹果以及和负轴同等数量的坐标上的苹果,再往后加多一个坐标的苹果,另一种情况的也差不多。
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 Round 263 (Div 2)D】【树形DP】Appleman and Tree 树上割k个黑点为k块的方案数
- Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees(模拟)
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- Codeforces Round #312 (Div. 2) Amr and Chemistry(位运算+暴搜)
- Codeforces Round #263 (Div. 1) B. Appleman and Tree(给一棵树,每个点为白色或黑色,切断一些边,使得每个连通块有且仅有一个黑点,问划分方案数。)
- Codeforces Round #312 (Div. 2)-B. Amr and The Large Array-暴力
- Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry
- Codeforces Round #312 (Div. 2)-A. Lala Land and Apple Trees-水题-模拟
- 【Codeforces Round 331 (Div 2)D】【DP 记忆化搜索 期望DP区间性质 好题】Wilbur and Trees 砍树随机从右从左概率左倒右倒的期望底面覆盖长度
- Codeforces Round #312 (Div. 2) A.Lala Land and Apple Trees
- Codeforces Round #169 (Div. 2):E - Little Girl and Problem on Trees
- 【Codeforces Round 169 (Div 2) E】【数据结构区间维护】Little Girl and Problem on Trees 大菊花树的距离性修改查询
- 【Codeforces Round 263 (Div 2)A】【水题 认真读题】Appleman and Easy Task 每个点的相邻o是否都为偶数
- 【Codeforces Round 263 (Div 2)B】【水题 贪心】Appleman and Card Game 选字符,每个字符个数的平方
- Codeforces Round #263 (Div. 1) A. Appleman and Toastman
- 【Codeforces Round 263 (Div 2)C】【贪心 哈弗曼思维】Appleman and Toastman 每个非1size子树延展为2子树的最大权
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力