Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)解题报告(A-C)
A. Diverse Game
链接: A. Diverse Game
题目大意:将所给矩阵进行变换为一个与原矩阵不同的矩阵
解题思路:每个值都右移一位,如果每行都只有一个值,则每个值都下移一位
个人代码:
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n+1, vector<int> (m+1, -1));
vector<vector<int>> b(n+1, vector<int> (m+1, -1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
// if (i == n && j == n) b[i][j] = a[1][1];
// else if (i == n) b[i][j] = a[1][j];
// else if (j == m) b[i][j] = a[i][1];
}
}
if (n == 1 && m == 1) cout << -1 << endl;
else if (n == 1){
for (int i = 1; i < m; i++) {
cout << a[1][i+1] << ' ';
}
cout << a[1][1] << endl;
} else if (m == 1) {
for (int i = 1; i < n; i++) {
cout << a[i+1][1] << endl;
}
cout << a[1][1] << endl;
} else {
for (int i = 1; i <= n; i++) {
for (int j = 1; j < m; j++) {
cout << a[i][j+1] << ' ';
}
cout << a[i][1] << endl;
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
题解代码(cpp):
#include<bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tests;
cin >> tests;
while (tests--) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (auto &i: a) {
for (auto &j: i) cin >> j;
}
if (n * m == 1) cout << "-1\n";
else {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << a[(i + 1) % n][(j + 1) % m] << ' ';
}
cout << '\n';
}
}
}
}
Fun Game
链接:B. Fun Game
题目大意:给两个二进制序列s和t,问s能否变换为t序列
解题思路:根据1 ^ 1 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 0 ^ 0 = 0, 可发现若想将当前不匹配的值进行变换,需前面出现过1,只需判断1是否出现就行了
个人代码:
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n;
vector<char> s(n+1), t(n+1);
vector<char> Change(n+1, '0');
for (int i = 1; i <= n; i++)
cin >> s[i];
for (int i = 1; i <= n; i++)
cin >> t[i];
int flag = 1;
if (s[1] == '1') ;
else {
for (int i = 1; i <= n; i++) {
if (s[i] == '1') Change[i] = '1';
else Change[i] = Change[i-1];
}
for (int i = 1; i <= n; i++) {
if (s[i] != t[i] && Change[i] == '0') {
flag = 0;
}
}
}
if (flag) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
题解代码(cpp):
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string s, t;
cin >> s >> t;
for (int i = 0; i < s.size() && s[i] == '0'; ++i) {
if (t[i] != '0') {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
Hungry Games
题目:
解题思路:先计算前缀和,然后用前缀和将当前位置与之后位置进行比较,找到超过x的位置,当前位置的dp的值等于两个位置的相差位数加上后一个位置的dp值,即 d p [ i ] = d p [ j + 1 ] + j − i dp[i] = dp[j + 1] + j - i dp[i]=dp[j+1]+j−i,
题解代码(cpp):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
// cin.tie(nullptr)->sync_with_stdio(false);
int tests;
cin >> tests;
while (tests--) {
int n;
ll x;
cin >> n >> x;
int q;
vector<ll> a(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
partial_sum(a.begin() + 1, a.end(), a.begin() + 1); //记录每个位置的前缀和
vector<int> dp(n + 2); //需考虑i为末尾时也需要跟之后的位置进行比较,故长度为n+2
//从后往前dp
for (int i = n - 1; i >= 0; --i) {
//找到i位置第一个大于x的值的位置q,i位置的方法数等于q位置的方法数加上i到q位置的差值-1(因为加到q时g重置为0)
q = upper_bound(a.begin(), a.end(), a[i] + x) - a.begin();
dp[i] = dp[q] + q - i - 1;
}
cout << accumulate(dp.begin(), dp.end(), 0ll) << '\n';
}
}