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 ...
随机推荐
- for循环进阶
[引例] 输出一行10个“*” #include<cstdio> int main(){ printf("**********\n"); ; } 思考: (1)输出一行 ...
- 走进科学之WAF(Web Appllication Firewall)篇
小编P.S:文章非常详尽对WAF领域进行了一次科普,能有让人快速了解当前WAF领域的相关背景及现状,推荐所有WAF领域的同学阅读本文. 1. 前言 当WEB应用越来越为丰富的同时,WEB 服务器以其强 ...
- (转)iOS消息推送机制的实现
原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...
- java equals 和hashcode
1 如果不知道怎么重载hashcode, eclipse自动代码生成工具会帮助你生成,大概的思路是设定一个int prim, 然后根据各个成员的值或者hashcode值进行某种运算即可,具体什么运 ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
- php生成图片
//生成图片,第一个参数宽,第二个高 1.$image = imagecreatetruecolor(100,25); //生成颜色,当第一次调用生成颜色的方法,是生成背景颜色 2.$backgrou ...
- Best Practice: Avoiding or minimizing synchronization in servlets
Introduction Minimize the use of synchronization in servlets. Because servlets are multi-threaded, s ...
- IOS 多线程编程之Grand Central Dispatch(GCD)介绍和使用 多线程基础和练习
介绍:前面内容源自网络 Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式 ...
- windows 命令修改IP
修改ip: netsh -c interface ip set address name="本地连接" source=static addr=192.168.11.100 mask ...
- iOS开发环境C语言基础
1 在“桌面”上创建一个文件夹Test 1.1 问题 在桌面上创建一个文件夹,我们需要了解Max OS X操作系统的操作方法.对于Max OS X操作系统,有两种方法可以在桌面上创建文件夹,一种方法是 ...