Codeforces Round #660 (Div. 2)

今晚海星, 不过wa的地方都是细节, 有点难受

A

wa一次, 忘了考虑重复的情况

#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 IO ios::sync_with_stdio(0); cin.tie(0);
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 main() {
    IO;
    for (cin >> _; _; --_) {
        cin >> n;
        if (n <= 30) cout << "NO\n";
        else {
            int a = 6, b = 10, c = 14, d = n - 30;
            if (d == a || d == b || d == c) {
                d -= 1; c += 1;
            }
            cout << "YES\n" << a << ' ' << b << ' ' << c << ' ' << d << '\n'; 
        }
    }
    return 0;
}

B

不是8就是9, 判一下就行

#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 IO ios::sync_with_stdio(0); cin.tie(0);
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 main() {
    IO;
    for (cin >> _; _; --_) {
        cin >> n; m = n;
        string s = "";
        rep (i, 1, n) {
            if (m >= 4) s += '8', m -= 4;
            else if (m > 0) s += '8', m = 0;
            else s += '9';
        }
 
        per (i, s.size() - 1, 0) cout << s[i];
        cout << '\n';
    }
    return 0;
}

C

wa 1次, 链式前向星没清空
dfs, 判断就行

#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 IO ios::sync_with_stdio(0); cin.tie(0);
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;
ll p[N], h[N];
int head[N], ne[N << 1], to[N << 1], tot;
 
void add(int u, int v) {
    ne[++tot] = head[u]; head[u] = tot; to[tot] = v;
}
 
int dfs(int u, int f) {
    int a = 0;
    for (int i = head[u]; i; i = ne[i]) {
        int v = to[i];
        if (v == f) continue;
        int cur = dfs(v, u);
        if (cur < 0) return -1;
        a += cur;
        p[u] += p[v];
    }
 
    if (abs(h[u]) > p[u]) return -1;
    if ((p[u] + h[u]) & 1) return -1;
    int x = (p[u] + h[u]) >> 1, y = p[u] - x;
    if (x < a) return -1;
    return x;
}
 
int main() {
    IO;
    for (cin >> _; _; --_) {
        memset(head, 0, sizeof head); tot = 0;
        cin >> n >> m;
        rep(i, 1, n) cin >> p[i];
        rep(i, 1, n) cin >> h[i];
        rep(i, 2, n) {
            int u, v; cin >> u >> v;
            add(u, v); add(v, u);
        }
 
        if (dfs(1, 0) >= 0) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}

D

wa1, 爆ll

也是dfs, 判就行了

#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 IO ios::sync_with_stdio(0); cin.tie(0);
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 = 2e5 + 5;
 
int n, m, _;
ll a[N], b[N], k;
int h[N], ne[N], to[N], tot;
vector<ll> ans, d[N], head;
 
void add(int u, int v) {
    ne[++tot] = h[u]; h[u] = tot; to[tot] = v;
}
 
void dfss(int u) {
    for (ll v : d[u]) {
        ans.pb(v);
        dfss(v);
    }
}
 
ll dfs(int u) {
    for (int i = h[u]; i; i = ne[i]) {
        int v = to[i];
        ll cur = dfs(v); k += cur;
        if (cur < 0)  d[u].pb(v);
        else {
            ans.pb(v), a[u] += cur;
            dfss(v);
        }
    }
    return a[u];
}
 
int main() {
    IO;
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    rep(i, 1, n) {
        cin >> b[i];
        if (b[i] != -1) add(b[i], i);
        else head.pb(i);
    }
 
    for (int i : head) {
        k += dfs(i); ans.pb(i);
        dfss(i);
    }
 
    cout << k << '\n';
    for (ll i : ans) cout << i << ' ';
    return 0;
}

posted @ 2020-07-31 00:40  洛绫璃  阅读(318)  评论(12编辑  收藏  举报