Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)
A. Diverse Game
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Petr, watching Sergey's stream, came up with a matrix \(a\), consisting of \(n\) rows and \(m\) columns (the number in the \(i\)-th row and \(j\)-th column is denoted as \(a_{i, j}\)), which contains all integers from \(1\) to \(n \cdot m\). But he didn't like the arrangement of the numbers, and now he wants to come up with a new matrix \(b\), consisting of \(n\) rows and \(m\) columns, which will also contain all integers from \(1\) to \(n \cdot m\), such that for any \(1 \leq i \leq n, 1 \leq j \leq m\) it holds that \(a_{i, j} \ne b_{i, j}\).
You are given the matrix \(a\), construct any matrix \(b\) that meets Petr's requirements, or determine that it is impossible.
Hurry up! Otherwise, he will donate all his money to the stream in search of an answer to his question.
Input
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10\)) — the number of rows and columns of matrix \(a\).
The next \(n\) lines contain \(m\) integers each, describing matrix \(a\). The \(i\)-th of these lines contains the elements of matrix \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}\).
It is guaranteed that all numbers in matrix \(a\) are distinct and \(1 \leq a_{i, j} \leq n \cdot m\).
It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(5 \cdot 10^4\).
Output
For each test case, output \(n \cdot m\) integers — any suitable matrix \(b\), or \(-1\) if such a matrix does not exist.
Example
input
5
1 1
1
2 1
2
1
1 5
2 4 5 3 1
2 4
1 2 3 4
5 6 7 8
3 3
4 2 1
9 8 3
6 7 5
output
-1
1
2
4 5 3 1 2
6 7 8 5
2 3 4 1
8 3 9
7 5 6
2 1 4
Note
In the first test case, there is only one element in the matrix, so matrix \(b\) is the only matrix and it does not fit.
In the second test case \(a_{1, 1} = 2 \neq 1 = b_{1, 1}\), \(a_{2, 1} = 1 \neq 2 = b_{2, 1}\).
題意
給你一個\(n*m\)的矩陣,矩陣中每一位不重複,要求你對其進行變換,使得變化後的每一位和原矩陣中不同,如果可以輸出變化後的矩陣,不行輸出\(-1\)
除矩陣為\(1*1\)時存在無解的情況,其他情況只需要將矩陣每行向上交換再向左交換即可
code
//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;
const int N = 2000010;
const int M = 200;
const int mod = 1e9+7;
void solve(){
int n , m;
cin >> n >> m;
vector<vector<int>> a(n+1,vector<int>(m+1));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++) cin >> a[i][j];
}
if(n==1&&m==1){
cout << -1 << endl;
return;
}
if(n!=1){
for(int i=2;i<=n;i++){
swap(a[i],a[i-1]);
}
}
if(m!=1){
for(int i=1;i<=n;i++)
for(int j=2;j<=m;j++){
swap(a[i][j],a[i][j-1]);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout << a[i][j] << " ";
}
cout << endl;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}
B. Fun Game
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Vova really loves the XOR operation (denoted as \(\oplus\)). Recently, when he was going to sleep, he came up with a fun game.
At the beginning of the game, Vova chooses two binary sequences \(s\) and \(t\) of length \(n\) and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers \(0\) and \(1\). Vanya can choose integers \(l, r\) such that \(1 \leq l \leq r \leq n\), and for all \(l \leq i \leq r\) simultaneously replace \(s_i\) with \(s_i \oplus s_{i - l + 1}\), where \(s_i\) is the \(i\)-th element of the sequence \(s\).
In order for the game to be interesting, there must be a possibility to win. Vanya wins if, with an unlimited number of actions, he can obtain the sequence \(t\) from the sequence \(s\). Determine if the game will be interesting for the sequences \(s\) and \(t\).
Input
Each test consists of multiple test cases. The first line contains an integer \(q\) (\(1 \le q \le 10^{4}\)) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the sequences \(s\) and \(t\).
The second line of each test case contains a binary sequence \(s\) of length \(n\).
The third line of each test case contains a binary sequence \(t\) of length \(n\).
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
Output
For each test case, output "Yes" if the game will be interesting, otherwise output "No".
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
Example
input
6
1
0
1
7
0110100
0110100
9
100101010
101111110
4
0011
1011
4
0100
0001
8
10110111
01100000
output
NO
YES
YES
NO
YES
YES
Note
In the first test case, Vanya will not be able to change the sequence \(s\) with the only possible action of choosing \(l = r = 1\).
In the second test case, the sequences \(s\) and \(t\) are already equal.
In the third test case, Vanya can act as follows:
- Choose \(l = 3\) and \(r = 5\), then \(s\) will become \(\mathtt{101101010}\).
- Choose \(l = 5\) and \(r = 6\), then \(s\) will become \(\mathtt{101111010}\).
- Choose \(l = 7\) and \(r = 7\), then \(s\) will become \(\mathtt{101111110}\).
題意
給你兩個\(01\)串,\(s\)和\(t\),問你經過無數次操作後使得\(s \equiv t\)
操作如下:
你可以選取一段\(l\)到\(r\)的\(s\),即\(s.substr(l,r-l+1)\),進行 \(s_i \oplus = s_{i - l + 1}\)
那麼如果\(s_i\) 是\(0\)要變化成\(1\)的話,只需要讓前面出現一個\(1\)即可;\(si\)是\(1\)要變化成\(0\)的話,只需要把\(l\)和\(r\)都定為\(i\)即可(自己\(\oplus\)自己\(= 0\) )
那麼我們就可以把\(s\)的最大部分先全變成\(1\),判斷是否覆蓋\(t\)中的\(1\)即可
code
//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;
const int N = 2000010;
const int M = 200;
const int mod = 1e9+7;
void solve(){
int n;
cin >> n;
string s , t;
cin >> s >> t;
s = " "+s;
t = " "+t;
vector<int> pos;
vector<int> cnt(n+1) , cnt1(n+1);
for(int i=1;i<=n;i++){
cnt[i] = cnt[i-1];
cnt1[i] = cnt1[i-1];
if(s[i]=='0') cnt[i]++;
if(s[i]=='1') cnt1[i]++;
if(s[i]=='0'&&t[i]=='1') pos.push_back(i);
}
bool fla = 1;
for(int i=0;i<pos.size();i++){
int v = pos[i];
if(cnt1[v]==0) fla = 0;
}
if(fla) cout << "YES" << endl;
else cout << "NO" << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}
C. Hungry Games
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Yaroslav is playing a computer game, and at one of the levels, he encountered \(n\) mushrooms arranged in a row. Each mushroom has its own level of toxicity; the \(i\)-th mushroom from the beginning has a toxicity level of \(a_i\). Yaroslav can choose two integers \(1 \le l \le r \le n\), and then his character will take turns from left to right to eat mushrooms from this subsegment one by one, i.e., the mushrooms with numbers \(l, l+1, l+2, \ldots, r\).
The character has a toxicity level \(g\), initially equal to \(0\). The computer game is defined by the number \(x\) — the maximum toxicity level at any given time. When eating a mushroom with toxicity level \(k\), the following happens:
- The toxicity level of the character is increased by \(k\).
- If \(g \leq x\), the process continues; otherwise, \(g\) becomes zero and the process continues.
Yaroslav became interested in how many ways there are to choose the values of \(l\) and \(r\) such that the final value of \(g\) is not zero. Help Yaroslav find this number!
Input
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{4}\)) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers \(n\), \(x\) (\(1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9\)) — the number of mushrooms and the maximum toxicity level.
The second line of each test case contains \(n\) numbers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
Output
For each test case, output a single number — the number of subsegments such that the final value of \(g\) will not be zero.
Example
input
5
4 2
1 1 1 1
3 2
1 2 3
1 6
10
6 3
1 2 1 4 3 8
5 999999999
999999999 999999998 1000000000 1000000000 500000000
output
8
2
0
10
7
Note
In the first test case, the subsegments \((1, 1)\), \((1, 2)\), \((1, 4)\), \((2, 2)\), \((2, 3)\), \((3, 3)\), \((3, 4)\) and \((4, 4)\) are suitable.
In the second test case, non-zero \(g\) will remain only on the subsegments \((1, 1)\) and \((2, 2)\).
In the third test case, on the only possible subsegment, \(g\) will be zero.
題意
給你一個長度為\(n\)的陣列\(a\),問你有多少個連續的\(\sum_l^r a_i\)進行下面過程
- 角色的毒性等級會增加 \(a_i\) 。
- 如果是 \(\sum a_i \leq x\) ,過程繼續;否則, \(\sum a_i\) 變為零,過程繼續。
\(\sum_l^r a_i\)不為零
連續的一段還求和,自然先字首和處理一下
然後當某一段求和之後\(\leq x\),那麼當前這一段可以出現的方案數即為\(r-l+1\),再考慮後面的,如果加完正好大於,那麼\(r+1\)不可取,再考慮\(r+2\) ,可行的值,即為\(r+2\)後面可取的方案數
那麼不妨從後向前遍歷\(i\),再從\(i\)~\(n\) 中二分找到最遠端的使得\(l\)~\(r+1\)為零的位置\(r\),那麼當前\(i\)中可以湊得的方案數即為\(r-i+1+(r+2)\)位置的方案數
code
//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;
const int N = 2000010;
const int M = 200;
const int mod = 1e9+7;
void solve(){
int n , m;
cin >> n >> m;
vector<int> a(n+1) , s(n+1);
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++) s[i] = s[i-1] + a[i];
int ans =0;
vector<int> f(n+10,0);
for(int i=n;i>0;i--){
int l = i , r = n , p = i-1;
while(l<=r){
int mid = (l+r) >> 1;
if(s[mid]-s[i-1]<=m){
p = mid;
l = mid+1;
}
else{
r = mid-1;
}
}
f[i] = (p-i+1) + f[p+2];
ans += f[i];
}
cout << ans << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}
D. Funny Game
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Vanya has a graph with \(n\) vertices (numbered from \(1\) to \(n\)) and an array \(a\) of \(n\) integers; initially, there are no edges in the graph. Vanya got bored, and to have fun, he decided to perform \(n - 1\) operations.
Operation number \(x\) (operations are numbered in order starting from \(1\)) is as follows:
- Choose \(2\) different numbers \(1 \leq u,v \leq n\), such that \(|a_u - a_v|\) is divisible by \(x\).
- Add an undirected edge between vertices \(u\) and \(v\) to the graph.
Help Vanya get a connected\(^{\text{∗}}\) graph using the \(n - 1\) operations, or determine that it is impossible.
\(^{\text{∗}}\)A graph is called connected if it is possible to reach any vertex from any other by moving along the edges.
Input
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{3}\)) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2000\)) — the number of vertices in the graph.
The second line of each test case contains \(n\) numbers \(a_1, a_2, \cdots a_n\) (\(1 \leq a_i \leq 10^9\)).
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
Output
For each test case, if there is no solution, then output "No" (without quotes).
Otherwise, output "Yes" (without quotes), and then output \(n - 1\) lines, where in the \(i\)-th line, output the numbers \(u\) and \(v\) that need to be chosen for operation \(i\).
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
題意
給你n個點的獨立點集,每個點有個對應的值\(a_i\)
現在需要新增\(n-1\)條邊,使得成為一個連通圖
新增邊有以下限制
當新增到第\(i\)條邊的時候,$\vert a_x - a_y \vert $ 要為\(i\)的倍數
如果可以連成連通圖,輸出"YES",並輸出對應的連邊方案
先解決連邊的條件$\vert a_x - a_y \vert $ 要為\(i\) 的倍數,即$\vert a_x - a_y \vert $ % \(i = 0\)
進一步分解,即\(a_x\) % \(i\) \(\equiv\) \(a_y\) % \(i\)
即\(a_x\) 和\(a_y\) 關於\(i\) 同餘
然後,處理連通圖的話就用並查集維護一下當前連通的塊即可
\(n\)的資料範圍就2000,資料範圍比較小,就寫的一坨,\(O(n^3)\) 寫法
code
//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;
const int N = 2010;
const int M = 200;
const int mod = 1e9+7;
vector<int> g[N][N];
int fa[N];
int find(int x){
if(x==fa[x]) return x;
return fa[x] = find(fa[x]);
}
void solve(){
memset(fa,0,sizeof fa);
int n;
cin >> n;
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
g[i][j].clear();
}
}
vector<int> a(n+1);
vector<PII> ans;
for(int i=1;i<=n;i++) cin >> a[i] , fa[i]=i;
for(int i=1;i<=n;i++){
for(int j=1;j<n;j++){
g[j][a[i]%j].push_back(i);
}
}
for(int i=n-1;i>0;i--){
int fla = 0;
for(int j=0; j<i;j++){
if(fla) break;
if(g[i][j].size()>=2){
for(int k=0;k<g[i][j].size();k++){
int x = g[i][j][k];
if(fla) break;
for(int k2=0;k2<g[i][j].size();k2++){
int y = g[i][j][k2];
if(x==y) continue;
if(find(x)==find(y)) continue;
fa[find(x)] = find(y);
ans.push_back({x,y});
fla = 1;
break;
}
}
}
}
}
if(ans.size()<n-1){
cout << "NO" << endl;
return;
}
reverse(ans.begin(),ans.end());
cout << "YES" << endl;
for(auto [x,y]:ans){
cout << x << " " << y << endl;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}