CodeForces 558A
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
10
3
-2 2
1 4
-1 3
9
3
1 9
3 5
7 10
9 题意:每一个节点都有两部分数据组成,第一部分是该节点在一维坐标轴上的位置,第二部分是它的权值。以原点x=0为界,分成正负两部分.从原点开始,要按“折返跑”的形式
收集权值,直到一边没值可收时结束,问能收集的最大值。 思路:只有第一次向负方向收集和第一次向正方向收集这两种方案。当正负两方向的节点个数相同时,答案明显,就是总和;不相同时,应第一次向节点个数多的方向收集,结果就是答案。 代码如下:
# include<iostream>
# include<cstdio>
# include<map>
# include<set>
# include<cstring>
using namespace std;
struct node
{
int x,v;
bool operator < (const node &a) const {
return x<a.x;
}
};
set<node>s;
int ss[105];
int main()
{
//freopen("A.txt","r",stdin);
int n,c1,c2;
while(~scanf("%d",&n))
{
s.clear();
int i,sum=0;
c1=c2=0;
node t;
for(i=0;i<n;++i){
scanf("%d%d",&t.x,&t.v);
s.insert(t);
if(t.x<0)
++c1;
else
++c2;
}
set<node>::iterator it;
ss[0]=0;
int cnt=1;
for(it=s.begin();it!=s.end();++it)
ss[cnt++]=it->v;
for(i=1;i<cnt;++i)
ss[i]+=ss[i-1];
if(c1>c2){
printf("%d\n",ss[cnt-1]-ss[cnt-2*c2-2]);
}else if(c1==c2)
printf("%d\n",ss[cnt-1]);
else
printf("%d\n",ss[2*c1+1]-ss[0]);
}
return 0;
}
CodeForces 558A的更多相关文章
- 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 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- my class 2.0
www.dropbox.com www.google.com/voice www.prezi.com www.evernote.com
- 如何利用SVN合并代码
一. 背景 平时在进行开发时,一般都会有多版本同时进行,包括项目版本.周版本.紧急版本等,当某一个版本具备上线条件后,需要在上一个已发布的版本基础上进行发布,才能够避免出现版本相互覆盖,因此 ...
- 推荐可以代替Visio的HTML开发的作图工具:ProcessOn
过去作图的时候一直都是在用visio,每一次换了电脑使用都要重新安装,这大家都知道,最头疼的就是激活问题,曾经因为激活问题我“找遍了”正个互联网,最后还没找到...从08年开始到现在,visio用了这 ...
- RPI学习--环境搭建_串口连接
有两种, 一种是通过MAX2323芯片连接的串口,要接VCC为芯片供电. 另一种是通过PL2302芯片连接的USB,可不接VCC,用电脑USB口为芯片供电. 下面以通过MAX2323方式为例. 1,V ...
- 如何通过web查看job的运行情况
当我们将作业提交到hadoop 的集群上之后,我们会发现一个问题就是无法通过web查看job运行情况,比如启动了多少个map任务,启动多少个reduce任务啊,分配多少个conbiner等等.这些信息 ...
- android自学笔记一
android是什么我自闭不必多说,我们挑精华整理 一.android体系架构: android从下而上分为四层: (1)分别是linux操作系统及驱动(C语言实现) (2)本地代码(C/C++)框架 ...
- Android Context
http://www.cnblogs.com/android100/p/Android-Context.html
- Android重写getResources规避用户调整系统字体大小影响Android屏幕适配
Android屏幕适配一直是一个头疼的问题.除此之外还要考虑APP在实际应用场景中,用户千奇百怪的设置,最常见的用户设置行为就是设置手机的字体大小,比如把字体设置成超大或者超小,这对屏幕适配又带来额外 ...
- 30道四则运算<2>单元测试
该测试未实现除法 该测试中间多了/)两个符号,而且没有等号和回车. 该测试也没有符合除法要求 该测试也没有满足除法要求 该测试满足要求. 总结:程序中涉及到有除法的输出都有问题,多次改正未果:其他条件 ...
- cocos2dx 搭建 android 平台
Mac OS X下配置Cocos2d-x for Android(Eclipse)&IOS(Xcode)开发环境 前面一段时间只用Cocos2d-x在IOS平台下开发, 学习Cocos2d-x ...