【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. 原生javascript 基础动画函数封装(一)

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...

  2. 生成TFRecord文件完整代码实例

    import os import json def get_annotation_dict(input_folder_path, word2number_dict): label_dict = {} ...

  3. Git高级之配置多个SSH key

    最近我们在代码托管平台上使用SSH的方式下拉代码,通常是用一个ssh key来拉取所有托管平台的代码,如码云,GitHub.GitLab等,但是总用一个不是太好.会有安全风险,这就需要为每个托管平台设 ...

  4. C# 根据年月日计算周次

    //day:要判断的日期,WeekStart:1 周一为一周的开始, 2 周日为一周的开始 public static int WeekOfMonth(DateTime day, int WeekSt ...

  5. Django SQLite3的使用

    https://blog.csdn.net/qq_34485436/article/details/72805908

  6. pywin32 获取 windows 的窗体内文本框的内容

    用 spy++去确认找到了文本框的句柄了. 用函数 win32gui.SendMessage 获取不了文本框的文本内容,用 str 类型的参数接收获取的内容的话没有获取到东西,而用 PyBuffer ...

  7. qt中的拖拽及其使用技巧

    关于qt中的拖放操作,首先可以看这篇官方文档:http://doc.qt.io/qt-5.5/dnd.html 一.QDrag 首先是创建QDrag,可以在mousePressEvent或者mouse ...

  8. Python中类属性和实例属性的区别

    在Python中经常会混淆类属性和实例属性的概念,今天专门记录一下个人理解以免日后忘记. 看下面的例子: class Tencent(): i = 10 # 此处i为类属性 def __init__( ...

  9. 深入Node模块Buffer-学会操作二进制

    Buffer 作为 nodejs 中重要的概念和功能,为开发者提供了操作二进制的能力.本文记录了几个问题,来加深对 Buffer 的理解和使用: 认识缓冲器 如何申请堆外内存 如何计算字节长度 如何计 ...

  10. Zabbix添加SNMP自定义监控项OID出现“No Such Instance currently exists at this OID”

    原因:zabbix 是用snmpget来获取指定的OID数据,snmpwalk是遍历某个OID下的数据. 所以一定要用snmpget来验证某个OID是否正确. snmptranslate 获取的OID ...