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?
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.
题解:正负贪心一下,注意两边相等的情况。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
void init(){
int n=read(),neg=;
pair<int,int>a[n];
for(int i=;i<n;i++){
a[i].first=read();a[i].second=read();
if(a[i].first<)neg++;
}sort(a,a+n);
int sol=;
if((neg<<)<n)for(int i=;i<min((neg<<)+,n);i++)sol+=a[i].second;
else for(int i=max((neg<<)-n-,);i<n;i++)sol+=a[i].second;
write(sol);
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}
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/cont ...
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
[CF简单介绍] 提交链接:A. Lala Land and Apple Trees 题面: A. Lala Land and Apple Trees time limit per test 1 se ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces Round #312 (Div. 2) ABC题解
[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...
- Codeforces Round #312 (Div. 2)
好吧,再一次被水题虐了. A. Lala Land and Apple Trees 敲码小技巧:故意添加两个苹果树(-1000000000, 0)和(1000000000, 0)(前者是位置,后者是价 ...
- codeforces 558A A. Lala Land and Apple Trees(水题)
题目链接: A. Lala Land and Apple Trees time limit per test 1 second memory limit per test 256 megabytes ...
- 【42.07%】【codeforces 558A】Lala Land and Apple Trees
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...
- Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力
B. Amr and The Large Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
随机推荐
- javascript中错误使用var造成undefined
在javascript中依据变量作用的范围不同分为局部变量和全局变量,直接定义的变量是全局变量,全局变量能够被全部的脚本訪问:在函数中定义的变量是局部变量,局部变量仅仅在函数内有效. 假设全局变量和局 ...
- Haskell 差点儿无痛苦上手指南
趁着自己重装Linux 虚拟机的机会,把安装 haskell 的过程记录一下,顺便帮那些还犹豫徘徊在haskell门外的读者入门. 基本概念: Haskell : 是一门通用函数式语言,差点儿能够进行 ...
- [Javascript] Logging Pretty-Printing Tabular Data to the Console
Learn how to use console.table to render arrays and objects in a tabular format for easy scanning ov ...
- Junit使用教程(一)
几乎所有程序员都听说过Junit的大名,但不知真正懂得运用它的人有多少,我便是其中的一个小白. 知道Junit是用来测试的,但却把“宝刀”当成了“菜刀”用.为了从此不再菜鸟,特此总结整理了下Junit ...
- U盘启动安装CentOS 6.3
无光驱U盘启动安装CentOS 6.3的一些必要条件: 1.主板要支持U盘启动 2.8G的U盘 3.UltraISO软件 http://www.linuxidc.com/Linux/2010-03/2 ...
- css3之 media query 使用(转)
原文链接:http://www.moke8.com/article-5657-1.html 讲到响应式布局, 相信大家都有一定的了解,响应式布局是今年很流行的一个设计理念,随着移动互联网的盛行,为解决 ...
- navicat导入mysql数据库sql时报错或数据不完全问题
错误详情:[Err] [Imp] 2006 - MySQL server has gone away 或无提示错误,但是导入数据明显缺少字段和数据 找到服务器上的MYSQL安装目录下的my.ini文件 ...
- 连接管理VMware SphereESXi
连接管理VMware SphereESXi 1. 准备 下载VMware-viclient-all-5.5.0-1993072,并按照提示安装 2. 使用VMware Sphere Client链接事 ...
- Decorator Wrapper 装饰模式 包装
简介 装饰模式 装饰模式以对客户端[透明]的方式[扩展]对象的功能,客户端并不会觉得对象在装饰前和装饰后有什么不同,是继承关系的一个替代方案. 若只为增加功能而使用继承,当基类较多时会导致继承体系越来 ...
- JS正则表达式验证账号、手机号、电话、邮箱、货币
验证帐号是否合法验证规则:字母.数字.下划线组成,字母开头,4-16位. function checkUser(str){ var re = /^[a-zA-z]\w{3,15}$/; if(re.t ...