Codeforces Round #531 (Div. 3)

A. Integer Sequence Dividing

You are given an integer sequence 1,2,…,n. You have to divide it into two sets A and B in such a way that each element belongs to exactly one set and |sum(A)−sum(B)| is minimum possible.
The value |x| is the absolute value of x and sum(S) is the sum of elements of the set S.
Input
The first line of the input contains one integer n (1n2109)(1≤n≤2⋅10^9).
Output
Print one integer — the minimum possible value of |sum(A)−sum(B)| if you divide the initial sequence 1,2,…,n into two sets A and B.

題意:將1-n的數劃分成兩個集合,求兩集合各自和差的絕對值最小
題解:等差序列 an3+an=an2+an1a_{n-3}+a_{n} = a_{n-2}+a_{n-1}可推出結論:

  • IF n%4==1 || n % 4 == 2 --> |sum(A)−sum(B)| = 1
  • ELSE --> |sum(A)−sum(B)| = 0

code

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n;
	scanf("%d", &n);
	printf("%d\n", n%4==1 || n%4==2? 1:0);
	return 0;
} 

B. Array K-Coloring

You are given an array a consisting of n integer numbers.
You have to color this array in k colors in such a way that:
Each element of the array should be colored in some color;
For each i from 1 to k there should be at least one element colored in the i-th color in the array;
For each i from 1 to k all elements colored in the i-th color should be distinct.
Obviously, such coloring might be impossible. In this case, print “NO”. Otherwise print “YES” and any coloring (i.e. numbers c1,c2,cnc_1,c_2,…c_n, where 1cik1≤c_i≤k and ci is the color of the i-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.
Input
The first line of the input contains two integers n and k (1kn5000)(1≤k≤n≤5000) — the length of the array a and the number of colors, respectively.
The second line of the input contains n integers a1,a2,,an(1ai5000)a_1,a_2,…,a_n (1≤a_i≤5000) — elements of the array a.
Output
If there is no answer, print “NO”. Otherwise print “YES” and any coloring (i.e. numbers c1,c2,cnc_1,c_2,…c_n, where 1≤ci≤k and ci is the color of the i-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any.

題意:給定一個數組a對其染色,元素相同的不能染同一種顏色,且要求必須染出K種顏色,輸出一種符合要求的染色方案。
題解:枚舉一遍數組a,每次查看存在哪一種顏色未使用,若存在第i種顏色未使用,則將該點變爲i;若所有顏色均使用過,則選擇不衝突的顏色對其染色(不衝突指該顏色沒有對該種元素上色)。最後檢查是否存在沒有進行染色的元素和沒使用的顏色。
code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 5e3+10;
int w[maxn], c[maxn];
bool vis[maxn][maxn];
int main(){
	memset(vis, false, sizeof(vis));
	int n, k;
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++){
		scanf("%d", &w[i]);
	}
	for(int i = 0; i < n; i++){
		bool flag = false;
		for(int j = 1; j <= k; j++){
			if(!vis[j][0]){
				vis[j][w[i]] = vis[j][0] = flag = true;
				c[i] = j;
				break;
			}
		}
		for(int j = 1; j <= k && !flag; j++){
			if(!vis[j][w[i]]){
				vis[j][w[i]] = flag = true;
				c[i] = j;
				break;
			}
		}
		if(!flag){
			printf("NO\n");
			return 0;
		}
	}
	for(int i = 1; i <= k; i++){
		if(!vis[i][0]){
			printf("NO\n");
			return 0;	
		}
	}
	printf("YES\n");
	for(int i = 0; i < n; i++) i!=n-1?printf("%d ", c[i]):printf("%d\n", c[i]);
	return 0;
} 

C. Doors Breaking and Repairing

You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move.
There are n doors, the i-th door initially has durability equal to ai.
During your move you can try to break one of the doors. If you choose door i and its current durability is bi then you reduce its durability to max(0,bix)max(0,b_i−x) (the value x is given).
During Slavik’s move he tries to repair one of the doors. If he chooses door i and its current durability is bi then he increases its durability to bi+yb_i+y (the value y is given). Slavik cannot repair doors with current durability equal to 0.
The game lasts 1010010^{100} turns. If some player cannot make his move then he has to skip it.
Your goal is to maximize the number of doors with durability equal to 0 at the end of the game. You can assume that Slavik wants to minimize the number of such doors. What is the number of such doors in the end if you both play optimally?
Input
The first line of the input contains three integers n, x and y (1n100,1x,y105)(1≤n≤100, 1≤x,y≤10^5) — the number of doors, value x and value y, respectively.
The second line of the input contains n integers a1,a2,,an(1ai105)a_1,a_2,…,a_n (1≤a_i≤105), where ai is the initial durability of the i-th door.
Output
Print one integer — the number of doors with durability equal to 0 at the end of the game, if you and Slavik both play optimally.

題意:給定一個數組,第奇數次操作可以對序列中任意一個元素值-x;第偶數次操作可以對數組中任意一個元素值+y(該元素值要求爲非0)。每當有一個元素值爲0即提供一個貢獻,問經過1010010^{100}次操作後貢獻值最大是多少?
題解:

  • x&gt;yx &gt; y時,所有貢獻爲n。
  • xyx \leq y時,奇數次操作將元素值小於等於x的元素變爲0貢獻值+1,偶數次操作將元素值小於等於x的元素值增+x,當序列除去元素值0的元素外不存在元素其值小於等於x時無法再提供貢獻。

code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e2+10;
int w[maxn];
int main(){
	int n, x, y;
	scanf("%d%d%d", &n, &x, &y);
	for(int i = 0; i < n;  i++){
		scanf("%d", &w[i]);
	}
	sort(w, w+n);
	if(x > y) printf("%d\n", n);
	else{
		int k = (int)(upper_bound(w, w+n, x)-w);
		printf("%d\n", k&1?k/2+1:k/2);
	}
	return 0;
} 

D. Balanced Ternary String

You are given a string s consisting of exactly n characters, and each character is either ‘0’, ‘1’ or ‘2’. Such strings are called ternary strings.
Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters ‘0’ in this string is equal to the number of characters ‘1’, and the number of characters ‘1’ (and ‘0’ obviously) is equal to the number of characters ‘2’).
Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.
Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters ‘0’, ‘1’ and ‘2’.
It is guaranteed that the answer exists.
Input
The first line of the input contains one integer n (3≤n≤3⋅105, n is divisible by 3) — the number of characters in s.
The second line contains the string s consisting of exactly n characters ‘0’, ‘1’ and ‘2’.
Output
Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.
Because n is divisible by 3 it is obvious that the answer exists. And it is obvious that there is only one possible answer.

題意:給定一個長度爲n(n爲3的倍數)的字符串僅存在字符‘0’‘1’‘2’,可以對字符進行修改,但不能添加或者刪除。要求將字符串修改成字符串中cnt(‘0’)=cnt(‘1’)=cnt(‘2’)且其字典序最小。
題解:

  • 若’0’個數多了,字符串從後往前掃,先填’2‘,後填’1‘
  • 若’2’個數多了,字符串從前往後掃,先填‘0’,後填‘1’
  • 若‘1’個數多了,字符串從前往後掃,把能換‘0’的換‘0’,再從後往前掃,把能換‘1’的換‘1’。

code

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 3e5+10;
char s[maxn];
int main(){
	int n;
	scanf("%d%s", &n, s);
	int f1, f2, f3, k = n/3;
	f1 = f2 = f3 = 0;
	for(int i = 0; i < n; i++){
		if(s[i]=='0') f1++;
		else if(s[i] == '1') f2++;
		else f3++;
	}
	for(int i = n-1; i>=0 && f1-k> 0; i--){
		if(s[i]=='0'){
			if(f3<k){
				s[i] = '2';
				f3++;
			}
			else{
				s[i] = '1';
				f2++;
			}
			f1--;
		}
	}
	for(int i = 0; i < n && f3-k> 0; i++){
		if(s[i]=='2'){
			if(f1<k){
				s[i] = '0';
				f1++;
			}
			else{
				s[i] = '1';
				f2++;
			}
			f3--;
		}
	}
	for(int i = 0; i < n && f2-k > 0; i++){
		if(s[i] == '1'){
			if(f1 < k){
				s[i] = '0';
				f1++;
				f2--;
			}
		}
	}
	for(int i = n-1; i >= 0 && f2-k > 0; i--){
		if(s[i] == '1'){
			if(f3 < k){
				s[i] = '2';
				f3++;
				f2--;
			}
		}
	}
	printf("%s", s);
	return 0;
}

E. Monotonic Renumeration

You are given an array a consisting of n integers. Let’s denote monotonic renumeration of array a as an array b consisting of n integers such that all of the following conditions are met:

  • b1=0b_1=0;
  • for every pair of indices i and j such that 1≤i,j≤n, if ai=aja_i=a_j, then bi=bjb_i=b_j (note that if aiaja_i≠a_j, it is still possible that bi=bjb_i=b_j);
  • for every index i[1,n1]i∈[1,n−1] either bi=bi+1b_i=b_{i+1} or bi+1=bi+1b_{i+1}=b_{i+1}.
    For example, if a=[1,2,1,2,3], then two possible monotonic renumerations of a are b=[0,0,0,0,0] and b=[0,0,0,0,1].

Your task is to calculate the number of different monotonic renumerations of a. The answer may be large, so print it modulo 998244353.
Input
The first line contains one integer n (2n2105)(2≤n≤2⋅10^5)— the number of elements in a.
The second line contains n integers a1,a2,,an(1ai109).a_1,a_2,…,a_n (1≤a_i≤10^9).
Output
Print one integer — the number of different monotonic renumerations of a, taken modulo 998244353.

題意:
給定一個數組a,求另一個數組b,問b可以是多少種形式。要求

  • ai=aj(ij)a_i = a_j(i≠j),則bi=bj(ij)b_i = b_j(i≠j)
  • bi=bi+1b_i=b_{i+1} or bi+1=bi+1b_{i+1}=b_{i+1}i[1,n1]i∈[1,n−1]

題解:瞎搞一遍就OK
code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod = 998244353;
const int maxn = 2e5+10;
int w[maxn], p[maxn], m[maxn];
pair<int, int> v[maxn];
long long POW(long long x, long long p){
	long long ans = 1;
	while(p){
		if(p & 1) ans = (ans*x)%mod;
		x = (x*x)%mod;
		p>>=1;
	}
	return ans;
}
int main(){
	memset(m, -1, sizeof(m));
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &w[i]);
		v[i] = make_pair(w[i], i);
	}
	sort(v, v+n);
	for(int i = 0; i < n; i++) p[i] = v[i].first;
	int node = (int)(lower_bound(p, p+n, w[n-1])-p);
	node = v[node].second;
	for(int i = node; i < n; i++) m[i] = 0;
	while(node-1>=0){
		int idx = upper_bound(p, p+n, w[node-1])-p;
		idx-=1;
		idx = v[idx].second;
		if(idx >= node){
			node = lower_bound(p, p+n, w[node-1])-p;
			node = v[node].second;
			int f = idx;
			for(int i = node; i <= idx; i++){
				int tidx = upper_bound(p, p+n, w[i])-p;
				tidx-=1;
				tidx = v[tidx].second;
				f = max(f, tidx);
				int tIdx = lower_bound(p, p+n, w[i])-p;
				tIdx = v[tIdx].second;
				node = min(node, tIdx);
			}
			idx = f;
			for(int i = node; i <= idx; i++){
				m[i] = m[idx];
			}
		}
		else{
			node = lower_bound(p, p+n, w[node-1])-p;
			node = v[node].second;	
			for(int i = node; i <= idx; i++){
				m[i] = m[idx+1]+1;
			}
		}
	}
	printf("%lld\n", POW(2, m[0]));
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章