Codeforces Round #312 (Div. 2) 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?

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 xiai ( - 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 51 5

output

10

input

3-2 21 4-1 3

output

9

input

31 93 57 10

output

9

Note

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.

题解:正负贪心一下,注意两边相等的情况。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(‘ ‘)
 8 #define ENT putchar(‘\n‘)
 9 using namespace std;
10 inline int read(){
11     int x=0,sig=1;char ch=getchar();
12     while(!isdigit(ch)){if(ch==‘-‘)sig=-1;ch=getchar();}
13     while(isdigit(ch))x=10*x+ch-‘0‘,ch=getchar();
14     return x*=sig;
15 }
16 inline void write(int x){
17     if(x==0){putchar(‘0‘);return;}if(x<0)putchar(‘-‘),x=-x;
18     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
19     for(int i=len-1;i>=0;i--)putchar(buf[i]+‘0‘);return;
20 }
21 void init(){
22     int n=read(),neg=0;
23     pair<int,int>a[n];
24     for(int i=0;i<n;i++){
25         a[i].first=read();a[i].second=read();
26         if(a[i].first<0)neg++;
27     }sort(a,a+n);
28     int sol=0;
29     if((neg<<1)<n)for(int i=0;i<min((neg<<1)+1,n);i++)sol+=a[i].second;
30     else for(int i=max((neg<<1)-n-1,0);i<n;i++)sol+=a[i].second;
31     write(sol);
32     return;
33 }
34 void work(){
35     return;
36 }
37 void print(){
38     return;
39 }
40 int main(){init();work();print();return 0;}
时间: 2024-10-13 21:22:50

Codeforces Round #312 (Div. 2) A.Lala Land and Apple Trees的相关文章

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

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

Codeforces Round #312 (Div. 2) ABC题解

[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地.拉拉土地是一个非常美丽的国家,位于坐标线.拉拉土地是与著名的苹果树越来越随处可见. 拉拉土地恰好n苹果树.树数i位于位置xi和具有人工智能的苹果就可以了增长.阿姆鲁希望从苹果树收集苹果. AMR目前维持在X =0的位置.在开始的时候,他可以选择是否去左边或右边.他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观.他会采取所有的苹果,然后扭转他的方向,继续走这

Codeforces Round #312 (Div. 2)

A. Lala Land and Apple Trees 题目描述: 一条坐标轴,在坐标轴上散布了一些苹果树,每棵树都有位置和所结果实数目两个属性,Amr在坐标轴0点的位置,Amr在开始的时候可以选择向左或者右走,然后遇到果树才能改变方向,问最后最多能拿到多少个苹果? 解题思路: 开两个数组,分别代表坐标正半轴和负半轴,然后对每个数组排下一序,刚开始选择苹果树多的方向,然后累加就好. 1 #include <bits/stdc++.h> 2 using namespace std; 3 str

Codeforces 558A Lala Land and Apple Trees(Sort快排)

A. Lala Land and Apple Trees time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is

Codeforces Round #312 (Div. 2) C.Amr and Chemistry

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals

Codeforces Round #312 (Div. 2) B.Amr and The Large Array

Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some

Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals

Codeforces Round #312 (Div. 2) E. A Simple Task

题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次操作后,这x个'a'要么去最左(升序),要么去最右(降序),我们可以建立一颗线段树来维护这样的操作,字符'a'出现的位置值为1,否则为0,那么q次操作后,最后值为1的地方填的就是'a'了. 在考虑字符'a'和'b'的情况,操作的情况和上面类似,字符'a'和'b'出现的位置值为1,否则为0,q次操作后

Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序

D. Guess Your Way Out! II time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that