【AtCoder】AGC011

A - Ice Tea Store

算一下每种零售最少的钱就行,然后优先买2,零头买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 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 Q,H,S,D,N;
int64 ans;
int main() {
    read(Q);read(H);read(S);read(D);
    read(N);
    H = min(2 * Q,H);
    S = min(2 * H,S);
    D = min(2 * S,D);
    ans = 1LL * (N / 2) * D + (N & 1) * S;
    out(ans);enter;
}

B - Reverse and Compare

对于两个相同的字符,在这个字符之间的翻转肯定能取代以这两个字符为端点的翻转

那么如果认为一个翻转会形成一个字符串,那么我们删掉左右两端点相同的翻转

#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);
}
int64 ans;
int N,L,cnt[30];
char s[MAXN];
void Solve() {
    scanf("%s",s + 1);
    N = strlen(s + 1);
    ans = 1 + 1LL * N * (N - 1) / 2;
    for(int i = 1 ; i <= N ; ++i) {
        cnt[s[i] - 'a']++;
    }
    for(int i = 0 ; i <= 25 ; ++i) {
        ans -= 1LL * cnt[i] * (cnt[i] - 1) / 2;
    }
    out(ans);enter;
}

int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

C - Fountain Walk

以横坐标上升对y求一个最长上升子序列

设长度为\(k\)

如果\(k < min(x_2 - x_1,y_2 - y_1) + 1\)

那么我就可以拐角k次,减少\((20 - 5\pi)k\)

否则我只能拐角k - 1次,不得不走一个半圆,减少\((20 - 5\pi)(k - 1)\)加上\((10\pi - 20)\)

#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);
}
const double PI = acos(-1.0);
int64 x[MAXN],y[MAXN],id[MAXN];
int64 X1,X2,Y1,Y2,num[MAXN],val[MAXN];
int N,tot,c;
void Solve() {
    read(X1);read(Y1);read(X2);read(Y2);
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(x[i]);read(y[i]);
    }
    if(X1 > X2) {
        swap(X1,X2);swap(Y1,Y2);
    }
    if(Y1 > Y2) {
        swap(Y1,Y2);
        for(int i = 1 ; i <= N ; ++i) {
            y[i] = Y1 + Y2 - y[i];
        }
    }
    for(int i = 1 ; i <= N ; ++i) {
        id[i] = i;
    }
    sort(id + 1,id + N + 1,[](int a,int b){return y[a] < y[b];});
    for(int i = 1 ; i <= N ; ++i) {
        int u = id[i];
        if(y[u] >= Y1 && y[u] <= Y2 && x[u] >= X1 && x[u] <= X2) {
            num[++tot] = x[u];
        }
    }
    c = 0;
    val[c] = -1;
    for(int i = 1 ; i <= tot ; ++i) {
        if(num[i] > val[c]) val[++c] = num[i];
        else {
            int t = upper_bound(val + 1,val + c + 1,num[i]) - val;
            val[t] = num[i];
        }
    }
    if(c < min(X2 - X1,Y2 - Y1) + 1) {
        double ans = 100.0 * (X2 - X1 + Y2 - Y1) - (20 - PI * 5) * c;
        printf("%.12lf\n",ans);
    }
    else {
        double ans = 100.0 * (X2 - X1 + Y2 - Y1) - (20 - PI * 5) * (c - 1) + (10 * PI - 20);
        printf("%.12lf\n",ans);
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

D - Shift and Flip

枚举B的第一位和A的哪一位对齐

然后对于A的每一位1,如果对着的B不是1,那么我们想办法把它消成0,这找这个1最近的两个1,这分别对应着两个和B第一位对齐的位置,放在数轴上

然后用twopoints,枚举当左边选择数轴上的点在这的时候,右边最远选到哪,计算两种走法,然后计算端点到目标点的距离

#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 4005
#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 L[MAXN],R[MAXN],N;
char a[MAXN],b[MAXN];
vector<int> v[MAXN];
int cnt[MAXN];
bool checkpos(int x) {
    if(x >= N) return false;
    for(auto t : v[x]) {
        cnt[t]--;
    }
    bool flag = 1;
    for(auto t : v[x]) if(!cnt[t]) flag = 0;
    for(auto t : v[x]) cnt[t]++;
    return flag;
}
void Solve() {
    scanf("%s%s",a,b);
    N = strlen(a);
    int ca = 0,cb = 0;
    for(int i = 0 ; i < N ; ++i) {
        if(a[i] == '1') ++ca;
        if(b[i] == '1') ++cb;
    }
    if(!cb) {
        if(!ca) puts("0");
        else puts("-1");
        return;
    }
    for(int i = 0 ; i < N ; ++i) {
        if(a[i] == '1') {
            int t = i;
            int cnt = 0;
            while(b[t] == '0') {
                t = (t - 1 + N) % N;
                ++cnt;
            }
            L[i] = cnt % N;
            t = i,cnt = 0;
            while(b[t] == '0') {
                t = (t + 1) % N;
                ++cnt;
            }
            R[i] = (N - cnt) % N;
        }
    }
    for(int i = 0 ; i < N ; ++i) a[i + N] = a[i];
    int ans = 1e9;
    for(int i = 0 ; i < N ; ++i) {
        int now = 0;
        for(int j = 0 ; j < N ; ++j) v[j].clear();
        memset(cnt,0,sizeof(cnt));
        for(int j = 0 ; j < N ; ++j) {
            if(b[j] != a[i + j]) {
                ++now;
                if(a[i + j] == '1') {
                    int t = (i + j) % N;
                    v[L[t]].pb(t);v[R[t]].pb(t);
                    cnt[t] += 2;
                }
            }
        }
        int p = 0,q = 1;
        while(p < N) {
            while(checkpos(q)) {
                for(auto t : v[q]) cnt[t]--;
                ++q;
            }
            int tmp = p + 2 * (N - q);
            tmp += min(abs(p - i),N - abs(p - i));
            ans = min(ans,tmp + now);
            tmp = N - q + 2 * p;
            tmp += min(abs(q - i),N - abs(q - i));
            ans = min(ans,tmp + now);
            ++p;
            for(auto t : v[p]) cnt[t]++;
        }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

E - Shuffle and Swap

就是对于01 和 10这两种序列配对,然后可以插任意个11进去

假如有d个01,总共k个1

这个方案数就是\((\frac{1}{1!}x^{1} + \frac{1}{2!}x^{2}.... + \frac{1}{k!}x^{k})^d\)

然后记\(f(i)\)是系数,那么方案数就是\(f(i) d!(k - d)!k!\),对于每个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 10005
#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);
}
const int MOD = 998244353,MAXL = (1 << 16);
char a[MAXN],b[MAXN];
int N,k,d,fac[MAXN],invfac[MAXN],W[MAXL];
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;
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
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;
}
struct poly {
    vector<int> v;
    void limit(int n = -1) {
        if(n != -1) v.resize(n);
        while(v.size() > 1 && (v.back()) == 0) v.pop_back();
    }
    friend void NTT(poly &f,int L,int on) {
        f.v.resize(L);
        for(int i = 1, j = L >> 1 ; i < L - 1 ; ++i) {
            if(i < j) swap(f.v[i],f.v[j]);
            int k = L >> 1;
            while(j >= k) {
                j -= k;
                k >>= 1;
            }
            j += k;
        }
        for(int h = 2 ; h <= L ; h <<= 1) {
            int wn = W[(MAXL + on * MAXL / h) % MAXL];
            for(int k = 0 ; k < L ; k += h) {
                int w = 1;
                for(int j = k ; j < k + h / 2 ; ++j) {
                    int u = f.v[j],v = mul(w,f.v[j + h / 2]);
                    f.v[j] = inc(u,v);
                    f.v[j + h / 2] = inc(u,MOD - v);
                    w = mul(w,wn);
                }
            }
        }
        if(on == -1) {
            int invL = fpow(L,MOD - 2);
            for(int i = 0 ; i < L ; ++i) {
                f.v[i] = mul(f.v[i],invL);
            }
        }
    }
    friend poly operator * (poly a,poly b) {
        poly c;c.v.clear();
        int s = a.v.size() + b.v.size();
        int L = 1;
        while(L <= s) L <<= 1;
        NTT(a,L,1);NTT(b,L,1);
        for(int i = 0 ; i < L ; ++i) {
            c.v.pb(mul(a.v[i],b.v[i]));
        }
        NTT(c,L,-1);
        return c;
    }
    friend poly fpow(const poly &a,int c,int n) {
        poly res,t = a;
        res.v.clear();res.v.pb(1);
        while(c) {
            if(c & 1) {
                res = res * t;
                res.limit(n);
            }
            t = t * t;t.limit(n);
            c >>= 1;
        }
        return res;
    }
}f;
void Solve() {
    scanf("%s%s",a + 1,b + 1);
    N = strlen(a + 1);
    for(int i = 1 ; i <= N ; ++i) {
        if(a[i] == '1') {
            ++k;
            if(b[i] == '0') ++d;
        }
    }
    fac[0] = 1;

    for(int i = 1 ; i <= N ; ++i) {
        fac[i] = mul(fac[i - 1],i);
    }
    invfac[N] = fpow(fac[N],MOD - 2);
    for(int i = N - 1 ; i >= 0 ; --i) {
        invfac[i] = mul(invfac[i + 1],i + 1);
    }
    W[0] = 1;W[1] = fpow(3,(MOD - 1) / MAXL);
    for(int i = 2 ; i < MAXL ; ++i) W[i] = mul(W[i - 1],W[1]);
    f.v.clear();
    f.v.resize(N);
    for(int i = 1 ; i <= k ; ++i) {
        f.v[i] = invfac[i];
    }
    f = fpow(f,d,N);
    int ans = 0;
    for(int i = d ; i <= k ; ++i) {
        if(i > f.v.size() - 1) break;
        int t = mul(f.v[i],mul(fac[i],fac[d]));
        t = mul(t,C(k,i));
        t = mul(t,mul(fac[k - d],fac[k - i]));
        ans = inc(ans,t);
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

F - Yes or No

本来是个组合数前缀和,但是根据递推只需要改一项???

这坐标算的也太难受了,不想说话了。。。

#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 1000005
#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);
}
const int MOD = 998244353;
int N,M;
int fac[MAXN],invfac[MAXN],f[MAXN],v[MAXN],h[MAXN],inv[MAXN];
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;
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int way(int x1,int y1,int x2,int y2) {
    return C(x2 - x1 + y2 - y1,y2 - y1);
}
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;
}
void Solve() {
    read(N);read(M);
    fac[0] = 1;
    inv[1] = 1;
    for(int i = 1 ; i <= N + M ; ++i) {
        fac[i] = mul(fac[i - 1],i);
        if(i > 1) inv[i] = mul(inv[MOD % i],MOD - MOD / i);
    }
    invfac[N + M] = fpow(fac[N + M],MOD - 2);
    for(int i = N + M - 1 ; i >= 0 ; --i) {
        invfac[i] = mul(invfac[i + 1],i + 1);
    }
    if(M > N) h[0] = 1;
    if(N > M) v[0] = 1;
    for(int i = 1 ; i <= N + M ; ++i) {
        h[i] = h[i - 1];
        int r = min(i - 1,N) - h[i - 1] + 1,c = i - 1 - r + 1;
        if(i <= N && M - (N - r) >= c + 1) ++h[i];
        if(i > N && M - (N - r) <= c) --h[i];
        v[i] = v[i - 1];
        c = min(i - 1,M) - v[i - 1] + 1,r = i - 1 - c + 1;
        if(i <= M && N - (M - c) >= r + 1) ++v[i];
        if(i > M && N - (M - c) <= r) --v[i];
    }
    f[0] = mul(C(N + M, N),max(N,M));
    int valh = 0,valv = 0;
    if(h[0]) valh = C(N + M - 1, M - 1);
    if(v[0]) valv = C(N + M - 1, N - 1);
    for(int i = 1 ; i < N + M ; ++i) {
        f[i] = f[i - 1];
        if(h[i - 1]) f[i] = inc(f[i],MOD - valh);
        if(v[i - 1]) f[i] = inc(f[i],MOD - valv);
        if(!h[i - 1] && h[i]) valh = way(i,0,N,M - 1);
        if(!v[i - 1] && v[i]) valv = way(0,i,N - 1,M);
        int r1 = min(i - 1,N) - h[i - 1] + 1,c1 = i - 1 - r1;
        int r2 = min(i,N) - h[i] + 1,c2 = i - r2;
        if(h[i - 1]) {
            if(r2 > r1) {
                valh = inc(valh, MOD - mul(way(0,0,r1,c1),way(r1,c1 + 1,N,M - 1)));
            }
            else {
                if(r2 >= 1) valh = inc(valh,mul(way(0,0,r2 - 1,c2),way(r2,c2,N,M - 1)));
            }
        }
        c1 = min(i - 1,M) - v[i - 1] + 1,r1 = i - 1 - c1;
        c2 = min(i,M) - v[i] + 1,r2 = i - c2;
        if(v[i - 1]) {
            if(c2 > c1) {
                valv = inc(valv, MOD - mul(way(0,0,r1,c1),way(r1 + 1,c1,N - 1,M)));
            }
            else {
                if(c2 >= 1) valv = inc(valv,mul(way(0,0,r2,c2 - 1),way(r2,c2,N - 1,M)));
            }
        }
    }
    int ans = 0;
    for(int i = 0 ; i < N + M ; ++i) {
        ans = inc(ans,mul(f[i],inv[N + M - i]));
    }
    ans = mul(ans,fpow(C(N + M,N),MOD - 2));
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

猜你喜欢

转载自www.cnblogs.com/ivorysi/p/10420689.html
今日推荐