Codeforces Round #741 (Div. 2), problem: (D1) Two Hundred Twenty One (easy version), 1700
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的更多相关文章
- Codeforces Round #741 (Div. 2)部分题题解
我果然还是太菜了,就写了两道题....真是水死了.... A The Miracle and the Sleeper 简化题意:给定\(l,r\),求\(a\)%\(b\)的最大值,其中\(r> ...
- 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 ...
- Codeforces Round #741 (Div. 2)
全部题目跳转链接 A - The Miracle and the Sleeper 题意 给定\([l, r]\) 求出在这个区间内的两个数字a和b的取模的最大值 (\(a \ge b\)) 分析 上届 ...
- Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation
还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门 Problem - D - Codeforces 题意 n个数字,n ...
- Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读
http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
随机推荐
- flask 数据库一节笔记
笔记一:os.path的用法:1. os.path.dirname(__file__) 返回当前脚本的执行路径,__file__为固定参数2. os.path.abspath(file) ...
- Listener是什么?有什么作用?
Listener是指Servlet中的监听器. Listener可以对ServletContext对象.HttpSession对象.ServletRequest对象进行监听.
- 什么情况下一个 broker 会从 isr中踢出去?
leader会维护一个与其基本保持同步的Replica列表,该列表称为ISR(in-sync Replica),每个Partition都会有一个ISR,而且是由leader动态维护 ,如果一个foll ...
- Redis 的持久化机制是什么?各自的优缺点?
Redis 提供两种持久化机制 RDB 和 AOF 机制: 1.RDBRedis DataBase)持久化方式: 是指用数据集快照的方式半持久化模式) 记录 redis 数据库的所有键值对,在某个时间 ...
- @Qualifier 注解 ?
当有多个相同类型的 bean 却只有一个需要自动装配时,将@Qualifier 注解和 @Autowire 注解结合使用以消除这种混淆,指定需要装配的确切的 bean.
- MyBatis 实现一对一有几种方式?具体怎么操作的?
有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次, 通过在 resultMap 里面配置 association 节点配置一对一的类就可以完成: 嵌套查询是先查一个表,根据这个表里面的结果的 ...
- Redis 集群的主从复制模型是怎样的?
为了使在部分节点失败或者大部分节点无法通信的情况下集群仍然可用,所 以集群使用了主从复制模型,每个节点都会有 N-1 个复制品.
- springboot使用策略模式实现一个基本的促销
策略模式 定义了算法族,分别封装起来,让它们之间可以互相替换, 此模式让算法的变化独立于使用算法的客户 源码:https://github.com/youxiu326/sb_promotion.git ...
- Python中的numpy库介绍!
转自:https://blog.csdn.net/codedz/article/details/82869370 机器学习算法中大部分都是调用Numpy库来完成基础数值计算的.安装方法: pip3 i ...
- Creating a File View
创建文件视图 为了映射一个文件的数据到进程的虚拟内存,你必须创建一个文件的视图.MapViewofFile和MapViewofFileEX使用CreateFileMapping返回的句柄,在虚拟地址空 ...