A. Love Story
Problem - A - Codeforces
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
//const int N =
void solve() {
string s;
cin >> s;
int cnt = 0;
string tmp = "codeforces";
for (int i = 0; i < s.size(); i ++)
if (s[i] != tmp[i]) cnt ++;
cout << cnt << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t --) solve();
return 0;
}
B. Blank Space
Problem - B - Codeforces
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
//const int N =
void solve() {
int n;
cin >> n;
vector<int> a(n + 10);
for (int i = 1; i <= n; i ++) cin >> a[i];
int ans = -inf, len = 0;
for (int i = 1; i <= n; i ++) {
if (a[i] == 0) {
len ++;
}
if (a[i] != 0) {
ans = max(ans, len);
len = 0;
}
}
cout << max(ans, len) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t --) solve();
return 0;
}
C. Mr. Perfectly Fine
Problem - C - Codeforces
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
//const int N =
void solve() {
int n;
cin >> n;
ll min1 = inf, min2 = inf, mintot = inf;
for (int i = 1; i <= n; i ++) {
ll a; string b;
cin >> a >> b;
if (b == "10") {
if (min1 > a) {
min1 = a;
}
}
if (b == "01") {
if (min2 > a) {
min2 = a;
}
}
if (b == "11") {
if (mintot > a) {
mintot = a;
}
}
}
ll res = min1 + min2;
res = min(mintot, res);
if (res >= inf / 2) cout << "-1" << endl;
else cout << res << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t --) solve();
return 0;
}
D. Gold Rush
Problem - D - Codeforces
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
//const int N =
void solve() {
int n, m;
cin >> n >> m;
if (n == m) cout << "YES" << endl;
else if (n < m) cout << "NO" << endl;
else {
unordered_map<int, bool> mp;
queue<int> q;
q.push(m);
while (q.size() != 0) {
int t = q.front();
q.pop();
if (t % 2 == 0) {
int tmp1 = t / 2 * 3;
if (tmp1 <= n) {
q.push(tmp1);
mp[tmp1] = true;
}
int tmp2 = t * 3;
if (tmp2 <= n) {
q.push(tmp2);
mp[tmp2] = true;
}
}
else {
int tmp2 = t * 3;
if (tmp2 <= n) {
q.push(tmp2);
mp[tmp2] = true;
}
}
}
if (mp[n] == true) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t --) solve();
return 0;
}
E. The Lakes
Problem - E - Codeforces
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
const int N = 1010;
int n, m;
int g[N][N];
bool st[N][N];
int dirx[4] = {-1, 0, 1, 0}, diry[4] = {0, 1, 0, -1};
int bfs(int x, int y) {
int ans = 0;
queue<PII> q;
st[x][y] = true;
q.push({x, y});
ans += g[x][y];
while (q.size() != 0) {
PII t = q.front();
q.pop();
for (int i = 0; i < 4; i ++) {
int tx = t.first + dirx[i], ty = t.second + diry[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && g[tx][ty] > 0 && st[tx][ty] == false) {
st[tx][ty] = true;
q.push({tx, ty});
ans += g[tx][ty];
}
}
}
return ans;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++) {
cin >> g[i][j];
st[i][j] = false;
}
int ans = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
if (g[i][j] > 0 && st[i][j] == false) {
ans = max(ans, bfs(i, j));
}
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t --) solve();
return 0;
}
F. Forever Winter
Problem - F - Codeforces文章来源:https://www.toymoban.com/news/detail-437282.html
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
// const int N = ;
void solve() {
int n, m;
cin >> n >> m;
vector<int> deg(n + 10);
for (int i = 1; i <= m; i ++) {
int u, v;
cin >> u >> v;
deg[u] ++; deg[v] ++; // 统计每个点的度数
}
unordered_map<int, int> mp;
for (int i = 1; i <= n; i ++)
if (deg[i] != 0)
mp[deg[i]] ++; // 统计不同度数出现的次数
int ans1 = 0, ans2 = 0;
for (auto x : mp) {
if (x.second == 1) ans1 = x.first; // 出现次数为1, 则是中心点
else {
// 出现次数 >1, 且度数不是1, 即不是最外圈的点, 则是 y, 中间层的点
if (x.first != 1) ans2 = x.first - 1;
}
}
// 有可能中心点的度数和中间层的度数一样, 则没有出现次数为1的点, 则ans1 = 0,
// 则中心点的度数 = 中间层点的度数 + 1
if (ans1 == 0) ans1 = ans2 + 1;
cout << ans1 << " " << ans2 << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t --) solve();
return 0;
}
G. Hits Different
Problem - G - Codeforces文章来源地址https://www.toymoban.com/news/detail-437282.html
// 1^2 + 2^2 + 3^2 + 4^2 + ... + n^2 = n * (n + 1) * (2 * n + 1) / 6
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;
const int N = 2025;
const int M = 2.1e6;
PII layer[N];
bool stl[M], str[M];
int n;
ll s[N][N];
int search(int x) { // 查找 数x 在哪一层
int now = 0;
for (int i = 1; i <= x; i ++) {
if (layer[i].first <= x && layer[i].second >= x) {
now = i;
break;
}
}
return now;
}
ll call(int c, int now) { // 计算左侧小三角的和
ll ans = 0;
for (int i = 1; i <= now - c + 1; i ++) { // 逐层利用公式O(1)时间复杂度求和并累加
int l = layer[c + i - 1].first;
int r = layer[c + i - 1].first + i - 1;
ans += (ll)r * (r + 1) * (2 * r + 1) / 6 - (ll)(l - 1) * (l + 1 - 1) * (2 * l + 1 - 2) / 6;
}
return ans;
}
ll calr(int c, int now) { // 计算右侧小三角的和
ll ans = 0;
for (int i = 1; i <= now - c + 1; i ++) { // 逐层利用公式O(1)时间复杂度求和并累加
int l = layer[c + i - 1].second - i + 1;
int r = layer[c + i - 1].second;
ans += (ll)r * (r + 1) * (2 * r + 1) / 6 - (ll)(l - 1) * (l + 1 - 1) * (2 * l + 1 - 2) / 6;
}
return ans;
}
void solve() {
cin >> n;
if (n == 1) {
cout << 1 << endl;
}
else if (n > 1) {
int now = search(n);
int pre = n - layer[now].first; // n 前面有几个数
int past = layer[now].second - n; // n 后面有几个数
int last = layer[now].second; // n所在层的最后一个数是多少
ll sum = (ll)last * (last + 1) * (2 * last + 1) / 6; // 求大三角的总和
int l = now - pre + 1; // 左面小三角的第一个数是在第几层
int r = now - past + 1; // 右面小三角的第一个数是在第几层
cout << sum - call(l, now) - calr(r, now) << endl; // 总大三角 - 左小三角 - 右小三角 = 中间所求的三角
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int l = 1, r = 1; // 预处理出每一层的左右端点的数是多少
layer[1] = {1, 1};
for (int i = 2; i <= 2023; i ++) {
l = layer[i - 1].first + i - 1;
r = layer[i - 1].second + i;
stl[l] = true;
str[r] = true;
layer[i] = {l, r};
}
int t;
cin >> t;
while(t --) solve();
return 0;
}
到了这里,关于Codeforces Round 871 (Div. 4) A ~ G的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!