【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
提交链接:A. Lala Land and Apple Trees
题面:
1 second
256 megabytes
standard input
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?
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 the maximum number of apples Amr can collect.
2
-1 5
1 5
10
3
-2 2
1 4
-1 3
9
3
1 9
3 5
7 10
9
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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
using namespace std;
struct apple_Tree
{
int amount,pos;
}store[100010];
bool cmp(apple_Tree a,apple_Tree b)
{
return a.pos<b.pos;
}
int main()
{
int n,cntminus=0,cntzheng=0;
long long ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&store[i].pos,&store[i].amount);
if(store[i].pos<0)cntminus++;
else cntzheng++;
}
if(cntminus==cntzheng)
{
for(int i=0;i<n;i++)
ans+=store[i].amount;
cout<<ans<<endl;
}
else
{
sort(store,store+n,cmp);
if(cntminus<cntzheng)
{
for(int i=0;i<2*cntminus+1;i++)
{
ans+=store[i].amount;
}
}
else
{
for(int j=n-1;j>=(n-2*cntzheng-1);j--)
ans+=store[j].amount;
}
cout<<ans<<endl;
}
return 0;
}
【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees的更多相关文章
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力
A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...
- 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. ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 【cf比赛记录】Codeforces Round #601 (Div. 2)
Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...
- 【cf比赛记录】Codeforces Round #600 (Div. 2)
Codeforces Round #600 (Div. 2) ---- 比赛传送门 昨晚成绩还好,AC A,B题,还能上分(到底有多菜) 补了C.D题,因为昨晚对C.D题已经有想法了,所以补起题来也快 ...
- 【42.07%】【codeforces 558A】Lala Land and Apple Trees
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...
- 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/ ...
- 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 ...
随机推荐
- [ Luogu 1273 ] 有线电视网
\(\\\) \(Description\) 一棵\(N\)个节点的树,编号在\([N-M+1,N]\)内的点必定为叶子节点,且这些点都有一个收益值\(Val_i\),同时每一条树边都有一个代价. 访 ...
- 图标文件ico制作以及使用说明
今天说一个图标文件——ico.我们在pc端浏览网页的时候网页栏那块都会显示一个本网站特有的图片,就是我们说的ico了.示例:<link href="image/favicon.ico& ...
- Java常见问题总结(二)
1.配置完Java环境变量之后,仍然不能使用java命令. 解决方法: 如果是Windows10系统出现此问题,是因为个别Windows10系统不识别“JAVA_HOME”环境变量,将path中所有的 ...
- 获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据
获取WebBrowser全cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true) ...
- 解决vue项目运行过程中,npm run dev 报错问题
[方案1] 错误如下: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! travel@1.0.0 dev: `webpack-dev-server ...
- ThinkPHP---thinkphp框架介绍
目录: (1)简述: (2)下载: (3)文件结构: (4)部署: (5)细节问题: 主体: (1)简述 ThinkPHP诞生于2006年初,最初叫FSC.于2007年元旦更名为PHP,同时官网上线. ...
- 数据库——DBUtils和连接池
第一章 DBUtils如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils.DBUtils就是JDBC的简 ...
- 2018最新Python小白入门教程,30天学会Python
随着Python的技术的流行,Python在为人们带来工作与生活上带来了很多的便捷,因为Python简单,学起来快,也是不少新手程序员入门的首选语言.作为一名Python爱好者,我也想跟大家分享分享我 ...
- 参考整理papers(一)
https://blog.csdn.net/qq_14845119/article/details/82219246 整理了OCR的论文,可以参考一下.还有一些相关论文 论文(poster):Scen ...
- 框架之---Flask
Flask是一个轻量级的WEB框架,它和Django相比,就是一个胖子和一个骨架的区别. Flask是一个py文件就可以run的web框架,但是就因为是py文件就能run,所有,啥都没有,等把Flas ...