1. 程式人生 > >【AtCoder】全國統一プログラミング王決定戦予選/NIKKEI Programming Contest 2019

【AtCoder】全國統一プログラミング王決定戦予選/NIKKEI Programming Contest 2019

span 4 sum sub c++ turn ins gin ack del

感覺最近好頹,以後不能這麽頹了,要省選了,爭取省選之前再板刷一面ATC???

A - Subscribers

簡單容斥

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(‘ ‘)
#define enter putchar(‘\n‘)
#define MAXN 20000005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘) {
        res = res * 10 + c - ‘0‘;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    int n,a,b;
    read(n);read(a);read(b);
    out(min(a,b));space;out(max(0,a + b - n));enter;
}

B - Touitsu

每一位單獨考慮,有多少個不同的字母就改這個不同個數-1次

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(‘ ‘)
#define enter putchar(‘\n‘)
#define MAXN 20000005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘) {
        res = res * 10 + c - ‘0‘;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
int N;
string a,b,c;
int num[4];
void Solve() {
    read(N);
    cin>> a >> b >> c;
    int ans = 0;
    for(int i = 0 ; i < N ; ++i) {
        num[0] = a[i] - ‘a‘;
        num[1] = b[i] - ‘a‘;
        num[2] = c[i] - ‘a‘;
        sort(num,num + 3);
        int m = unique(num,num + 3) - num - 1;
        ans += m;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

C - Different Strokes

假設第二個人全吃了,第一個人的代價就是每次\(a_{i} + b_{i}\)

第二個一口沒動

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(‘ ‘)
#define enter putchar(‘\n‘)
#define MAXN 100005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘) {
        res = res * 10 + c - ‘0‘;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
int N;
int a[MAXN],b[MAXN],id[MAXN];
bool vis[MAXN];
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(a[i]);read(b[i]);id[i] = i;
    }
    sort(id + 1,id + N + 1,[](int s,int t){return a[s] + b[s] > a[t] + b[t];});
    int64 ans = 0;
    for(int i = 1 ; i <= N ; ++i) {
        if(i & 1) ans += a[id[i]];
        else ans -= b[id[i]];
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

D - Restore the Tree

每個點在拓撲圖裏找一條最長路,把最長路上的路徑保留就是這棵樹

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(‘ ‘)
#define enter putchar(‘\n‘)
#define MAXN 100005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘) {
        res = res * 10 + c - ‘0‘;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
vector<int> to[MAXN],pre[MAXN];
int N,M,deg[MAXN],p[MAXN],fa[MAXN];
queue<int> Q;
void Solve() {
    read(N);read(M);
    int u,v;
    for(int i = 1 ; i <= N - 1 + M ; ++i) {
        read(u);read(v);
        deg[v]++;
        to[u].pb(v);
        pre[v].pb(u);
    }
    for(int i = 1 ; i <= N ; ++i) {
        if(!deg[i]) Q.push(i);
    }
    while(!Q.empty()) {
        int u = Q.front();Q.pop();
        for(auto v : to[u]) {
            p[v] = max(p[v],p[u] + 1);
            if(!(--deg[v])) Q.push(v);
        }
        for(auto t : pre[u]) {
            if(p[t] + 1 == p[u]) fa[u] = t;
        }
    }
    for(int i = 1 ; i <= N ; ++i) {
        out(fa[i]);enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

E - Weights on Vertices and Edges

一個聯通塊合法只取決於聯通塊裏價值最大的邊

從小到大加邊,判斷某個價值的邊在比它小的邊連成的聯通塊裏是否能達到限制

然後從大到小,每次把一條邊和這個邊連著的點比它小的邊都連出來

如果發現一條邊兩個端點沒聯通,這條邊又不合法,那這條邊就得被刪掉

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(‘ ‘)
#define enter putchar(‘\n‘)
#define MAXN 100005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘) {
        res = res * 10 + c - ‘0‘;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
int N,M;
struct node {
    int to,next,val;
}E[MAXN * 2];
int head[MAXN],sumE;
int fa[MAXN];
int64 sum[MAXN],x[MAXN];
bool vis[MAXN];
struct Enode {
    int u,v,c;
}s[MAXN];
void add(int u,int v,int c) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    E[sumE].val = c;
    head[u] = sumE;
}
int getfa(int u) {
    return fa[u] == u ? u : fa[u] = getfa(fa[u]);
}
void dfs(int u,int c) {
    int p = getfa(u);
    for(int i = head[u] ; i ; i = E[i].next) {
        if(E[i].val <= c) {
            int q = getfa(E[i].to);
            if(q != p) {
                fa[q] = p;
                dfs(E[i].to,c);
            }
        }
    }
}
void Solve() {
    read(N);read(M);
    for(int i = 1 ; i <= N ; ++i) read(x[i]);
    int u,v,c;
    for(int i = 1 ; i <= M ; ++i) {
        read(u);read(v);read(c);
        add(u,v,c);add(v,u,c);
        s[i] = (Enode){u,v,c};
    }
    sort(s + 1,s + M + 1,[](Enode a,Enode b){return a.c < b.c;});
    for(int i = 1 ; i <= N ; ++i) {
        fa[i] = i;sum[i] = x[i];
    }
    int t = 1;
    for(int i = 1 ; i <= M ; ++i) {
        int p = getfa(s[i].u),q = getfa(s[i].v);
        if(p != q) {
            fa[q] = p;
            sum[p] += sum[q];
        }
        if(i == N || s[i].c != s[i + 1].c) {
            for(int j = t; j <= i; ++j) {
                p = getfa(s[j].u);
                if(sum[p] >= s[j].c) vis[j] = 1;
            }
            t = i + 1;
        }
    }
    for(int i = 1 ; i <= N ; ++i) fa[i] = i;
    int ans = 0;
    for(int i = M ; i >= 1 ; --i) {
        if(getfa(s[i].u) == getfa(s[i].v)) continue;
        if(!vis[i]) {++ans;continue;}
        dfs(s[i].u,s[i].c);
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

F - Jewels

每次增加1的時候

要麽就從已經選了兩個點的剩余集合裏+1

或者去掉一個已選的單個,加上一對

或者去掉一個已選的一對,加上三個

或者去掉三個,扔兩個對,可以通過去掉一個然後把剩余選的對用未選的對更新一下。。

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(‘ ‘)
#define enter putchar(‘\n‘)
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘) {
        res = res * 10 + c - ‘0‘;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
struct node {
    int pos;int64 v;
    friend bool operator < (const node &a,const node &b) {
        return a.v > b.v || (a.v == b.v && a.pos < b.pos);
    }
};
set<node> S,D,T,CD,CS;
vector<int64> ver[MAXN];
int p[MAXN],N,K;
int64 ans,rec[MAXN];
void Choose_one() {
    node t = *S.begin();S.erase(S.begin());CS.insert(t);
    ans += t.v;
    ++p[t.pos];
    if(p[t.pos] <= ver[t.pos].size() - 1) {
        S.insert((node){t.pos,ver[t.pos][p[t.pos]]});
    }
}
void Delete_one() {
    node t = *(--CS.end());CS.erase(--CS.end());
    ans -= t.v;
    if(p[t.pos] <= ver[t.pos].size() - 1) {
        S.erase((node){t.pos,ver[t.pos][p[t.pos]]});
    }
    S.insert(t);--p[t.pos];
}
void Choose_two() {
    node t = *D.begin();D.erase(D.begin());CD.insert(t);
    ans += t.v;
    p[t.pos] = 2;
    if(p[t.pos] <= ver[t.pos].size() - 1) {
        S.insert((node){t.pos,ver[t.pos][p[t.pos]]});
    }
    if(ver[t.pos].size() >= 3) {
        T.erase((node){t.pos,ver[t.pos][0] + ver[t.pos][1] + ver[t.pos][2]});
    }
}
void Delete_two() {
    node t = *(--CD.end());CD.erase(--CD.end());
    S.erase((node){t.pos,ver[t.pos][p[t.pos]]});
    D.insert((node){t.pos,ver[t.pos][0] + ver[t.pos][1]});
    if(ver[t.pos].size() >= 3) {
        T.insert((node){t.pos,ver[t.pos][0] + ver[t.pos][1] + ver[t.pos][2]});
    }
    ans -= t.v;
}
void Choose_three() {
    node t = *T.begin();T.erase(T.begin());
    ans += t.v;
    p[t.pos] = 3;
    D.erase((node){t.pos,ver[t.pos][0] + ver[t.pos][1]});
    CD.insert((node){t.pos,ver[t.pos][0] + ver[t.pos][1]});
    CS.insert((node){t.pos,ver[t.pos][2]});
    if(p[t.pos] <= ver[t.pos].size() - 1) {
        S.insert((node){t.pos,ver[t.pos][p[t.pos]]});
    }
}
void Solve() {
    read(N);read(K);
    int c;int64 v;
    for(int i = 1 ; i <= N ; ++i) {
        read(c);read(v);ver[c].pb(v);
    }
    for(int i = 1 ; i <= K ; ++i) {
        sort(ver[i].begin(),ver[i].end(),[](int64 a,int64 b){return a > b;});
    }
    for(int i = 1 ; i <= K ; ++i) {
        if(ver[i].size()) {
            D.insert((node){i,ver[i][0] + ver[i][1]});
            if(ver[i].size() >= 3) T.insert((node){i,ver[i][0] + ver[i][1] + ver[i][2]});
        }
    }
    rec[1] = -1;ans = 0;
    puts("-1");
    for(int i = 2 ; i <= N ; ++i) {
        if(rec[i - 1] == -1) {
            Choose_two();
            rec[i] = ans;
        }
        else {
            int64 w[3] = {-1,-1,-1};
            if(S.size()) {
                w[0] = ans + (*S.begin()).v;
            }
            if(CS.size() && D.size()) {
                w[1] = ans - (*(--CS.end())).v + (*D.begin()).v;
            }
            if(CD.size() && T.size()) {
                w[2] = ans - (*(--CD.end())).v + (*T.begin()).v;
            }
            if(w[0] < 0 && w[1] < 0 && w[2] < 0) {rec[i] = -1;}
            else {
                if(w[0] >= max(w[1],w[2])) {Choose_one();}
                else if(w[1] >= max(w[0],w[2])) {Delete_one();Choose_two();}
                else {Delete_two();Choose_three();}
                rec[i] = ans;
            }
            while(1) {
                if(!D.size()) break;
                if(!CD.size()) break;
                node t = *(--CD.end());
                node s = *D.begin();
                if(p[t.pos] == 2) {
                    if(s.v > t.v) {
                        Delete_two();Choose_two();
                    }
                    else break;
                }
                else break;
                rec[i] = ans;
            }
        }
        out(rec[i]);enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

【AtCoder】全國統一プログラミング王決定戦予選/NIKKEI Programming Contest 2019