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 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

Sample Output

10

HINT

题意

有一个人,一开始会往左边走或者往右边走,碰到苹果树就会拾起这棵苹果树的所有果子,然后再交换方向,重复这个过程

问你这个人最多能拿多少个果子

题解:

先左右按着坐标排一个序,很显然只有两种走法,挨个判一下谁最大就好了

代码

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std; struct node
{
int x,y;
};
node a[];
node b[];
bool cmp(node aa,node bb)
{
return aa.x<bb.x;
}
int main()
{
int n;
scanf("%d",&n);
int num1=,num2=;
for(int i=;i<n;i++)
{
int c,d;
cin>>c>>d;
if(c<)
a[num1].x=-c,a[num1++].y=d;
else
b[num2].x=c,b[num2++].y=d;
}
sort(a,a+num1,cmp);
sort(b,b+num2,cmp);
int num=min(num1,num2);
int ans=;
for(int i=;i<num;i++)
ans+=(a[i].y+b[i].y);
if(num1>num2)
ans+=a[num2].y;
else if(num2>num1)
ans+=b[num1].y;
cout<<ans<<endl; }

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

  1. 【打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 ...

  2. 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. ...

  3. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  4. 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 ...

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

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  6. Codeforces Round #312 (Div. 2)

    好吧,再一次被水题虐了. A. Lala Land and Apple Trees 敲码小技巧:故意添加两个苹果树(-1000000000, 0)和(1000000000, 0)(前者是位置,后者是价 ...

  7. 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 ...

  8. 【42.07%】【codeforces 558A】Lala Land and Apple Trees

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 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/ ...

随机推荐

  1. Cent OS5.2安装Hyper-V集成光盘

    一.Hyper-V安装windows系统没有问题,windows2000以后系统都可以,一切顺利. 驱动程序:IDE.SCSI.网络.视频和鼠标 要想实现更强的功能,宿主机需要安装Hyper-V集成光 ...

  2. Delphi VclSkin使用教程

    1. TSkinData   TSkinData 主要用于美化你的程序, 只要把TSkinData控件放下去,它就能自动美化所有窗体. 属性 Active: 使用或取消对程序的美化. DisableT ...

  3. 获取当前的 viewController

    - (UIViewController *)currentController {    UIViewController *result = nil;    UIWindow *window = [ ...

  4. php sortable 动态排序

    php sortable 动态排序未分页版.php 预览图: <?php mysql_connect("localhost","root","r ...

  5. js 判断输入是否为正整数

    javascript代码如下: var re = new RegExp("^[1-9][0-9]*$"); if (re.test("11k")) { cons ...

  6. SQL查询数据库信息, 数据库表名, 数据库表信息

    SQL查询数据库信息, 数据库表名, 数据库表信息 ---------------------------------------------- -- 以下例子, 在sql_server 中可以直接运 ...

  7. grails下的httpclient

    httpclient已经发布到4.3版本了,其中的API发生了巨大的变化,尤其是对于关闭连接显得更加容易理解,比如引入了CloseableHttpResponse. 然而grails2.3.2中自带了 ...

  8. 【LeetCode】70 - Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. ASP.NET MVC Razor视图引擎攻略

    --引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...

  10. 使用jqueryui

    $(function () { $('#search_button').button(); /* 同时打开两个对话框 $('#reg').dialog(); $('#login').dialog(); ...