码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-04 23:21:22      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:unsigned   insert   make   拓扑图   signed   tin   bool   端点   保留   

感觉最近好颓,以后不能这么颓了,要省选了,争取省选之前再板刷一面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

标签:unsigned   insert   make   拓扑图   signed   tin   bool   端点   保留   

原文地址:https://www.cnblogs.com/ivorysi/p/10352316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!