Educational Codeforces Round 31

Educational Codeforces Round 31 

A. Book Reading

暴力

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "
"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
    int n, t;
    sci(n); sci(t);
    vi A(n);
    for(int &x : A) sci(x);
    int tt = 0;
    for(int i = 0; i < n; i++){
        tt += 86400 - A[i];
        if(tt>=t){
            cout << i + 1 << endl;
            return;
        }
    }
    cout << n << endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}

B. Japanese Crosswords Strike Back

确定唯一,必须是每中间插一个(0)

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "
"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
    int n, x;
    sci(n); sci(x);
    vi A(n);
    for(int &x : A) sci(x);
    if(accumulate(all(A),0)+n-1==x) cout << "YES" << endl;
    else cout << "NO" << endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}

C. Bertown Subway

找出所有置换环,把最大的两个连起来即可

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "
"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
    int n; sci(n);
    vi A(n); for(int &x : A) sci(x);
    vi loop;
    vi vis(n,0);
    LL ret = 0;
    for(int i = 0; i < n; i++){
        if(vis[i]) continue;
        int u = i, sz = 0;
        while(!vis[u]){
            sz++;
            vis[u] = true;
            u = A[u] - 1;
        }
        loop << sz;
        ret += 1ll * sz * sz;
    }
    sort(all(loop),greater<int>());
    if(loop.size()==1) cout << ret << endl;
    else ret += (loop[0]+loop[1]+0ll) * (loop[0]+loop[1]) - 1ll * loop[0] * loop[0] - 1ll * loop[1] * loop[1], cout << ret << endl;

}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}

D. Boxes And Balls

霍夫曼编码,如果是偶数的话加一个(0),然后每次取最小的三个合并

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "
"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
    int n; sci(n);
    vi A(n); for(int &x : A) sci(x);
    priority_queue<LL,vl,greater<LL> > que;    
    for(int &x : A) que.push(x);
    LL ret = 0;
    if(!(que.size()&1)) que.push(0);
    while(que.size()!=1){
        LL x = que.top(); que.pop();
        LL y = que.top(); que.pop();
        LL z = que.top(); que.pop();
        ret += x + y + z;
        que.push(x+y+z);
    }
    cout << ret << endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}

E. Binary Matrix

看似卡空间

实则卡时间

暴力来做的话把每个所有位置并查集一下就好了

由于空间只有(16MB),那就一行一行来处理就行了

记一下上一行有哪些位置是联通的

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "
"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = (1<<15) + 7;
int f[MAXN];
int exc(char c){
    if(isdigit(c)) return c - '0';
    else return c - 'A' + 10;
}
int n, m;
bool last[MAXN], now[MAXN];
char str[MAXN];
int findx(int x){ return x == f[x] ? x : f[x] = findx(f[x]); }
void solve(){
    int ret = 0;
    sci(n); sci(m);
    for(int i = 0; i < m * 2; i++) f[i] = i;
    while(n--){
        scanf("%s",str);
        for(int i = 0; i < m / 4; i++){
            int x = exc(str[i]);
            for(int j = 0; j < 4; j++) now[i*4+3-j] = (x>>j&1);
        }
        int cont = 0;
        for(int i = 0; i < m; i++) if(now[i]) cont++;
        for(int i = 0; i < m; i++){
            if(!now[i]) continue;
            if(i!=0 and now[i-1] and findx(i)!=findx(i-1)) f[findx(i)] = findx(i-1), cont--;
            if(last[i] and findx(i)!=findx(i+m)) f[findx(i+m)] = findx(i), cont--;
        }
        for(int i = 0; i < m; i++) findx(i);
        for(int i = 0; i < m; i++) f[i+m] = f[i] + m, f[i] = i;
        ret += cont;
        for(int i = 0; i < m; i++) last[i] = now[i];
    }
    cout << ret << endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}

F. Anti-Palindromize

费用流,源点向每个字符连边,容量为字符出现次数,费用为(0)

每个位置向汇点连边,容量为(1),费用为(0)

接下来对称的位置对每个字符公用一个点,用于限流,字符点向新增的这个点连容量(1),费用(0)的点,然后再这个点向对称的两个位置分别连容量为(1),费用为对应费用的边

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "
"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e3+7;
int pre[MAXN], dist[MAXN], flow[MAXN], preid[MAXN];
bool vis[MAXN];
#define S 0
#define T MAXN - 1
struct EDGE{
    int to,cap,fee,rev;
    EDGE(){}
    EDGE(int _to, int _cap, int _fee, int _rev){
        to = _to; cap = _cap;
        fee = _fee; rev = _rev;
    }
};
vector<EDGE> G[MAXN];
void ADDEDGE(int u, int v, int cap, int fee){
    G[u].emplace_back(EDGE(v,cap,fee,(int)G[v].size()));
    G[v].emplace_back(EDGE(u,0,-fee,(int)G[u].size()-1));
}
bool spfa(){
    memset(dist,0x3f,sizeof(dist));
    dist[S] = 0;
    flow[S] = INF;
    memset(vis,0,sizeof(vis));
    queue<int> que;
    que.push(S);
    while(!que.empty()){
        int u = que.front();
        que.pop();
        vis[u] = 0;
        for(int i = 0; i < (int)G[u].size(); i++){
            auto e = G[u][i];
            if(!e.cap or dist[e.to]<=dist[u]+e.fee) continue;
            dist[e.to] = dist[u] + e.fee;
            flow[e.to] = min(e.cap,flow[u]);
            pre[e.to] = u; preid[e.to] = i;
            if(!vis[e.to]){
                vis[e.to] = 1;
                que.push(e.to);
            }
        }
    }
    return dist[T]!=INF;
}
int mcmf(){
    int cost = 0;
    while(spfa()){
        int u = T;
        cost += dist[T] * flow[T];
        while(u!=S){
            int p = pre[u], id = preid[u];
            G[p][id].cap -= flow[T];
            G[u][G[p][id].rev].cap += flow[T];
            u = pre[u];
        }
    }
    return cost;
}
char s[MAXN];
int n, w[MAXN], cnt[26];
void solve(){
    sci(n);
    scanf("%s",s+1);
    for(int i = 1; i <= n; i++) sci(w[i]);
    for(int i = 1; i <= n; i++) cnt[s[i]-'a']++;
    for(int i = 1; i <= 26; i++) ADDEDGE(S,i,cnt[i-1],0);
    for(int i = 1; i <= n; i++) ADDEDGE(26+i,T,1,0);
    int tot = 26 + n;
    for(int i = 1; i <= n/2; i++){
        for(int j = 0; j < 26; j++){
            tot++;
            ADDEDGE(j+1,tot,1,0);
            ADDEDGE(tot,26+i,1,j==s[i]-'a'?-w[i]:0);
            ADDEDGE(tot,26+n+1-i,1,j==s[n+1-i]-'a'?-w[n+1-i]:0);
        }
    }
    cout << -mcmf() << endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}
原文地址:https://www.cnblogs.com/kikokiko/p/13566100.html