【AtCoder】AGC034

AGC034

AtCoder brush for so long I found myself still only ABCE (manual goodbye

A - Kenken Race

The effect that a row, each point can hop-step or two-step jump, each grid is open space or stone, the stone required for each step can not go on or someone grid, whether we can find \ (A \) to move to the \ ( C \) , \ (B \) is moved to the \ (D \) , \ (A <C, B <D, A <B \)

See \ (A \) to \ (C \) and \ (B \) to \ (D \) way there two stones together, there is no legitimate

If \ (A \) need to cross the \ (B \) , then see \ (B \) to \ (D \) on the way there three linked spaces, no not legitimate

#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 eps 1e-10
#define MAXN 200005
#define ba 47
//#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,B,C,D;
char s[MAXN];
void Solve() {
    read(N);read(A);read(B);read(C);read(D);
    scanf("%s",s + 1);
    int ed = max(C,D),st = min(A,B);
    for(int i = st + 1 ; i <= ed ; ++i) {
    if(s[i] == '#' && s[i - 1] == '#') {
        puts("No");return;
    }
    }
    if(C > D) {
    bool f = 0;
    for(int i = B ; i <= D ; ++i) {
        if(s[i] == '.' && s[i - 1] == '.'  && s[i + 1] == '.') {f = 1;break;}
    }
    if(!f) {puts("No");return;}
    }
    puts("Yes");
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

B - ABC

Each time you select \ (ABC \) can become \ (BCA \) , the most asked several operations

Statistics with \ (i \) followed by the suffix have started much \ (BC \)

If \ (s [i] == B , s [i + 1] == C \) then \ (suf [i] = suf [i + 2] + 1 \)

If \ (s [i] == A \) then \ (suf [i] = suf [i + 1] \)

Each answer is \ (s [i] == A \) a \ (suf [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 eps 1e-10
#define MAXN 200005
#define ba 47
//#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);
}
char s[MAXN];
int L,suf[MAXN];
void Solve() {
    scanf("%s",s + 1);
    L = strlen(s + 1);
    int64 ans = 0;
    for(int i = L - 1; i >= 1 ; --i) {
    if(s[i] == 'B' && s[i + 1] == 'C') suf[i] = suf[i + 2] + 1;
    else if(s[i] == 'A') {
        suf[i] = suf[i + 1];
        ans += suf[i];
    }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

C - Tests

To the effect that two people with the exam, the first person can be assigned a degree of importance to each subject score (degree of importance for each subject is an interval, the first person selected in this interval in), and then himself desperately school, every school a Branch this division will be one hour +1 score, the score has an upper limit of X, the first person to claim the final score for each subject for each subject take the importance of greater than or equal importance of each subject, the second individual scores for each subject ride, ask the first person at least to learn how long

Consider, if the degree of importance of each subject is fixed, the first person certainly important to first learn out of a maximum degree, to learn the second largest, the answer will certainly be a number of X, add up to less than a division of X

With the form of answers, we look back on this issue, you can calculate for each science to lead the second person after X number, greedy choose the k X X makes a re-election, the first man to exceed the second one of the

Then enumerate each subject, plus in addition to their maximum k-X, and then enumerate how long can this science more than the second person, is a monotonic function can be half, so he finished

#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 eps 1e-10
#define MAXN 100005
#define ba 47
//#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,pos,id[MAXN];
int64 l[MAXN],u[MAXN],b[MAXN],val[MAXN],X,all;
bool vis[MAXN];
void Solve() {
    read(N);read(X);
    for(int i = 1 ; i <= N ; ++i) {
    read(b[i]);read(l[i]);read(u[i]);
    all -= b[i] * l[i];
    val[i] = (X - b[i]) * u[i] + b[i] * l[i];
    }
    for(int i = 1 ; i <= N ; ++i) id[i] = i;
    sort(id + 1,id + N + 1,[](int a,int b){return val[a] > val[b];});
    int64 sum = 0;
    for(int i = 1 ; i <= N ; ++i) {
    sum += val[id[i]];
    if(sum + all >= 0) {pos = i - 1;sum -= val[id[i]];break;}
    vis[id[i]] = 1;
    }
    all += sum;
    int64 ans = (pos + 1) * X; 
    for(int i = 1 ; i <= N ; ++i) {
    int64 tmp = all;
    if(vis[i]) {tmp -= val[i];tmp += val[id[pos + 1]];}
    int64 L = 0,R = X + 1;
    while(L < R) {
        int64 mid = (L + R) >> 1;
        int64 sc = 0;
        if(mid >= b[i]) sc = (mid - b[i]) * u[i];
        else sc = (mid - b[i]) * l[i];
        if(tmp + b[i] * l[i] + sc >= 0) R = mid;
        else L = mid + 1;
    }
    if(R <= X) {
        ans = min(ans,pos * X + R);
    }
    }
    out(ans);enter;
    
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

D - Manhattan Max Matching

N meaning of the title is a red dot and one blue n points in the plane, each of the red and blue dots have several red ball or basketball, equal to the total number of required two two red basketball match, a matching value of each two point Manhattan distance, not exceeding 10000, n <= 1000

Low level, see cost flow problem usually do not always

Due to the excellent flow properties of the cost, it is easy to find cost flow can help us choose the kind of greatest expansion in the four Manhattan distance, so we set up four kinds of dot represents four kinds of expansion, the number of sides is not \ (N ^ 2 \) instead \ (N \) a

#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 eps 1e-10
#define MAXN 2005
#define ba 47
//#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 S,T,Ncnt,N;
int a[2][MAXN],ty[4];
struct node {
    int to,next,cap;int64 val;
}E[MAXN * 100];
int head[MAXN * 2],sumE = 1;
int rx[MAXN],ry[MAXN],rc[MAXN];
int bx[MAXN],by[MAXN],bc[MAXN];
int dx[4] = {1,1,-1,-1};
int dy[4] = {1,-1,1,-1};

void add(int u,int v,int c,int64 a) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    E[sumE].cap = c;
    E[sumE].val = a;
    head[u] = sumE;
}
void addtwo(int u,int v,int c,int64 a) {
    add(u,v,c,a);
    add(v,u,0,-a);
}
int64 dis[MAXN];bool inq[MAXN];
int preE[MAXN];
queue<int> Q;
int64 SPFA() {
    for(int i = 1 ; i <= Ncnt ; ++i) dis[i] = -1e18;
    memset(inq,0,sizeof(inq));
    dis[S] = 0;inq[S] = 1;Q.push(S);
    while(!Q.empty()) {
    int u = Q.front();Q.pop();inq[u] = 0;
    for(int i = head[u] ; i ; i = E[i].next) {
        int v = E[i].to;
        if(E[i].cap) {
        if(dis[v] < dis[u] + E[i].val) {
            dis[v] = dis[u] + E[i].val;
            preE[v] = i;
            if(!inq[v]) {Q.push(v);inq[v] = 1;}
        }
        }
    }
    }
    return dis[T];
}
void Init() {
    read(N);
    S = ++Ncnt;
    for(int i = 0 ; i < 2 ; ++i) {
    for(int j = 1 ; j <= N ; ++j) {
        a[i][j] = ++Ncnt;
    }
    }
    for(int i = 0 ; i < 4 ; ++i) ty[i] = ++Ncnt;
    T = ++Ncnt;
    for(int i = 1 ; i <= N ; ++i) {read(rx[i]);read(ry[i]);read(rc[i]);}
    for(int i = 1 ; i <= N ; ++i) {read(bx[i]);read(by[i]);read(bc[i]);}
    for(int i = 1 ; i <= N ; ++i) {
    addtwo(S,a[0][i],rc[i],0);
    for(int j = 0 ; j < 4 ; ++j) {
        addtwo(a[0][i],ty[j],1e9,dx[j] * rx[i] + dy[j] * ry[i]);
    }
    }
    for(int i = 1 ; i <= N ; ++i) {
    addtwo(a[1][i],T,bc[i],0);
    for(int j = 0 ; j < 4 ; ++j) {
        addtwo(ty[j],a[1][i],1e9,-dx[j] * bx[i] - dy[j] * by[i]);
    }
    }
}
void Solve() {
    int64 ans = 0;
    while(SPFA() > 0) {
    int p = preE[T],f = 1e9;
    while(p) {
        f = min(f,E[p].cap);
        p = preE[E[p ^ 1].to];
    }
    ans += dis[T] * f;
    p = preE[T];
    while(p) {
        E[p].cap -= f;E[p ^ 1].cap += f;
        p = preE[E[p ^ 1].to];
    }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
}

E - Complete Compress

Meaning of the questions: Each point may initially have a mark, you can select two each have at least one marker point, the initial time point to a distance of at least 2, and at the same time closer to a place away from the step, do not ask may all point to a point, and find the minimum distance

Enumeration end \ (u \) , if all the marked points and \ (u \) distance and is \ (x \) , then if \ (x \) is odd apparently come to the stage, if \ (x \) is even, then the answer must be \ (\ frac {x} { 2} \) now we just sentenced to answer whether there is

Provided that exists \ (U \) after removal of the point in each sub-tree can be paired in different sub-trees, having a number satisfying the condition that most of the points in the sub-tree is less than a point equal to one half of all the points

However, possible sub-tree can also be paired off, reducing the portion of the distance, how much distance we have a bottom-up dp calculated for each point can be reduced by up to

#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 eps 1e-10
#define MAXN 500005
#define ba 47
//#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);
}
const int MAXV = 150000,LEN = 450000;
int N,M;
int a[MAXN],d;
int cnt[LEN + 5];
int getpos(int x) {
    return x - d + MAXV;
}
struct node {
    int l,r,val,cnt,lz;
}tr[LEN * 4 + 5];
void update(int u) {
    tr[u].val = min(tr[u << 1].val,tr[u << 1 | 1].val);
    tr[u].cnt = 0;
    if(tr[u].val == tr[u << 1].val) tr[u].cnt += tr[u << 1].cnt;
    if(tr[u].val == tr[u << 1 | 1].val) tr[u].cnt += tr[u << 1 | 1].cnt;
}
void build(int u,int l,int r) {
    tr[u].l = l;tr[u].r = r;
    if(l == r) {tr[u].cnt = 1;return;}
    int mid = (l + r) >> 1;
    build(u << 1,l,mid);
    build(u << 1 | 1,mid + 1,r);
    update(u);
}
void addlz(int u,int v) {
    tr[u].val += v;tr[u].lz += v;
}
void pushdown(int u) {
    if(tr[u].lz) {
    addlz(u << 1,tr[u].lz);
    addlz(u << 1 | 1,tr[u].lz);
    tr[u].lz = 0;
    }
}
void add(int u,int l,int r,int v) {
    if(tr[u].l == l && tr[u].r == r) {
    addlz(u,v);
    return;
    }
    pushdown(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    if(r <= mid) add(u << 1,l,r,v);
    else if(l > mid) add(u << 1 | 1,l,r,v);
    else {add(u << 1,l,mid,v);add(u << 1 | 1,mid + 1,r,v);}
    update(u);
}
pii Query(int u,int l,int r) {
    if(tr[u].l == l && tr[u].r == r) return mp(tr[u].val,tr[u].cnt);
    pushdown(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    if(r <= mid) return Query(u << 1,l,r);
    else if(l > mid) return Query(u << 1 | 1,l,r);
    else {
    pii a = Query(u << 1,l,mid),b = Query(u << 1 | 1,mid + 1,r);
    if(a.fi > b.fi) swap(a,b);
    if(a.fi == b.fi) a.se += b.se;
    return a;
    }
}
void Solve() {
    read(N);read(M);
    build(1,1,LEN);
    for(int i = 1 ; i <= N ; ++i) {
    read(a[i]);
    a[i] += MAXV;
    add(1,a[i] - cnt[a[i]],a[i] - cnt[a[i]],1);
    cnt[a[i]]++;
    }
    int p,x;
    for(int i = 1 ; i <= M ; ++i) {
    read(p);read(x);
    if(p == 0) {
        if(x == 1) {
        if(cnt[getpos(N)]) {add(1,getpos(N) - cnt[getpos(N)] + 1,getpos(N),-1);}
        }
        else {
        if(cnt[getpos(N + 1)]) {add(1,getpos(N + 1) - cnt[getpos(N + 1)] + 1,getpos(N + 1),1);}
        }
        d += x;
    }
    else {
        if(a[p] <= getpos(N)) {
        add(1,a[p] - cnt[a[p]] + 1,a[p] - cnt[a[p]] + 1,-1);
        }
        cnt[a[p]]--;
        a[p] = x - d + MAXV;
        if(a[p] <= getpos(N)) {
        add(1,a[p] - cnt[a[p]],a[p] - cnt[a[p]],1);
        }
        cnt[a[p]]++;
    }
    pii res = Query(1,getpos(1),getpos(N));
    int ans = 0;
    if(res.fi == 0) ans = res.se;
    out(ans);enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

F - RNG and XOR

Title effect: a random number generator with a certain probability generation for each number \ (0 \) to \ (2 ^ {N} - 1 \) is an integer value, a initial X, each operating to generate a number \ (V \) , then \ (X = X \ oplus v \) that symbol is XOR, from Q \ (X-\) for the first time to \ - ([0,2 ^ {N } 1] \) the desired What is the number of steps

We can become a number from \ (I \) to (0 \) \ a desired number of steps, which is obviously equivalent
\ [x_ {i} = ( \ sum_ {j = 0} ^ {2 ^ {N } - {J}. 1} P_ X_ {J \ I} Oplus) +. 1 \]
EM, this form appears unhelpful

So we put a move past
\ [x_ {i} - 1
= \ sum_ {j = 0} ^ {2 ^ {N} - 1} p_ {j} x_ {j \ oplus i} \] So we can think of. . . FWT exclusive or convolutional
\ [(x_ {0}, x_1, x_2, x_3 \ cdots x_ {2 ^ {N} - 1}) \ bigoplus (p_ {0}, p_ {1}, p_ {2}, p_ {3} \ cdots p_ { 2 ^ {N} - 1}) = (, x_ {1} -? 1, x_ {2} - 1, x_ {3} - 1, \ cdots x_ {2 ^ {N } --1} --1) \]
? \ (\) should be what. . Is the sum should be the same before and also found, then \ (\?) Is \ (x_ {0} + 2 ^ {N} - 1 \)

So the equation becomes

\ [(X_ {0}, x_1, x_2, x_3 \ cdots x_ {2 ^ {N} - 1}) \ bigoplus (p_ {0}, p_ {1}, p_ {2}, p_ {3} \ cdots p_ {2 ^ {N} - 1}) = (x_ {0} + 2 ^ {N} -1, x_ {1} - 1, x_ {2} - 1, x_ {3} - 1, \ cdots x_ 2 {N} ^ {- 1} - 1) \]
EM, there is little skill, the \ (p_0 \) minus 1, it is easy to find the back of the variables we got of!
\ [(X_ {0}, x_1, x_2, x_3 \ cdots x_ {2 ^ {N} - 1}) \ bigoplus (p_ {0} - 1, p_ {1}, p_ {2}, p_ {3} \ cdots p_ {2 ^ {N
} - 1}) = (2 ^ {N} -1, - 1, - 1, - 1, \ cdots - 1) \] in this case, we seek a FWT exclusive or equivalent deconvolution, and this can achieve a similar recursive

But end it has a layer is
\ [x_ {0} + x_1 + x_2 + x_3 + \ cdots + x_ {2 ^ {N} - 1} \ bigoplus (p_ {0} - 1 + p_ {1} + p_ {2 } + p_ {3} + \
cdots + p_ {2 ^ {N} - 1}) = 0 \] at this time do not contribute, so we choose to each \ (X \) are plus a constant, and finally \ (x_ {0} = 0 \) to eliminate this constant, then this layer can return to an arbitrary positive number

#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 eps 1e-10
#define MAXN 2005
#define ba 47
//#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);
}
const int MOD = 998244353,inv2 = (MOD + 1) / 2;
int N,A[(1 << 18) + 5],S;
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
void update(int &x,int y) {
    x = inc(x,y);
}
int fpow(int x,int c) {
    int res = 1,t = x;
    while(c) {
    if(c & 1) res = mul(res,t);
    t = mul(t,t);
    c >>= 1;
    }
    return res;
}
vector<int> trans(vector<int> p,vector<int> m) {
    if(p.size() == 1) {
    if(p[0] == 0 && m[0] == 0) return (vector<int>){1};
    else return (vector<int>){mul(m[0],fpow(p[0],MOD - 2))};
    }
    vector<int> p0,p1,m0,m1;
    for(int i = 0 ; i < p.size() ; i += 2) {
    p0.pb(inc(p[i],p[i + 1]));
    p1.pb(inc(p[i],MOD - p[i + 1]));
    m0.pb(inc(m[i],m[i + 1]));
    m1.pb(inc(m[i],MOD - m[i + 1]));
    }
    p0 = trans(p0,m0);p1 = trans(p1,m1);
    vector<int> res;
    for(int i = 0 ; i < p0.size() ; ++i) {
    res.pb(mul(inc(p0[i],p1[i]),inv2));
    res.pb(mul(inc(p0[i],MOD - p1[i]),inv2));
    }
    return res;
}
void Solve() {
    read(N);
    for(int i = 0 ; i < (1 << N) ; ++i) {
    read(A[i]);
    update(S,A[i]);
    }
    S = fpow(S,MOD - 2);
    for(int i = 0 ; i < (1 << N) ; ++i) {
    A[i] = mul(A[i],S);
    }
    vector<int> a,b;
    b.pb((1 << N) - 1);
    for(int i = 1 ; i < (1 << N) ; ++i) b.pb(-1);
    for(int i = 0 ; i < (1 << N) ; ++i) {
    int t = A[i];
    if(i == 0) t = inc(t,MOD - 1);
    a.pb(t);
    }
    vector<int> ans = trans(a,b);
    for(int i = 0 ; i < (1 << N) ; ++i) {
    out(inc(ans[i],MOD - ans[0]));enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

Guess you like

Origin www.cnblogs.com/ivorysi/p/10984113.html