博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
阅读量:6835 次
发布时间:2019-06-26

本文共 2723 字,大约阅读时间需要 9 分钟。

 

提交链接:

题面:

A. Lala Land and Apple Trees
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 numberi 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 inx = 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 eachxi,ai ( - 105 ≤ xi ≤ 105,xi ≠ 0,1 ≤ ai ≤ 105), representing the position of thei-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 point0.

Output

Output the maximum number of apples Amr can collect.

Sample test(s)
Input
2-1 51 5
Output
10
Input
3-2 21 4-1 3
Output
9
Input
31 93 57 10
Output
9
Note

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 tox = 1, collect apples from there, then the direction will be reversed and Amr goes to the final treex =  - 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.

解题: 假设正负位置数量同样。那么都可取,假设不同样,那么少的一边全取。多的一边取离0近的(少的数量+1个)。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;struct apple_Tree{ int amount,pos;}store[100010];bool cmp(apple_Tree a,apple_Tree b){ return a.pos
=(n-2*cntzheng-1);j--) ans+=store[j].amount; } cout<
<

转载于:https://www.cnblogs.com/gavanwanggw/p/6856210.html

你可能感兴趣的文章
Oracle 数据库范式
查看>>
[设计模式]<<设计模式之禅>>模板方法模式
查看>>
281. Zigzag Iterator - Medium
查看>>
205. Isomorphic Strings - Easy
查看>>
去除Android打开软件出现的红边框
查看>>
Ubuntu 12.04.1 OK335xS busybox-1.24.1 文件系统编译错误及解决方案
查看>>
一次优秀的代码提交应该包含什么?
查看>>
CF1036C Classy Numbers dfs+二分
查看>>
linux管理和进程(4)
查看>>
公钥与私钥,HTTPS详解 转载
查看>>
构建之法阅读笔记(3)
查看>>
UVA 10269 Adventure of Super Mario 最短路
查看>>
mysql having,group by查询去除重复记录
查看>>
PHP正则表达式 /i, /is, /s, /isU等
查看>>
羊车门问题
查看>>
【HNOI】 小A的树 tree-dp
查看>>
聊天室--java socket
查看>>
iOS 之 Swift 新特性
查看>>
j2me必备之网络开发数据处理
查看>>
关于C++的递归调用(n的阶乘为例)
查看>>