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. CMDB反思4

    CMDB模型设计2 http://blog.vsharing.com/xqscool/A1275233.html 估计大家看到破子的这两篇都有点晕哈,我也有点晕. 两篇对比来看.   第1处,属性部分 ...

  2. saltstack配置安装的一些关键步骤及安装时各种报错的分析

    以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...

  3. 约瑟夫环 --- 面向对象 --- java代码

    约瑟夫环 的 面向对象 解法 罗马人占领乔塔帕特后,39个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个 ...

  4. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

  5. 常用SQL整理

    整理了日常用到的一些sqls 1.插入表 insert into table_B select * from table_A 2.清空表 truncate table test #清空表,结构还存在d ...

  6. linux c 多线程编程

    linux 下 c 语言多线程: /* 06.3.6 Mhello1.c Hello,world -- Multile Thread */ #include<stdio.h> #inclu ...

  7. Ubuntu上安装zsh

    先安装zsh,同时移除之前可能的oh-my-zsh的安装 sudo apt-get install zsh if [ -d ~/.oh-my-zsh ]; then rm -r ~/.oh-my-zs ...

  8. [WebService]之TCPMon的使用

    TCPMon是apache下的一个项目,下载地址:http://ws.apache.org/commons/tcpmon/download.cgi (1)功能: TCPMon可以拦截客户与服务之间的H ...

  9. Compiling Xen-4.4 From Source And Installing It On Ubuntu Server (Amd-64)

    First of all, you should install a clean Ubuntu Server (Amd-64) on your server. (Version 14.04 is st ...

  10. jszs 对象引用

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...