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. 项目构建工具之maven01

    Maven 是一个项目管理工具,可以对 Java 项目进行构建.依赖管理.Maven 也可被用于构建和管理各种项目,例如 C#,Ruby,Scala 和其他语言编写的项目.Maven 曾是 Jakar ...

  2. (web)Bugs_Bunny_CTF_writeup 部分简单web

    Nothing here QnVnc19CdW5ueXs1MjljNDI5YWJkZTIxNzFkMGEyNTU4NDQ3MmFmODIxN30K Bugs_Bunny{529c429abde2171 ...

  3. Python 的排序方法 sort 和 sorted 的区别

    使用 sort() 或内建函数 sorted() 对列表进行排序.它们之间的区别有两点: sort() 方法是对原列表进行操作,而 sorted() 方法会返回一个新列表,不是在原来的基础上进行操作. ...

  4. Key-Value存储系统简介

    Redis是一个Key-Value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类 ...

  5. Apache Tomcat如何高并发处理请求

    介绍 作为常用的http协议服务器,tomcat应用非常广泛.tomcat也是遵循Servelt协议的,Servelt协议可以让服务器与真实服务逻辑代码进行解耦.各自只需要关注Servlet协议即可. ...

  6. JDK8新特性关于Stream流

    在Java1.8之前还没有stream流式算法的时候,我们要是在一个放有多个User对象的list集合中,将每个User对象的主键ID取出,组合成一个新的集合,首先想到的肯定是遍历,如下: 1 2 3 ...

  7. 详细描述一下 Elasticsearch 搜索的过程?

    想了解 ES 搜索的底层原理,不再只关注业务层面了. 解答: 搜索拆解为"query then fetch" 两个阶段. query 阶段的目的:定位到位置,但不取. 步骤拆解如下 ...

  8. SpringBoot bean映射yml中的属性举例

    pom:导入配置文件处理器,配置文件进行绑定就会有提示 <dependency> <groupId>org.springframework.boot</groupId&g ...

  9. Shiro Session放到Redis中常遇到的问题

    Shiro会话管理:https://shiro.apache.org/session-management.html#SessionManagement-CustomSessionIDs Redis主 ...

  10. probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address ...

    probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address ...