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 ( 1 ≤ n ≤ 2 ⋅ 1 0 9 ) (1≤n≤2⋅10^9) (1≤n≤2⋅109).
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的数划分成两个集合,求两集合各自和差的绝对值最小
题解:等差序列 a n − 3 + a n = a n − 2 + a n − 1 a_{n-3}+a_{n} = a_{n-2}+a_{n-1} an−3+an=an−2+an−1可推出结论:
code
#include
#include
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 c 1 , c 2 , … c n c_1,c_2,…c_n c1,c2,…cn, where 1 ≤ c i ≤ k 1≤c_i≤k 1≤ci≤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 ( 1 ≤ k ≤ n ≤ 5000 ) (1≤k≤n≤5000) (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 a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 5000 ) a_1,a_2,…,a_n (1≤a_i≤5000) a1,a2,…,an(1≤ai≤5000) — elements of the array a.
Output
If there is no answer, print “NO”. Otherwise print “YES” and any coloring (i.e. numbers c 1 , c 2 , … c n c_1,c_2,…c_n c1,c2,…cn, 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
#include
#include
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 m a x ( 0 , b i − x ) max(0,b_i−x) max(0,bi−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 b i + y b_i+y bi+y (the value y is given). Slavik cannot repair doors with current durability equal to 0.
The game lasts 1 0 100 10^{100} 10100 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 ( 1 ≤ n ≤ 100 , 1 ≤ x , y ≤ 1 0 5 ) (1≤n≤100, 1≤x,y≤10^5) (1≤n≤100,1≤x,y≤105) — the number of doors, value x and value y, respectively.
The second line of the input contains n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 105 ) a_1,a_2,…,a_n (1≤a_i≤105) a1,a2,…,an(1≤ai≤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即提供一个贡献,问经过 1 0 100 10^{100} 10100次操作后贡献值最大是多少?
题解:
code
#include
#include
#include
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’)且其字典序最小。
题解:
code
#include
#include
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 0; i++){
if(s[i]=='2'){
if(f1 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:
- b 1 = 0 b_1=0 b1=0;
- for every pair of indices i and j such that 1≤i,j≤n, if a i = a j a_i=a_j ai=aj, then b i = b j b_i=b_j bi=bj (note that if a i ≠ a j a_i≠a_j ai̸=aj, it is still possible that b i = b j b_i=b_j bi=bj);
- for every index i ∈ [ 1 , n − 1 ] i∈[1,n−1] i∈[1,n−1] either b i = b i + 1 b_i=b_{i+1} bi=bi+1 or b i + 1 = b i + 1 b_{i+1}=b_{i+1} bi+1=bi+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 ( 2 ≤ n ≤ 2 ⋅ 1 0 5 ) (2≤n≤2⋅10^5) (2≤n≤2⋅105)— the number of elements in a.
The second line contains n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 9 ) . a_1,a_2,…,a_n (1≤a_i≤10^9). a1,a2,…,an(1≤ai≤109).
Output
Print one integer — the number of different monotonic renumerations of a, taken modulo 998244353.
题意:
给定一个数组a,求另一个数组b,问b可以是多少种形式。要求
题解:瞎搞一遍就OK
code
#include
#include
#include
#include
using namespace std;
const int mod = 998244353;
const int maxn = 2e5+10;
int w[maxn], p[maxn], m[maxn];
pair 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;
}