Codeforces Round #668 (Div. 2)

A

倒着输出

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e5 + 5;
 
int n, m, _, k;
int a[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        rep (i, 1, n) cin >> a[i];
        per (i, n, 1) cout << a[i] << ' ';
        cout << endl;
    }
    return 0;
}

B

贪心

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e5 + 5;
 
int n, m, _, k;
int a[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        ll ans = 0, s = 0;
        rep (i, 1, n) {
            cin >> a[i];
            if (a[i] == 0) continue;
            if (a[i] > 0) s += a[i];
            else if (a[i] + s >= 0) s += a[i];
            else {
                a[i] += s; s = 0;
                ans += -a[i];
                //cout << ans << '
';
            }
        }
        cout << ans << '
';
    }
    return 0;
}

C

正着倒着来一遍

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 3e5 + 5;
 
int n, m, _, k;
char s[N];

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> k >> s + 1;
        int x = 0, y = 0, z = 0;
        rep (i, 1, k)
            if (s[i] == '0') ++x;
            else if (s[i] == '1') ++y;
            else ++z;
        if (abs(x - y) > z) {cout << "NO
"; continue; }
 
        bool f = 1;
        rep (i, k + 1, n) {
            if (s[i - k] == s[i]) continue;
            if (s[i] == '?') s[i] = s[i - k];
            else if (s[i - k] == '?') s[i - k] = s[i];
            else { f = 0; break; }
        }
        if (!f) {cout << "NO
"; continue; }
 
        x = y = z = 0;
        per (i, n, n - k + 1)
            if (s[i] == '0') ++x;
            else if (s[i] == '1') ++y;
            else ++z;
        if (abs(x - y) > z) {cout << "NO
"; continue; }
 
        f = 1;
        per (i, n - k, 1) {
            if (s[i + k] == s[i]) continue;
            if (s[i] == '?') s[i] = s[i + k];
            else if (s[i + k] == '?') s[i + k] = s[i];
            else { f = 0; break; }
        }
        if (!f) {cout << "NO
"; continue; }
        else cout << "YES
";
    }
    return 0;
}

D

单独写开了, 我nt了

E

是一道很好的区间修改思维问题, 单独写开了

原文地址:https://www.cnblogs.com/2aptx4869/p/13624532.html