銆怉tCoder銆慉GC030

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怉tCoder銆慉GC030相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/pac' title='pac'>pac   double   second   tchar   鑰屼笖   type   浣嶇疆   ack   ace   

A - Poisonous Cookies

鏈夋瘨杩樺悆锛屾湁姣掑惂

#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 double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < 鈥?鈥?|| c > 鈥?鈥? {
        if(c == 鈥?鈥? f = -1;
        c = getchar();
    }
    while(c >= 鈥?鈥?&& c <= 鈥?鈥? {
        res = res * 10 + c - 鈥?鈥?
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(鈥?鈥?;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(鈥?鈥?+ x % 10);
}
int64 A,B,C;
void Solve() {
    read(A);read(B);read(C);
    out(B + min(C,A + B + 1));enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

B - Tree Burning

鎴戜滑鏋氫妇涓€涓柇鐐癸紝涔熷氨鏄墠(i)涓槸椤烘椂閽堣蛋鐨勶紝鍚?span class="math inline">(N - i)涓槸閫嗘椂閽堣蛋鐨?br> 鍙戠幇鐩稿綋浜庡乏杈归€夊悗t涓偣锛屽彸杈归€夊悗t + 1涓偣锛坱 <= i && t + 1 <= N - i锛?br> 锛堟垨鑰呭乏杈瑰悗t + 1涓紝鍙硉涓紝鍜岃繖涓儏鍐电被浼硷級
鐒跺悗杩欎簺鐐瑰埌鍘熺偣鐨勮窛绂讳箻浜岋紝鍐嶅噺鍘诲彸杈规渶鍚庝竴涓偣鍒板師鐐圭殑璺濈
鏄繖绉嶆儏鍐佃兘鍒拌揪鐨勬渶澶у€?/p>

#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 double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < 鈥?鈥?|| c > 鈥?鈥? {
        if(c == 鈥?鈥? f = -1;
        c = getchar();
    }
    while(c >= 鈥?鈥?&& c <= 鈥?鈥? {
        res = res * 10 + c - 鈥?鈥?
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(鈥?鈥?;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(鈥?鈥?+ x % 10);
}
int64 L;int N;
int64 a[MAXN],sum[2][MAXN];
void Solve() {
    read(L);read(N);
    for(int i = 1 ; i <= N ; ++i) read(a[i]);
    for(int i = 1 ; i <= N ; ++i) {
        sum[0][i] = sum[0][i - 1] + a[i];
    }
    for(int i = N ; i >= 1 ; --i) {
        sum[1][i] = sum[1][i + 1] + L - a[i];
    }
    int64 ans = max(L - a[1],a[N]);
    for(int i = 1 ; i <= N - 1; ++i) {
        int t = min(i,N - i);
        int64 res = 2 * (sum[0][i] - sum[0][i - t] + sum[1][i + 1] - sum[1][i + 1 + t]);
        int64 c = res - a[i];
        if(i > t) {c += 2 * a[i - t];}
        ans = max(ans,c);
        c = res - (L - a[i + 1]);
        if(N - i > t) c += 2 * (L - a[i + 1 + t]);
        ans = max(ans,c);
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

C - Coloring Torus

鎴戞瀯閫犳按骞充綆鍟娿€傘€傘€?br> 杩欓鏄鏋?span class="math inline">(K)鏄?鐨勫€嶆暟锛屽緢濂藉仛
灏辨槸杩欎箞濉紝濡傛灉鏄? * 4
鑰屼笖鏍囧彿璁や负鏄?span class="math inline">(0-K - 1)
0 1 2 3
5 6 7 4
2 3 0 1
7 4 5 6
濡傛灉瑕佸噺灏戜竴涓暟锛屾垜浠妸(i + n)閮芥浛鎹㈡垚(i)鍗冲彲銆傘€傘€?/p>

#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 double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < 鈥?鈥?|| c > 鈥?鈥? {
        if(c == 鈥?鈥? f = -1;
        c = getchar();
    }
    while(c >= 鈥?鈥?&& c <= 鈥?鈥? {
        res = res * 10 + c - 鈥?鈥?
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(鈥?鈥?;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(鈥?鈥?+ x % 10);
}
int K;
int a[505][505],N;
void Solve() {
    read(K);
    if(K == 1) {
        puts("1");puts("1");return;
    }
    for(int i = 2 ; i <= 500 ; i += 2) {
        if(2 * i >= K) {N = i;break;}
    }
    for(int i = 0 ; i < N ; ++i) {
        for(int j = 0 ; j < N ; ++j) {
            if(i & 1) a[i][j] = ((i + j) % N) + N;
            else a[i][j] = (i + j) % N;
        }
    }
    int p = N - 1;
    while(K < 2 * N) {
        for(int i = 0 ; i < N ; ++i) {
            for(int j = 0 ; j < N ; ++j) {
                if(a[i][j] == p + N) a[i][j] = p;
            }
        }
        --p;
        ++K;
    }
    out(N);enter;
    for(int i = 0 ; i < N ; ++i) {
        for(int j = 0 ; j < N ; ++j) {
            out(a[i][j] + 1);space;
        }
        enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

D - Inversion Sum

杩欓濂界浠欏晩QAQ
瀵逛簬杩欑涓€鑴歌鏁扮殑棰橈紝sd閫夋墜鍙細涓婂幓鎯宠鏁帮紝鐒惰€岃繖棰樿浆鎴愭鐜囧仛锛岄潪甯稿
(dp[t][i][j])琛ㄧず绗?span class="math inline">(t)娆℃搷浣滅(i)涓綅缃ぇ浜庣(j)涓綅缃殑鏂规鏁?br> 姣忔杞Щ褰卞搷4n涓綅缃?br> 鐒跺悗鐩存帴缁熻鏈熸湜锛屾渶鍚庝箻涓?span class="math inline">(2^Q)灏辨槸绛旀

#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 double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < 鈥?鈥?|| c > 鈥?鈥? {
        if(c == 鈥?鈥? f = -1;
        c = getchar();
    }
    while(c >= 鈥?鈥?&& c <= 鈥?鈥? {
        res = res * 10 + c - 鈥?鈥?
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(鈥?鈥?;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(鈥?鈥?+ x % 10);
}
const int MOD = 1000000007;
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 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;
}
int N,Q,Inv2;
int dp[3005][3005],f[3005][3005];
int A[3005];
void Solve() {
    Inv2 = (MOD + 1) / 2;
    read(N);read(Q);
    for(int i = 1 ; i <= N ; ++i) read(A[i]);
    for(int i = 1 ; i <= N ; ++i) {
        for(int j = 1 ; j <= N ; ++j) {
            if(A[i] > A[j]) dp[i][j] = 1;
        }
    }
    int x,y;
    for(int i = 1 ; i <= Q ; ++i) {
        read(x);read(y);
        for(int j = 1 ; j <= N ; ++j) {
            f[x][j] = dp[x][j];
            f[j][x] = dp[j][x];
            f[j][y] = dp[j][y];
            f[y][j] = dp[y][j];
        }
        for(int j = 1 ; j <= N ; ++j) {
            if(j != x && j != y)
            dp[x][j] = mul(Inv2,inc(f[x][j],f[y][j]));
            dp[j][x] = mul(Inv2,inc(f[j][x],f[j][y]));
            dp[y][j] = mul(Inv2,inc(f[y][j],f[x][j]));
            dp[j][y] = mul(Inv2,inc(f[j][y],f[j][x]));
        }
        dp[x][y] = mul(Inv2,inc(f[x][y],f[y][x]));
        dp[y][x] = mul(Inv2,inc(f[y][x],f[x][y]));
    }
    int ans = 0;
    for(int i = 1 ; i <= N ; ++i) {
        for(int j = i + 1 ; j <= N ; ++j) {
            ans = inc(ans,dp[i][j]);
        }
    }
    ans = mul(ans,fpow(2,Q));
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

E - Less than 3

棰樿В鐨勫浘鐢荤殑鎸哄ソ鐨勶紝灏变笉鐓ф惉浜?br> 灏辨槸鍦?span class="math inline">(01)鍜?span class="math inline">(10)涔嬮棿鐢讳竴鏉$嚎锛屽彂鐜板氨鏄繖浜涚嚎涓嶅仠鐨勭Щ鍔紝鎴戜滑鏋氫妇鏌愭潯绾垮拰鍝潯绾垮尮閰嶏紝璁╁悗寰€涓よ竟鎵╁睍锛屾渶澶氬尮閰嶅氨N绉?/p>

#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 double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < 鈥?鈥?|| c > 鈥?鈥? {
        if(c == 鈥?鈥? f = -1;
        c = getchar();
    }
    while(c >= 鈥?鈥?&& c <= 鈥?鈥? {
        res = res * 10 + c - 鈥?鈥?
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(鈥?鈥?;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(鈥?鈥?+ x % 10);
}
const int MOD = 1000000007;
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 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;
}
int N,st[2],ed[2];
char s[5005],t[5005];
vector<int> a,b;
void Init() {
    read(N);
    scanf("%s%s",s + 1,t + 1);
    for(int i = 1 ; i <= 10010 ; ++i) {
        a.pb(0);b.pb(0);
    }
    if(s[1] == 鈥?鈥? a.pb(0);
    if(t[1] == 鈥?鈥? b.pb(0);
    st[0] = a.size() - 1;st[1] = b.size() - 1;
    for(int i = 1 ; i < N ; ++i) {
        if(s[i] != s[i + 1]) a.pb(i);
        if(t[i] != t[i + 1]) b.pb(i);
    }
    ed[1] = b.size();
    for(int i = 1 ; i <= 10010 ; ++i) {
        a.pb(N);b.pb(N);
    }
}
void Solve() {
    int p = st[0] - 2,q = ed[1] + 2;
    if((p & 1) != (q & 1)) ++q;
    int ans = N * N;
    while(q >= st[1] - N - 2) {
        int t0 = p + 1,t1 = q + 1;
        int res = abs(a[p] - b[q]);
        while(a[t0] != N || b[t1] != N) {
            res += abs(a[t0] - b[t1]);
            ++t0;++t1;
        }
        t0 = p - 1,t1 = q - 1;
        while(a[t0] != 0 || b[t1] != 0) {
            res += abs(a[t0] - b[t1]);
            --t0;--t1;
        }
        ans = min(ans,res);
        q -= 2;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
}

F - Permutation and Minimum

鐪嬪嚭鏉ョ被浼间竴涓崱鐗瑰叞鏁扮殑鍓嶅悗鍖归厤锛屽彂鐜版鐫€鍋氬氨鏄笉琛岋紝缁撴灉鍙嶇潃鎺ㄥ氨瀵逛簡銆傘€傘€傜湅鏉ユ鎺ㄥ鏉傚害鍫咖灏卞緱璇曡瘯鍙嶇潃
鏄剧劧鍙互鎶婁袱涓綅缃兘濉笂鏁扮殑浣嶇疆鍏ㄩ儴鍒犳帀锛屼笉褰卞搷绛旀
鐜板湪榛樿浠绘剰(2i,2i+1)涓や釜浣嶇疆娌℃湁濉笂鏁?br> 璁句竴瀵逛綅缃兘鏄┖鐨勪釜鏁版槸(cnt)锛屾垜浠绠楁暟浜掍笉鐩稿悓鐨刡搴忓垪鏈夊涓紝绛旀鍐嶄箻涓?span class="math inline">(cnt!)
鐒跺悗(f[n][j][k])琛ㄧず浠?N鍒皀锛屾湁j涓病鏈夊湪缁欏嚭鐨凙搴忓垪閲岀殑鏁板嚭鐜拌繃鐨勬暟琚挦瀹氭垚浜嗚緝澶х殑涓€鏂癸紝鏈塳涓湪A搴忓垪閲屽嚭鐜拌繃鐨勬暟琚挦瀹氭垚浜嗚緝澶х殑涓€鏂?br> 杞Щ鐨勮瘽锛屽鏋?span class="math inline">(n)鍦ˋ涓嚭鐜拌繃灏辨槸
(f[n][j][k + 1] leftarrow f[n + 1][j][k])
(f[n][j - 1][k] leftarrow f[n + 1][j][k])
濡傛灉(n)娌″嚭鐜拌繃灏辨槸
(f[n][j][k - 1] leftarrow f[n + 1][j][k] * k)
(f[n][j + 1][k] leftarrow f[n + 1][j][k])
(f[n][j - 1][k] leftarrow f[n + 1][j][k])

#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 double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < 鈥?鈥?|| c > 鈥?鈥? {
        if(c == 鈥?鈥? f = -1;
        c = getchar();
    }
    while(c >= 鈥?鈥?&& c <= 鈥?鈥? {
        res = res * 10 + c - 鈥?鈥?
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(鈥?鈥?;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(鈥?鈥?+ x % 10);
}
const int MOD = 1000000007;
int N,dp[2][605][605],C[606][606],fac[606];
int A[605],cnt;
bool vis[605],used[605];
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);
}
void Init() {
    read(N);
    for(int i = 1 ; i <= 2 * N ; ++i) read(A[i]);
    cnt = 0;
    for(int i = 1 ; i <= N ; ++i) {
        if(A[2 * i] == -1 && A[2 * i - 1] == -1) ++cnt;
        if(A[2 * i] != -1 && A[2 * i - 1] != -1) {
            vis[A[2 * i]] = vis[A[2 * i - 1]] = 1;
        }
        else if(A[2 * i] != -1) used[A[2 * i]] = 1;
        else if(A[2 * i - 1] != -1) used[A[2 * i - 1]] = 1;
    }
    C[0][0] = 1;
    for(int i = 1 ; i <= 2 * N ; ++i) {
        C[i][0] = 1;
        for(int j = 1 ; j <= i ; ++j) {
            C[i][j] = inc(C[i - 1][j - 1],C[i - 1][j]);
        }
    }
    fac[0] = 1;
    for(int i = 1 ; i <= 2 * N ; ++i) fac[i] = mul(fac[i - 1],i);
}
void Solve() {
    int cur = 0;
    dp[0][0][0] = 1;
    for(int i = 2 * N ; i >= 1 ; --i) {
        if(vis[i]) continue;
        memset(dp[cur ^ 1],0,sizeof(dp[cur ^ 1]));
        int t = (2 * N) - i;
        if(used[i]) {
            for(int j = 0 ; j <= t ; ++j) {
                for(int k = 0 ; k <= t - j ; ++k) {
                    if(dp[cur][j][k]) {
                        update(dp[cur ^ 1][j][k + 1],dp[cur][j][k]);
                        if(j) update(dp[cur ^ 1][j - 1][k],dp[cur][j][k]);
                    }
                }
            }
        }
        else {
            for(int j = 0 ; j <= t ; ++j) {
                for(int k = 0 ; k <= t - j ; ++k) {
                    if(dp[cur][j][k]) {
                        update(dp[cur ^ 1][j + 1][k],dp[cur][j][k]);
                        if(k) update(dp[cur ^ 1][j][k - 1],mul(k,dp[cur][j][k]));
                        if(j) update(dp[cur ^ 1][j - 1][k],dp[cur][j][k]);
                    }
                }
            }
        }
        cur ^= 1;
    }
    int ans = dp[cur][0][0];
    ans = mul(ans,fac[cnt]);
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
}




















以上是关于銆怉tCoder銆慉GC030的主要内容,如果未能解决你的问题,请参考以下文章

銆怉sphyre寮曟搸銆戝彂甯冧簡鏂扮増鏈琕101

銆怉OP tips銆慡tand on Giants' Shoulders

銆愯浆銆戙€怱alesfoece銆慉pproval Process 鍦?Apex 涓殑浣跨敤

UOJ266. 銆愭竻鍗庨泦璁?016銆慉lice鍜孊ob鍙堝湪鐜╂父鎴?鍗氬紙璁?01-trie)

Unity2019 增量式GC(使用时间片段执行GC,减少卡顿)

Unity2019 增量式GC(使用时间片段执行GC,减少卡顿)