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


首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原点开始走,方向自选(<- or ->),在过程中,若遇到一个权值>0的点,则将此权值计入答案,并归零。当次、此方向上的所有点均为0后,输出此时的答案。

然后,进行分析:

我们很容易想到这是一个贪心,我们将正的和负的分别存入两个数组,最初的方向为: \(zhengsum > fusum ? zheng : fu\)即正负两边那边权值 > 0的点多就先往哪个方向走,然后,就成模拟题了……

Code:

#include<bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
const int M = 100001;
int n,dir,l[M],r[M],lsum,rsum,ans;
int markjia[M] = {0},markjian[M] = {0};
int check()
{
if(dir == -1)
{
for(int i=1;i<=100000;i++)
{
if(markjian[i])return i;
}
return 0;
}else{
for(int i=1;i<=100000;i++)
{
if(markjia[i])return i;
}
return 0;
}
}
int main()
{
scanf("%d",&n);
int x,a;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&x,&a);
if(x < 0){x *= -1;l[x] = a,markjian[x] = 1,lsum++;}
else r[x] = a,markjia[x] = 1,rsum++;
}
if(lsum >= rsum)dir = -1;
else dir = 1;
for(int i=1;i<=n;i++)
{
if(check() == 0)break;
if(dir == -1)
{
ans += l[check()];
markjian[check()] = 0;
dir = 1;
}else{
ans += r[check()];
markjia[check()] = 0;
dir = -1;
}
}
cout << ans;
return 0;
}

\(End.\)

【Codeforces #312 div2 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 暴力

    A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...

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

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

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

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

  6. CF 558A(Lala Land and Apple Trees-暴力)

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

  7. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  8. 【SRM 717 DIV2 C】DerangementsDiv2

    Problem Statement You are given two ints: n and m. Let D be the number of permutations of the set {1 ...

  9. 【SRM 717 div2 B】LexmaxReplace

    Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...

随机推荐

  1. 20.java-JDBC连接mysql数据库详解

    1.JDBC介绍 jdbc(java database connectivity)为java开发者使用数据库提供了统一的编程接口,它由一组java类和接口组成. JDBC需要用到的类和接口有: Dri ...

  2. postman传递当前时间戳

    有时我们在请求接口时,需要带上当前时间戳这种动态参数,那么postman能不能自动的填充上呢. 1请求动态参数(例如时间戳) 直接在参数值写 {{$timestamp}} 如下: 我们也可以使用pos ...

  3. 2018南京现场赛K 随机输出

    题目链接:http://codeforces.com/gym/101981/attachments n和m太小,空地联通无环,总步数太大,直接随机输出5w个方向 #include<iostrea ...

  4. vue拦截器

    1.在路由添加 meta:{ requireAuth:true } 完整 { path: '/xx', name: 'xx', component: xx, meta:{ requireAuth:tr ...

  5. Djaingo 随机生成验证码(PIL)

    基础: https://www.cnblogs.com/wupeiqi/articles/5812291.html 实例: https://www.cnblogs.com/6324TV/p/88112 ...

  6. 设计模式(Java语言)- 工厂方法模式

    前言 在介绍工厂方法模式之前,我们需要知道这个设计模式是什么,解决了什么样的问题?在上一篇博客 设计模式(Java语言)- 简单工厂模式 介绍了简单工厂模式,然后总结了简单工厂模式的缺点: 1.当新增 ...

  7. visul studio 使用git扫盲帖。

    写给和一样的菜B 有必要知道的命令: git rm --cached (文章底部有git命令大全) 创建.gitignore文件 windows版: 在项目根目录下面创建gitignore.txt文件 ...

  8. Error:Cannot build artifact 'XXX:war exploded' because it is included into a circular dependency (artifact 'XXXX:war exploded', artifact 'XXX:war exploded') Idea启动项目报错解决方案

    在Idea中使用Maven创建父子工程,第一个Model的那个项目可以很好的运行,在创建一个Model运行时报这个错.原因是tomcat部署了多个Web项目,可能最开始是两个项目的配置文件混用用,最后 ...

  9. 「 从0到1学习微服务SpringCloud 」13 断路器Hystrix

    背景与功能 在微服务架构中,很多情况下,各个服务之间是相互依赖,一个服务可能会调用了好几个其他服务,假设其中有一个服务故障,便会产生级联故障,最终导致整个系统崩溃无法使用(这称为雪崩效应),Sprin ...

  10. selenium常见的元素定位方法

    一.获取元素 1)通过谷歌浏览器自动的工具访问百度首页,我们可以看到,页面上的元素都是由一行行的代码组成的,它们之间有层级地组织起来,每个元素之间都有不同的标签和值,我们可以通过这些不同的标签和值来找 ...