Problem - D1 - Codeforces

题意:

给n个符号(+或-), +代表+1, -代表-1, 求最少删去几个点, 使得

 

 

题解(仅此个人理解):

1. 这题打眼一看, 肯定和奇偶有关系, 奇数为+, 偶数为-, 但是删去点这一操作是动态的, 删去某点后, 后面的点的正负随之颠倒, 即奇数位+变偶数位-, 偶数位-变奇数位+, 恰好可以利用该特质

也就是说可以这样理解: 找到一个位置,删去该点并使得后面的点正负颠倒, 最后满足条件.

2. 对于奇数个数, 必须变成偶数个个数才可能满足条件, 它一定存在个地方, 删去该点并且后面的点正负颠倒后满足条件, 结果为1

3. 对于偶数个数, 如果已经满足条件, 则输出0,

否,则先变成奇数个数再操作(和上面一样), 结果为2

AC代码

#include<iostream>
#include<string>
#include <cmath>
#include <algorithm> using namespace std;
const int N = 3e5+10;
int num[N]; void solve()
{
int n, q;
string s;
cin >> n >> q >>s;
for(int i = 0; i <= n; i ++)
// num[i+1] = num[i] + ((i&1)?-1:1) * ((s[i]=='+')?1:-1);
if(i%2==0&&s[i]=='+' || i%2==1&& s[i]=='-')num[i+1]=num[i]+1;
else num[i+1] = num[i]-1; while(q --)
{
int l, r;
cin >> l >> r;
if((r-l+1)&1)
{
puts("1");
continue;
}
if((num[r]-num[l-1])==0)puts("0");
else puts("2");
} return;
}
int main()
{
int t;
cin >> t;
while(t --)
solve(); return 0;
}

Codeforces Round #741 (Div. 2), problem: (D1) Two Hundred Twenty One (easy version), 1700的更多相关文章

  1. Codeforces Round #741 (Div. 2)部分题题解

    我果然还是太菜了,就写了两道题....真是水死了.... A The Miracle and the Sleeper 简化题意:给定\(l,r\),求\(a\)%\(b\)的最大值,其中\(r> ...

  2. Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维

    & -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...

  3. Codeforces Round #741 (Div. 2)

    全部题目跳转链接 A - The Miracle and the Sleeper 题意 给定\([l, r]\) 求出在这个区间内的两个数字a和b的取模的最大值 (\(a \ge b\)) 分析 上届 ...

  4. Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation

    还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门  Problem - D - Codeforces 题意 n个数字,n ...

  5. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读

    http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...

  6. Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分

    Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...

  7. Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学

    — This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...

  8. Codeforces Round #439 (Div. 2) Problem B (Codeforces 869B)

    Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...

  9. Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力

    Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...

随机推荐

  1. 在微信小程序中使用 echarts 图片-例 折线图

    首先进入echarts官方[https://echarts.apache.org/handbook/zh/get-started/].这边只需要在小程序中简单应用一下echarts折线图 所以不需要把 ...

  2. python中的super是什么?

    技术背景 python中的super,名为超类,可以简单的理解为执行父类的__init__函数.由于在python中不论是一对一的继承,还是一子类继承多个父类,都会涉及到执行的先后顺序的问题.那么本文 ...

  3. Mybatis——xml配置

    注:该文档参考了 https://mybatis.org/mybatis-3/zh/configuration.html 狂神的视频:https://www.bilibili.com/video/BV ...

  4. CVE-2017-12615漏洞复现附EXP

    CVE-2017-12615复现 0x00 漏洞介绍 漏洞编号: CVE-2017-12615 CVE-2017-12616 漏洞名称: CVE-2017-12615-远程代码执行漏洞 CVE-201 ...

  5. 使用 Mosh 来优化 SSH 连接

    1.什么是Mosh Mosh表示移动Shell(Mobile Shell),是一个用于从客户端跨互联网连接远程服务器的命令行工具.它能用于SSH连接,但是比Secure Shell功能更多.它是一个类 ...

  6. JavaScript 02 运算符,分支结构

    一元换算符 字符串类型 对于字符串,来说自增或自减会自动进行类型转换 1.var str = '10' 自动转换 从string转为number 2.str++ NaN 对于转化不成功的string类 ...

  7. redis之 主从复制和哨兵(一)

    一.Redis主从复制 主从复制:主节点负责写数据,从节点负责读数据,主节点定期把数据同步到从节点保证数据的一致性 1. 主从复制的相关操作 a,配置主从复制方式一.新增redis6380.conf, ...

  8. 渲染一个react?

    分为首次渲染和更新渲染 生命周期, 建立虚拟DOM, 进行diff算法 对比新旧DOM, 节点对比, 将算法复杂度从O(n^3)降低到O(n) key值优化, 避免用index作为key值, 兄弟节点 ...

  9. SpringBoot 上传文件大小限制,SizeLimitExceededException: the request was rejected because its size (64042302) exceeds the configured maximum (10485760)

    对应的配置属性文件:org.springframework.boot.autoconfigure.web.servlet.MultipartProperties 由于我是yml文件,所以直接这样定义就 ...

  10. Linux下离线安装docker与fastDFS

    一.Linux下离线安装Docker 基础环境 1.操作系统:CentOS 7 2.Docker版本:docker-19.03.9.tgz 官方下载地址(打不开可能需要科学-上网) 3.官方参考文档: ...