A
Petr, watching Sergey’s stream, came up with a matrix a a a, consisting of n n n rows and m m m columns (the number in the i i i-th row and j j j-th column is denoted as a i , j a_{i, j} ai,j), which contains all integers from 1 1 1 to n ⋅ m n \cdot m n⋅m. But he didn’t like the arrangement of the numbers, and now he wants to come up with a new matrix b b b, consisting of n n n rows and m m m columns, which will also contain all integers from 1 1 1 to n ⋅ m n \cdot m n⋅m, such that for any 1 ≤ i ≤ n , 1 ≤ j ≤ m 1 \leq i \leq n, 1 \leq j \leq m 1≤i≤n,1≤j≤m it holds that a i , j ≠ b i , j a_{i, j} \ne b_{i, j} ai,j=bi,j.
You are given the matrix a a a, construct any matrix b b 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 t t ( 1 ≤ t ≤ 1 0 3 1 \leq t \leq 10^3 1≤t≤103) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers n n n and m m m ( 1 ≤ n , m ≤ 10 1 \leq n, m \leq 10 1≤n,m≤10) — the number of rows and columns of matrix a a a.
The next n n n lines contain m m m integers each, describing matrix a a a. The i i i-th of these lines contains the elements of matrix a i , 1 , a i , 2 , … , a i , m a_{i, 1}, a_{i, 2}, \ldots, a_{i, m} ai,1,ai,2,…,ai,m.
It is guaranteed that all numbers in matrix a a a are distinct and 1 ≤ a i , j ≤ n ⋅ m 1 \leq a_{i, j} \leq n \cdot m 1≤ai,j≤n⋅m.
It is guaranteed that the sum of n ⋅ m n \cdot m n⋅m over all test cases does not exceed 5 ⋅ 1 0 4 5 \cdot 10^4 5⋅104.
**这道题我唐的一批,交了好多发错的,先来欣赏一下我的错误吧 **
我直接把1~n做了个标记,然后从大到小输出满足(num[k]!=1&&a[i][j]!=k)
就输出了,听起来好像问题不大,不就换个顺序嘛,但是其实非也。
例如3124如果按我的方法,就是先美美输出123,但是发现,4输不出来了,WA’
后来我又用了另一种方法,就是直接给所给数换序
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int num[1001];
int main() {
int t, n, m, sign;
int a[11][11];
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
if (n == 1 && m == 1) {
cout << -1 << endl;
continue;
} else if (n == 1) {
for (int i = 1; i < m; i++)
printf("%d ", a[0][i]);
cout << a[0][0] << endl;
} else if (m == 1) {
for (int j = 1; j < n; j++)
cout << a[j][0] << endl;
cout << a[0][0] << endl;
} else {
for (int i = 0; i < n ; i++) {
for (int j = 1; j < m; j++)
cout << a[i][j] << ' ';
cout << a[i][0];
cout << endl;
}
}
}
return 0;
}
要注意判断n=m=1时输出-1
B
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 s s and t t t of length n n n and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers 0 0 0 and 1 1 1. Vanya can choose integers l , r l, r l,r such that 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1≤l≤r≤n, and for all l ≤ i ≤ r l \leq i \leq r l≤i≤r simultaneously replace s i s_i si with s i ⊕ s i − l + 1 s_i \oplus s_{i - l + 1} si⊕si−l+1, where s i s_i si is the i i i-th element of the sequence s s 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
t
t from the sequence
s
s
s. Determine if the game will be interesting for the sequences
s
s
s and
t
t
t.
Input
Each test consists of multiple test cases. The first line contains an integer q q q ( 1 ≤ q ≤ 1 0 4 1 \le q \le 10^{4} 1≤q≤104) — 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 n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1≤n≤2⋅105) — the length of the sequences s s s and t t t.
The second line of each test case contains a binary sequence s s s of length n n n.
The third line of each test case contains a binary sequence t t t of length n n n.
It is guaranteed that the sum of
n
n
n over all test cases does not exceed
2
⋅
1
0
5
2 \cdot 10^5
2⋅105.
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).
6
1
0
1
7
0110100
0110100
9
100101010
101111110
4
0011
1011
4
0100
0001
8
10110111
01100000
NO
YES
YES
NO
YES
YES
这个题需要一些思考,自己去试一试样例,然后就发现其实整个异或就是全是0,然后就是如果第一个数是1,那么所有的都能直接变成1。
然后看第二个给的NO的样例
其实就是前面0011的00没办法改变,所以其实就是找第一个出现的1,如果给的数组比所需数组早,就可以,这里注意如果没有出现1,就把位置设成最大的数(我设成最小的,结果特判错了)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a1[200005],a2[200005];
int main() {
int n, t, s1, s2;
cin >> t;
while (t--) {
cin >> n;
s1 = s2 = 5000000;
for (int i = 0; i < n; i++)
scanf("%1d", &a1[i]);
for (int i = 0; i < n; i++)
if (a1[i]) {
s1 = i;
break;
}
for (int i = 0; i < n; i++)
scanf("%1d", &a2[i]);
for (int i = 0; i < n; i++)
if (a2[i]) {
s2 = i;
break;
}
if (s1 <= s2) {
cout << "YES" << endl;
} else
cout << "NO" << endl;
}
return 0;
}
C
Yaroslav is playing a computer game, and at one of the levels, he encountered n n n mushrooms arranged in a row. Each mushroom has its own level of toxicity; the i i i-th mushroom from the beginning has a toxicity level of a i a_i ai. Yaroslav can choose two integers 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1≤l≤r≤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 , … , r l, l+1, l+2, \ldots, r l,l+1,l+2,…,r.
The character has a toxicity level g g g, initially equal to 0 0 0. The computer game is defined by the number x x x — the maximum toxicity level at any given time. When eating a mushroom with toxicity level k k k, the following happens:
- The toxicity level of the character is increased by k k k.
- If g ≤ x g \leq x g≤x, the process continues; otherwise, g g g becomes zero and the process continues.
Yaroslav became interested in how many ways there are to choose the values of
l
l
l and
r
r
r such that the final value of
g
g
g is not zero. Help Yaroslav find this number!
Input
Each test consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^{4} 1≤t≤104) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers n n n, x x x ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ x ≤ 1 0 9 1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9 1≤n≤2⋅105,1≤x≤109) — the number of mushrooms and the maximum toxicity level.
The second line of each test case contains n n n numbers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109).
It is guaranteed that the sum of
n
n
n over all test cases does not exceed
2
⋅
1
0
5
2 \cdot 10^5
2⋅105.
Output
For each test case, output a single number — the number of subsegments such that the final value of
g
g
g will not be zero.
这道题,是dp+二分+前缀和
首先我们可以发现,他的和从0开始(a),过一会又归0了(b),其中有sum[b]-sum[a-1]=x所以其实我们可以用dp把dp[a]和dp[b]联系起来,因为到b以后相当于另1一个开始,即dp[a]=dp[b]+b-a
其中dp[i]表示以i为左端点的情况数之和,在a到b之间有b-a的情况当然可以算到a为节点里面的,而且我们这个前面的状态是以后面为转移的,所以从后往前进行dp。
这就是大体思路
我们将a的前缀和存起来,用二分找sum[a-1]+x在哪里(也就是b的位置)
这里吧二分函数再复习一下
lower_bound(a+1,a+1+n,v);返回位置 a+1~a+1+n 中v的位置 若没有,则返回它应该所在的位置 ,若多个则返回第一个的位置
这个是小于等于的位置
upper_bound(a+1,a+1+n,v);返回位置 a+1~a+1+n 中v的位置加1 若没有,则返回它应该所在的位置 ,若多个则返回最后一个的位置加1;
这个是大于的位置
要好好理解这个它应该所在的位置
就像你就算给1,2,3给一个4他虽然只有3个下标,但是会返回4,因为他应该在第四个下标的位置
这里还有要注意,就是其实这个sum[a-1]+b的点不能做下一个点,因为是0嘛,所以要在这个点的下一个,所以用upper_bound
下面是代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 200005
ll a[N], sum[N], dp[N];
int main() {
ll x, n, t, ans = 0, tmp;
cin >> t;
while (t--) {
memset(dp, 0, sizeof(dp));
ans = 0;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
for (int i = n; i >= 1; i--) {
// if(sum[i]+x>=sum[n]){
// dp[i]=n-i+1;
// continue;
// }
tmp = upper_bound(sum + 1, sum + 1 + n, sum[i - 1] + x) - sum;
dp[i] = tmp - i + dp[tmp + 1];
// cout << i << ' ' << tmp << ' ' << dp[i] << endl;
}
for (int i = 1; i <= n; i++)
ans += dp[i];
cout << ans << endl;
}
return 0;
}
结语
第一次打codeforces 虽然不简单但好像也没有很难,所以以后还想打,希望可以继续加油
1212

被折叠的 条评论
为什么被折叠?



