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. Java基础——StringBuilder

    /* StringBuilder作用: String 在new一个新的对象以及进行字符串拼接时,会在常量池创建拼接之前的两个字符串对象,拼接完成后会造成内存浪费,时间浪费 StringBuilder ...

  2. Termux镜像在阿里云镜像站首发上线

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 简介 Termux 是 Android 平台上的一个终端模拟器,它将众多 Linux 上运行的软件和工具近乎完美的移植到了手机端. 无需任何复杂的安 ...

  3. 使用 Docker 部署 LNMP 并搭建 wordpress

    准备 系统版本:CentOS Linux release 7.4.1708 (Core)   内核版本:3.10.0-693.el7.x86_64    IP:192.168.31.43    可访问 ...

  4. 九鼎RK3399笔记一:Linux平台手册

    @ 目录 一.git下载九鼎SDK 二.安装所需的软件包: 三.安装 kernel 及 u-boot 编译需要依赖的软件包 四.安装文件系统需要依赖的软件包 五.安装 Buildroot 编译需要依赖 ...

  5. python 命令行参数学习(二)

    照着例子看看打打,码了就会.写了个命令行参数调用进行运算的脚本. 参考文章链接:http://www.jianshu.com/p/a50aead61319 #-*-coding:utf-8-*- __ ...

  6. 论文解读(GMI)《Graph Representation Learning via Graphical Mutual Information Maximization》

    Paper Information 论文作者:Zhen Peng.Wenbing Huang.Minnan Luo.Q. Zheng.Yu Rong.Tingyang Xu.Junzhou Huang ...

  7. 通过Kuberneters Goat学习K8S安全(上)

    实验环境:https://katacoda.com/madhuakula/scenarios/kubernetes-goat 0x1.敏感信息泄露利用 第一关是代码泄露利用,打开网站后显示: 告诉我们 ...

  8. MySQL中的约束,添加约束,删除约束,以及其他修饰

    一.NOT NULL(非空约束)添加非空约束 1)建表时直接添加 CREATE TABLE t_user(user_id INT(10) NOT NULL); 2)通过ALTER 语句 ALTER T ...

  9. 为什么 wait(), notify()和 notifyAll ()必须在同步方法或 者同步块中被调用?

    当一个线程需要调用对象的 wait()方法的时候,这个线程必须拥有该对象的锁,接 着它就会释放这个对象锁并进入等待状态直到其他线程调用这个对象上的 notify() 方法.同样的,当一个线程需要调用对 ...

  10. 在虚拟机里面安装mysql

    https://dev.mysql.com/downloads/repo/yum/ 首先到网站里面下载 mysql80-community-release-el7-3.noarch.rpm 通过xft ...