【AtCoder】ARC080

C - 4-adjacent

咱們挑出來4的倍數和不是4的倍數而是2的倍數,和奇數c++

而後就是放一個奇數,放一個4,若是一個奇數以後沒法放4,而後它又不是最後一個,那麼就不合法ui

#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-12
//#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,a[MAXN];
vector<int> v[5],ans;
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) read(a[i]);
    for(int i = 1 ; i <= N ; ++i) {
        if(a[i] % 4 == 0) v[4].pb(a[i]);
        else if(a[i] % 2 == 0) v[2].pb(a[i]);
        else v[1].pb(a[i]);
    }
    while(v[1].size()) {
        ans.pb(v[1].back());
        v[1].pop_back();
        if(ans.size() == N) break;
        if(v[4].size()) {
            ans.pb(v[4].back());
            v[4].pop_back();
        }
        else {
            puts("No");return;
        }
    }
    puts("Yes");
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

D - Grid Coloring

就是每行填,若是填到末尾,就下一行從末尾開始填spa

若是填到開頭,下一行從開始填code

#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-12
//#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 H,W,N;
int a[100005],c[105][105],g[105];
void Solve() {
    read(H);read(W);
    read(N);
    for(int i = 1 ; i <= N ; ++i) read(a[i]);
    int f = 0;
    int t = 1;
    for(int i = 1 ; i <= N ; ++i) {
        if(g[t] + a[i] < W) {
            if(!f) {
                for(int j = g[t] + 1 ; j <= g[t] + a[i] ; ++j) c[t][j] = i;
            }
            else {
                for(int j = W - g[t] ; j >= W - g[t] - a[i] + 1 ; --j) c[t][j] = i;
            }
            g[t] += a[i];
        }
        else {
            for(int j = 1 ; j <= W ; ++j) {
                if(!c[t][j]) c[t][j] = i;
            }
            a[i] -= W - g[t];
            if(c[t][W] == i) f = 1;
            else f = 0;
            ++t;
            while(a[i]) {
                if(!f) c[t][g[t] + 1] = i;
                else c[t][W - g[t]] = i;
                ++g[t];--a[i];
                if(g[t] == W) ++t;
            }
        }
    }
    for(int i = 1 ; i <= H ; ++i) {
        for(int j = 1 ; j <= W ; ++j) {
            out(c[i][j]);space;
        }
        enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

E - Young Maids

對於一個序列,咱們取一個最小的奇數位置,而後取這個最小的奇數後面的偶數位置,用st表實現,序列會被分紅三份,而後遞歸會造成樹,咱們求一個最小的dfs序便可遞歸

#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-12
//#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,p[MAXN],tot;
int st[2][MAXN][19],len[MAXN],pos[MAXN];
pii t[MAXN];
vector<int> son[MAXN];
set<pii > S;
int query_min(int k,int l,int r) {
    int s = len[r - l + 1];
    return min(st[k][l][s],st[k][r - (1 << s) + 1][s]);
}
int build_tree(int l,int r) {
    if(r < l) return 0;
    int k = (l & 1);
    int a = query_min(k,l,r);
    int b = query_min(k ^ 1,pos[a] + 1,r);
    t[++tot] = mp(a,b);
    int res = tot;
    son[res].pb(build_tree(l,pos[a] - 1));
    son[res].pb(build_tree(pos[a] + 1,pos[b] - 1));
    son[res].pb(build_tree(pos[b] + 1,r));
    return res;
}
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(p[i]);
        st[i & 1][i][0] = p[i];
        st[(i & 1) ^ 1][i][0] = 0x7fffffff;
        pos[p[i]] = i;
    }
    for(int k = 0 ; k <= 1 ; ++k) {
        for(int j = 1 ; j <= 18 ; ++j) {
            for(int i = 1 ; i <= N ; ++i) {
                if(i + (1 << j) - 1 > N) break;
                st[k][i][j] = min(st[k][i][j - 1],st[k][i + (1 << j - 1)][j - 1]);
            }
        }
    }
    for(int i = 2 ; i <= N ; ++i) {
        len[i] = len[i / 2] + 1;
    }
    int rt = build_tree(1,N);
    S.insert(mp(t[rt].fi,rt));
    while(!S.empty()) {
        auto b = *S.begin();S.erase(S.begin());
        out(t[b.se].fi);space;out(t[b.se].se);space;
        for(auto k : son[b.se]) {
            if(k) S.insert(mp(t[k].fi,k));
        }
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

F - Prime Flip

把每一個數改爲異或差分ip

而後修改一個區間至關於修改兩個點,不須要加額外的點,咱們須要把全部異或差分值爲1的地方兩兩配對get

若是相差爲質數,那麼花費爲1it

相差爲偶數 花費爲2class

相差爲奇數且非質數,花費爲3(一個偶數能夠拆成兩個奇素數的和,怎麼地這麼小的範圍總得成立吧,否則哥德巴赫猜測早證僞了,而後只須要挑一個大一點的奇素數減掉這個偶數就好了)sed

以後咱們把這些點分爲奇數點和偶數點,差值爲質數則連邊,作一個最大匹配,以後每一個點集兩兩匹配,若是兩個點集還剩一個,就連一條長度爲3的邊

#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-12
//#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;
bool nonprime[10000005],vis[10000005];
int prime[10000005],tot,x[205],b[205],M[2],matc[205];
vector<int> to[505];
bool used[205];
bool match(int u) {
    for(auto t : to[u]) {
        if(!used[t]) {
            used[t] = 1;
            if(!matc[t] || match(matc[t])) {
                matc[t] = u;
                return true;
            }
        }
    }
    return false;
}
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(x[i]);
        vis[x[i]] = 1;
    }
    for(int i = 2 ; i <= 10000000 ; ++i) {
        if(!nonprime[i]) {
            prime[++tot] = i;
        }
        for(int j = 1 ; j <= tot ; ++j) {
            if(prime[j] > 10000000 / i) break;
            nonprime[i * prime[j]] = 1;
            if(i % prime[j] == 0) break;
        }
    }
    tot = 0;
    for(int i = 1 ; i <= 10000001 ; ++i) {
        if(vis[i] != vis[i - 1]) b[++tot] = i;
    }
    for(int i = 1 ; i <= tot ; ++i) {
        M[b[i] & 1]++;
        for(int j = 1 ; j <= tot ; ++j) {
            if(i == j) continue;
            if(abs(b[i] - b[j]) < 3) continue;
            if(!nonprime[abs(b[i] - b[j])]) to[i].pb(j);
        }
    }
    int ans = 0;
    for(int i = 1 ; i <= tot ; ++i) {
        if(b[i] & 1) {
            memset(used,0,sizeof(used));
            if(match(i)) ++ans;
        }
    }
    out(ans + ((M[0] - ans) / 2 + (M[1] - ans) / 2) * 2 + ((M[0] - ans) & 1) * 3);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}
相關文章
相關標籤/搜索