Codeforces Round #590 (Div. 3)订正(待更E和Fde代码)

CF1234C Pipes

题目描述
You are given a system of pipes. It consists of two rows, each row consists of n n n pipes. The top left pipe has the coordinates ( 1 , 1 ) (1, 1) (1,1) and the bottom right — ( 2 , n ) (2, n) (2,n) .
There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types:
Types of pipes You can turn each of the given pipes 90 90 90 degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types 1 1 1 and 2 2 2 can become each other and types 3 , 4 , 5 , 6 3, 4, 5, 6 3,4,5,6 can become each other).
You want to turn some pipes in a way that the water flow can start at ( 1 , 0 ) (1, 0) (1,0) (to the left of the top left pipe), move to the pipe at ( 1 , 1 ) (1, 1) (1,1) , flow somehow by connected pipes to the pipe at ( 2 , n ) (2, n) (2,n) and flow right to ( 2 , n + 1 ) (2, n + 1) (2,n+1) .
Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes: Examples of connected pipes Let’s describe the problem using some example: The first example input And its solution is below: The first example answer As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at ( 1 , 2 ) (1, 2) (1,2) 90 90 90 degrees clockwise, the pipe at ( 2 , 3 ) (2, 3) (2,3) 90 90 90 degrees, the pipe at ( 1 , 6 ) (1, 6) (1,6) 90 90 90 degrees, the pipe at ( 1 , 7 ) (1, 7) (1,7) 180 180 180 degrees and the pipe at ( 2 , 7 ) (2, 7) (2,7) 180 180 180 degrees. Then the flow of water can reach ( 2 , n + 1 ) (2, n + 1) (2,n+1) from ( 1 , 0 ) (1, 0) (1,0) .
You have to answer q q q independent queries.
输入输出格式
输入格式
The first line of the input contains one integer q q q ( 1 ≤ q ≤ 1 0 4 1 \le q \le 10^4 1q104 ) — the number of queries. Then q q q queries follow.
Each query consists of exactly three lines. The first line of the query contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105 ) — the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of n n n digits from 1 1 1 to 6 6 6 without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes.
It is guaranteed that the sum of $ n $ over all queries does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 .
输出格式
For the i i i -th query print the answer for it — “YES” (without quotes) if it is possible to turn some pipes in a way that the water flow can reach ( 2 , n + 1 ) (2, n + 1) (2,n+1) from ( 1 , 0 ) (1, 0) (1,0) , and “NO” otherwise.
输入输出样例
输入样例 #1
6
7
2323216
1615124
1
3
4
2
13
24
2
12
34
3
536
345
2
46
54
输出样例 #1
YES
YES
YES
NO
YES
NO
说明
The first query from the example is described in the problem statement.

(自己的自己的!)题目翻译:有六种标号为1 ~ 6的管道(如上图1),1 ~ 2可以相互转化,3 ~ 4之间可以相互转化,当两个管道的末段相连时,这两个管道可以相互到达(如上图2),现在从(1,0)出发,问经过若干管道是否可以到达(2,n+1)。
数据有Q组,每组数据由三行组成,第一行是有n列管道,之后的两行是管道的标号。


心路历程

一开始看到这道题的时候,就感觉它是一道模拟题,但是我完全没有思路……
之后,就开始考虑什么时候这条管道是不合法的,我考虑到了转行应该是有奇数个的,最后一列的两个数字应该是在3~4之间的,……但是,这些考虑有的是对的,有的是对的,反正都是不全面的……所以,我就gg了。

最后,我看了题解:题解上考虑的不合法的情况,不多,但是很准确。

  1. 最后所在的行数一定要是第二行;
  2. 当中间有3 ~ 4和1 ~ 2的处于同一列的情况就是不合法的。

思考一下,为什么这样子做是对的?

  • 第一条是因为最后的终点规定的是(2,n+1),所以,最后一定要是从第二行出来的。

  • 第二条是因为当遇到3 ~ 4的时候,是一定是需要换行的(自行思考),那么此时若是遇到1~2的话,为了承接换行,1 ~ 2一定是竖着的状态,那么最后水就会流出在两行之外,所以,这种情况是不合法的。


CF1234D Distinct Characters Queries

You are given a string s s s consisting of lowercase(小写字母) Latin letters and q q q queries for this string.
Recall that the substring(截取子串) s [ l , r ] s[l, r] s[l,r] of the string s s s is the string s l s l + 1 … s r s_l s_{l + 1} \dots s_r slsl+1sr . For example, the substrings of “codeforces” are “code”, “force”, “f”, “for”, but not “coder” and “top”.
There are two types of queries;

  • 1   p o s   c 1~pos~c 1 pos c ( 1 ≤ p o s ≤ ∣ s ∣ 1 \le pos \le |s| 1poss , c c c is lowercase Latin letter): replace s p o s s_{pos} spos with c c c (set s p o s = c s_{pos} = c spos=c );
  • 2   l   r 2~ l~ r 2 l r ( 1 ≤ l ≤ r ≤ ∣ s ∣ 1 \le l \le r \le |s| 1lrs ): calculate the number of distinct characters in the substring s [ l , r ] s[l, r] s[l,r] .

输入输出格式
输入格式
The first line of the input contains one string s s s consisting of no more than 1 0 5 10^5 105 lowercase Latin letters.
The second line of the input contains one integer q q q ( 1 ≤ q ≤ 1 0 5 1 \le q \le 10^5 1q105 ) — the number of queries.
The next q q q lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.
输出格式
For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.
输入输出样例
输入样例 #1
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7
输出样例 #1
3
1
2
输入样例 #2
dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11
输出样例 #2
5
2
5
2
6

题目翻译:
给定一个字符串s,有q次操作。

  • 1   p o s   c 1~pos~c 1 pos c替换字符操作。 pos是位置,c是把 s p o s s_{pos} spos替换掉的字符;
  • 2   l   r 2~ l~ r 2 l r ( 1 ≤ l ≤ r ≤ ∣ s ∣ 1 \le l \le r \le |s| 1lrs )查询 s l s_{l} sl s r s_{r} sr中有多少不同的字符的个数。

心路历程

在看这道题之前,我的亲爱的同学就已经告诉我了,这是一道树状数组……
在这里插入图片描述为什么在别人没有看题的时候,就要说出答案……好吧,还是我太菜了

之后,我就询问我的同学,为什么这是一道树状数组的基操(基础操作 )?

同学如是说:看到题之后是单点修改,还有区间查询……那能实现这些操作的不就是树状数组和线段树……,线段树不好敲,那不就是树状数组了……

确实,这道题是可以用树状数组实现的。

正解:对于每一个字母,我们都可以建一棵树状数组维护该字母在区间上的值(也就是说,我们建了26棵树状数组),之后在实现单点修改和区间查询的操作。


CF1234E Special Permutations

题目描述
Let’s define p i ( n ) p_i(n) pi(n) as the following permutation: [ i , 1 , 2 , … , i − 1 , i + 1 , … , n ] [i, 1, 2, \dots, i - 1, i + 1, \dots, n] [i,1,2,,i1,i+1,,n] . This means that the i i i -th permutation is almost identity (i.e. which maps every element to itself) permutation but the element i i i is on the first position. Examples:

  • p 1 ( 4 ) = [ 1 , 2 , 3 , 4 ] p_1(4) = [1, 2, 3, 4] p1(4)=[1,2,3,4] ;
  • p 2 ( 4 ) = [ 2 , 1 , 3 , 4 ] p_2(4) = [2, 1, 3, 4] p2(4)=[2,1,3,4] ;
  • p 3 ( 4 ) = [ 3 , 1 , 2 , 4 ] p_3(4) = [3, 1, 2, 4] p3(4)=[3,1,2,4] ;
  • p 4 ( 4 ) = [ 4 , 1 , 2 , 3 ] p_4(4) = [4, 1, 2, 3] p4(4)=[4,1,2,3] .

You are given an array x 1 , x 2 , … , x m x_1, x_2, \dots, x_m x1,x2,,xm ( 1 ≤ x i ≤ n 1 \le x_i \le n 1xin ).
Let p o s ( p , v a l ) pos(p, val) pos(p,val) be the position of the element v a l val val in p p p . So, p o s ( p 1 ( 4 ) , 3 ) = 3 , p o s ( p 2 ( 4 ) , 2 ) = 1 , p o s ( p 4 ( 4 ) , 4 ) = 1 pos(p_1(4), 3) = 3, pos(p_2(4), 2) = 1, pos(p_4(4), 4) = 1 pos(p1(4),3)=3,pos(p2(4),2)=1,pos(p4(4),4)=1 .
Let’s define a function f ( p ) = ∑ i = 1 m − 1 ∣ p o s ( p , x i ) − p o s ( p , x i + 1 ) ∣ f(p) = \sum\limits_{i=1}^{m - 1} |pos(p, x_i) - pos(p, x_{i + 1})| f(p)=i=1m1pos(p,xi)pos(p,xi+1) , where ∣ v a l ∣ |val| val is the absolute value of v a l val val . This function means the sum of distances between adjacent elements of x x x in p p p .
Your task is to calculate f ( p 1 ( n ) ) , f ( p 2 ( n ) ) , … , f ( p n ( n ) ) f(p_1(n)), f(p_2(n)), \dots, f(p_n(n)) f(p1(n)),f(p2(n)),,f(pn(n)) .
输入输出格式
输入格式
The first line of the input contains two integers n n n and m m m ( 2 ≤ n , m ≤ 2 ⋅ 1 0 5 2 \le n, m \le 2 \cdot 10^5 2n,m2105 ) — the number of elements in each permutation and the number of elements in x x x .
The second line of the input contains m m m integers ( m m m , not n n n ) x 1 , x 2 , … , x m x_1, x_2, \dots, x_m x1,x2,,xm ( 1 ≤ x i ≤ n 1 \le x_i \le n 1xin ), where x i x_i xi is the i i i -th element of x x x . Elements of x x x can repeat and appear in arbitrary order.
输出格式
Print n n n integers: f ( p 1 ( n ) ) , f ( p 2 ( n ) ) , … , f ( p n ( n ) ) f(p_1(n)), f(p_2(n)), \dots, f(p_n(n)) f(p1(n)),f(p2(n)),,f(pn(n)) .
输入输出样例
输入样例 #1
4 4
1 2 3 4
输出样例 #1
3 4 6 5
输入样例 #2
5 5
2 1 5 3 5
输出样例 #2
9 8 12 6 8
输入样例 #3
2 10
1 2 1 1 2 2 2 2 2 2
输出样例 #3
3 3
说明
Consider the first example:
x = [ 1 , 2 , 3 , 4 ] x = [1, 2, 3, 4] x=[1,2,3,4] , so

  • for the permutation p 1 ( 4 ) = [ 1 , 2 , 3 , 4 ] p_1(4) = [1, 2, 3, 4] p1(4)=[1,2,3,4] the answer is ∣ 1 − 2 ∣ + ∣ 2 − 3 ∣ + ∣ 3 − 4 ∣ = 3 |1 - 2| + |2 - 3| + |3 - 4| = 3 12+23+34=3 ;
  • for the permutation p 2 ( 4 ) = [ 2 , 1 , 3 , 4 ] p_2(4) = [2, 1, 3, 4] p2(4)=[2,1,3,4] the answer is ∣ 2 − 1 ∣ + ∣ 1 − 3 ∣ + ∣ 3 − 4 ∣ = 4 |2 - 1| + |1 - 3| + |3 - 4| = 4 21+13+34=4 ;
  • for the permutation p 3 ( 4 ) = [ 3 , 1 , 2 , 4 ] p_3(4) = [3, 1, 2, 4] p3(4)=[3,1,2,4] the answer is ∣ 2 − 3 ∣ + ∣ 3 − 1 ∣ + ∣ 1 − 4 ∣ = 6 |2 - 3| + |3 - 1| + |1 - 4| = 6 23+31+14=6 ;
  • for the permutation p 4 ( 4 ) = [ 4 , 1 , 2 , 3 ] p_4(4) = [4, 1, 2, 3] p4(4)=[4,1,2,3] the answer is ∣ 2 − 3 ∣ + ∣ 3 − 4 ∣ + ∣ 4 − 1 ∣ = 5 |2 - 3| + |3 - 4| + |4 - 1| = 5 23+34+41=5 .

Consider the second example:
x = [ 2 , 1 , 5 , 3 , 5 ] x = [2, 1, 5, 3, 5] x=[2,1,5,3,5] , so

  • for the permutation p 1 ( 5 ) = [ 1 , 2 , 3 , 4 , 5 ] p_1(5) = [1, 2, 3, 4, 5] p1(5)=[1,2,3,4,5] the answer is ∣ 2 − 1 ∣ + ∣ 1 − 5 ∣ + ∣ 5 − 3 ∣ + ∣ 3 − 5 ∣ = 9 |2 - 1| + |1 - 5| + |5 - 3| + |3 - 5| = 9 21+15+53+35=9 ;
  • for the permutation p 2 ( 5 ) = [ 2 , 1 , 3 , 4 , 5 ] p_2(5) = [2, 1, 3, 4, 5] p2(5)=[2,1,3,4,5] the answer is ∣ 1 − 2 ∣ + ∣ 2 − 5 ∣ + ∣ 5 − 3 ∣ + ∣ 3 − 5 ∣ = 8 |1 - 2| + |2 - 5| + |5 - 3| + |3 - 5| = 8 12+25+53+35=8 ;
  • for the permutation p 3 ( 5 ) = [ 3 , 1 , 2 , 4 , 5 ] p_3(5) = [3, 1, 2, 4, 5] p3(5)=[3,1,2,4,5] the answer is ∣ 3 − 2 ∣ + ∣ 2 − 5 ∣ + ∣ 5 − 1 ∣ + ∣ 1 − 5 ∣ = 12 |3 - 2| + |2 - 5| + |5 - 1| + |1 - 5| = 12 32+25+51+15=12 ;
  • for the permutation p 4 ( 5 ) = [ 4 , 1 , 2 , 3 , 5 ] p_4(5) = [4, 1, 2, 3, 5] p4(5)=[4,1,2,3,5] the answer is ∣ 3 − 2 ∣ + ∣ 2 − 5 ∣ + ∣ 5 − 4 ∣ + ∣ 4 − 5 ∣ = 6 |3 - 2| + |2 - 5| + |5 - 4| + |4 - 5| = 6 32+25+54+45=6 ;
  • for the permutation p 5 ( 5 ) = [ 5 , 1 , 2 , 3 , 4 ] p_5(5) = [5, 1, 2, 3, 4] p5(5)=[5,1,2,3,4] the answer is ∣ 3 − 2 ∣ + ∣ 2 − 1 ∣ + ∣ 1 − 4 ∣ + ∣ 4 − 1 ∣ = 8 |3 - 2| + |2 - 1| + |1 - 4| + |4 - 1| = 8 32+21+14+41=8 .

题目翻译:
定义: p i ( n ) p_i(n) pi(n) 表示把 i i i提到前面的的一个长度为n的序列: [ i , 1 , 2 , … , i − 1 , i + 1 , … , n ] [i, 1, 2, \dots, i - 1, i + 1, \dots, n] [i,1,2,,i1,i+1,,n]
定义: p o s ( p , v a l ) pos(p, val) pos(p,val)表示val在p中的位置,这里的p指的是上面的 p i ( n ) p_i(n) pi(n)
定义: f ( p ) = ∑ i = 1 m − 1 ∣ p o s ( p , x i ) − p o s ( p , x i + 1 ) ∣ f(p) = \sum\limits_{i=1}^{m - 1} |pos(p, x_i) - pos(p, x_{i + 1})| f(p)=i=1m1pos(p,xi)pos(p,xi+1),这里是有绝对值的;
求出: f ( p 1 ( n ) ) , f ( p 2 ( n ) ) , … , f ( p n ( n ) ) f(p_1(n)), f(p_2(n)), \dots, f(p_n(n)) f(p1(n)),f(p2(n)),,f(pn(n))


心路历程

在看到这道题的,第一反应就是:这是什么玩意题?题都看不懂……之后,磨了好长时间,才看懂了题,我太难了……

之后,我就想到了一个优秀的 (n^2)的暴力算法:我们可以构造出每一个 p i ( n ) p_i(n) pi(n),之后再逐一求值。但是,他死了,只过了3组数据,之后就TLE了。

在之后,我就请教了dalao的优秀的差分算法:在这里插入图片描述
但是,我也仅仅只是现在暂时理解了为什么要用差分算法……(挖坑……)


CF1234F Yet Another Substring Reverse

题意翻译(来自洛谷的翻译)
给你一个字符串 S S S,你可以翻转一次 S S S的任意一个子串。
问翻转后 S S S的子串中各个字符都不相同的最长子串长度。
∣ S ∣ ≤ 1 0 6 , ∣ |S|\le10^6,| S106,字符集 ∣ ≤ 20 |\le20 20

思路

因为可以翻转一次,所以我们可以把问题转化成是:求出两个不连续的各个字符都不相同的区间,是的这两个区间的长度和最长。

但是,我们此时又有了一个问题,如何保证这两个区间的字符没有重复呢?

此时,因为字符集的个数最多只有20,所以,我们就可以使用状压了呀!我们,可以把每一个数字的位置表示为字符是否出现。

推荐博客


code

C题正解

#include<bits/stdc++.h>
using namespace std;

const int nn=200003;

int T, n;
char s[2][nn];

int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		scanf("%s%s",s[0]+1, s[1]+1);
		int now=0; bool flag=true; 
		for(int i=1;i<=n;++i)
		{
			if(s[now][i]-'0'>=3)/*需要换行*/
			{
				now=!now;
				if(s[now][i]-'0'<=2) {flag=false; break;}
			}
			if(!flag)	break;
		}
		if(now&&flag)	puts("YES");
		else puts("NO");
	}
	return 0;
}

D题正解

#include<bits/stdc++.h>
using namespace std;

int c[27][100003];
string s;
int Q, op;

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}

void add(int x,int y,int ver)
{
	for(int j=y;j<=s.size();j+=j&(-j))
		c[x][j]+=ver; 
}

int sum(int x,int y)
{
	int ans=0;
	for(int j=y;j>0;j-=j&(-j))
		ans+=c[x][j];
	return ans;
}

int main()
{
	cin>>s;
	for(int i=0;i<s.size();++i)	
		add(s[i]-'a'+1, i+1, 1);
	Q=read();
	while(Q--)
	{
		op=read();
		if(op==1)
		{
			int pos; char ch;
			pos=read(), scanf("%c",&ch);
			char d=s[pos-1];/*原字符*/
			if(d==ch)	continue;
			add(d-'a'+1, pos, -1);
			add(ch-'a'+1, pos, 1);
			s[pos-1]=ch;
		}
		else
		{
			int l=read(), r=read(), ans=0;
			for(int i=1;i<=26;++i)
				ans+=(sum(i,r)-sum(i,l-1)>0?1:0);
			printf("%d\n",ans);
		}
	}
	return 0;
}

E题暴力

#include<bits/stdc++.h>
using namespace std;

const int nn=2e5+3;

int n, m, cnt=0;
int a[nn], s[nn]; 

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}

int main()
{
	n=read(), m=read();
	for(int i=1;i<=m;++i)	a[i]=read();
	for(int i=1;i<=n;++i)
	{
		s[i]=1; int ans=0;
		if(i==1)	cnt=1;
		else	cnt=0;
		for(int j=2;j<=n;++j)	
		{
			if(cnt+1==i)	++cnt;
			s[++cnt]=j;
		}
		for(int k=2;k<=m;++k)
			ans+=abs(s[a[k]]-s[a[k-1]]);
		printf("%d ",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值