A. Array Divisibility
Array Divisibility
直接输出1到n
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << i << (i == n ? '\n' : ' ');
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
B. Corner Twist
Corner Twist
每一次都使用2*2
的方块进行操作,判断将前(n-1)*(m-1)
方块复原后是否相等就可以了
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<string> s1(n);
for (int i = 0; i < n; i++) {
cin >> s1[i];
}
vector<string> s2(n);
for (int i = 0; i < n; i++) {
cin >> s2[i];
}
vector a(n, vector<int>(m));
vector b(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = s1[i][j] - '0';
b[i][j] = s2[i][j] - '0';
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m - 1; j++) {
while (a[i][j] != b[i][j]) {
a[i][j] = (a[i][j] + 1) % 3;
a[i][j + 1] = (a[i][j + 1] + 2) % 3;
a[i + 1][j] = (a[i + 1][j] + 2) % 3;
a[i + 1][j + 1] = (a[i + 1][j + 1] + 1) % 3;
}
}
}
int ans = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != b[i][j]) {
ans = 0;
}
}
}
if (ans) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
C. Have Your Cake and Eat It Too
Have Your Cake and Eat It Too
暴力枚举六种吃蛋糕的顺序即可,注意向上取整,代码写的非常丑陋
#include <bits/stdc++.h>
using namespace std;
void work(int n, long long sum, vector<int> &l, vector<int> &r, vector<long long> &a, vector<long long> &b, vector<long long> &c) {
int ll = 0, rr = 1;
while (rr <= n && a[rr] - a[ll] < sum) {
rr++;
}
if (rr <= n) {
l.push_back(ll + 1);
r.push_back(rr);
}
ll = rr;
rr = ll + 1;
while (rr <= n && b[rr] - b[ll] < sum) {
rr++;
}
if (rr <= n) {
l.push_back(ll + 1);
r.push_back(rr);
}
ll = rr;
rr = n;
if (c[rr] - c[ll] >= sum) {
l.push_back(ll + 1);
r.push_back(rr);
}
}
void solve() {
int n;
cin >> n;
vector<long long> a(n + 1), b(n + 1), c(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += a[i];
}
sum = (sum + 2) / 3;
for (int i = 0; i < n; i++) {
a[i + 1] += a[i];
b[i + 1] += b[i];
c[i + 1] += c[i];
}
vector<int> l, r;
work(n, sum, l, r, a, b, c);
if (l.size() == 3) {
cout << l[0] << ' ' << r[0] << ' ';
cout << l[1] << ' ' << r[1] << ' ';
cout << l[2] << ' ' << r[2] << '\n';
return;
}
l.clear(); r.clear();
work(n, sum, l, r, a, c, b);
if (l.size() == 3) {
cout << l[0] << ' ' << r[0] << ' ';
cout << l[2] << ' ' << r[2] << ' ';
cout << l[1] << ' ' << r[1] << '\n';
return;
}
l.clear(); r.clear();
work(n, sum, l, r, b, a, c);
if (l.size() == 3) {
cout << l[1] << ' ' << r[1] << ' ';
cout << l[0] << ' ' << r[0] << ' ';
cout << l[2] << ' ' << r[2] << '\n';
return;
}
l.clear(); r.clear();
work(n, sum, l, r, b, c, a);
if (l.size() == 3) {
cout << l[2] << ' ' << r[2] << ' ';
cout << l[0] << ' ' << r[0] << ' ';
cout << l[1] << ' ' << r[1] << '\n';
return;
}
l.clear(); r.clear();
work(n, sum, l, r, c, b, a);
if (l.size() == 3) {
cout << l[2] << ' ' << r[2] << ' ';
cout << l[1] << ' ' << r[1] << ' ';
cout << l[0] << ' ' << r[0] << '\n';
return;
}
l.clear(); r.clear();
work(n, sum, l, r, c, a, b);
if (l.size() == 3) {
cout << l[1] << ' ' << r[1] << ' ';
cout << l[2] << ' ' << r[2] << ' ';
cout << l[0] << ' ' << r[0] << '\n';
return;
}
cout << -1 << endl;
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
标签:cin,int,Codeforces,956,++,while,vector,solve,Div
From: https://www.cnblogs.com/foolnine/p/18289507