日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【AtCoder】AGC017

發布時間:2023/12/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【AtCoder】AGC017 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

A - Biscuits

dp[i][0/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 1000005 #define eps 1e-10 //#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; int a[55]; int64 dp[55][2]; void Solve() {read(N);read(P);for(int i = 1 ; i <= N ; ++i) read(a[i]);dp[0][0] = 1;for(int i = 1 ; i <= N ; ++i) {int k = a[i] & 1;dp[i][0] = dp[i - 1][0];dp[i][1] = dp[i - 1][1];dp[i][k ^ 0] += dp[i - 1][0];dp[i][k ^ 1] += dp[i - 1][1];}out(dp[N][P]);enter; } int main() { #ifdef ivorysifreopen("f1.in","r",stdin); #endifSolve(); }

B - Moderate Differences

A和B差值在\(N \times C\)\((N-1) \times D\)之間肯定能達到
因為下降操作和上升操作差不多,那么我們默認所有的下降操作都在前面
容易發現,當我們進行\(i\)次至少為\(C\)的下降后
\(-i \times C + (N - 1 - i) \times C\)\(-i \times C + (N - 1 - i) \times D\)之間的也可以達到
且這樣能構造最多的重合的區間

#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-10 //#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; int64 A,B,C,D; bool check(int64 a,int64 b) {return B - A >= a && B - A <= b; } void Solve() {read(N);read(A);read(B);read(C);read(D);for(int i = 0 ; i <= N - 1 ; ++i) {int64 u = (N - 1 - i) * D - C * i,d = (N - 1 - i) * C - C * i;if(check(d,u)) {puts("YES");return;}u = C * i - (N - 1 - i) * C,d = C * i - (N - 1 - i) * D;if(check(d,u)) {puts("YES");return;}}puts("NO"); } int main() { #ifdef ivorysifreopen("f1.in","r",stdin); #endifSolve(); }

C - Snuke and Spells

假如\(i\)出現了\(A[i]\)次,那么覆蓋\([i - A[i],i]\)這個區間,然后找\([0,L]\)沒有被覆蓋的區間長度,就是答案
可以\(O(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 1000005 #define eps 1e-10 //#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,M; int A[MAXN]; int cnt[MAXN],cov[MAXN],ans; void Solve() {read(N);read(M);for(int i = 1 ; i <= N ; ++i) {read(A[i]);cnt[A[i]]++;cov[A[i] - cnt[A[i]] + 1]++;}for(int i = 1 ; i <= N ; ++i) {if(!cov[i]) ++ans;}int x,y;for(int i = 1 ; i <= M ; ++i) {read(x);read(y);if(A[x] - cnt[A[x]] + 1 >= 1) {if(!--cov[A[x] - cnt[A[x]] + 1]) ++ans;}--cnt[A[x]];A[x] = y;++cnt[A[x]];if(A[x] - cnt[A[x]] + 1 >= 1) {if(!cov[A[x] - cnt[A[x]] + 1]++) --ans;}out(ans);enter;} } int main() { #ifdef ivorysifreopen("f1.in","r",stdin); #endifSolve(); }

D - Game on Tree

一個點的sg函數值顯然是0,而一個樹加一條邊一個點的sg函數值是這棵樹的sg函數值+1

證明,設新加的邊為\(u,v\)\(u\)\(v\)的祖先,若斷掉新加的邊,則sg值是0

否則斷樹中的邊,sg函數值為\([0,sg[v] - 1]\),從小到大取,使得\(v\)為根游戲狀態為0的那條邊,斷了之后,由于斷掉新邊游戲狀態是0,則這個狀態給\(u\)為根的貢獻是\(1\)

使得\(v\)為根貢獻為1的斷邊狀態,子游戲中斷邊為0的狀態可以轉化為1,再填上斷掉新邊的狀態是0,則這個對\(u\)的貢獻變成了2

以此類推,這種情況游戲狀態就是\(sg[v] + 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-10 //#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; struct node {int to,next; }E[MAXN * 2]; int sumE,sg[MAXN],head[MAXN]; void add(int u,int v) {E[++sumE].to = v;E[sumE].next = head[u];head[u] = sumE; } void dfs(int u,int fa) {sg[u] = 0;for(int i = head[u]; i ; i = E[i].next) {int v = E[i].to;if(v != fa) {dfs(v,u);sg[u] ^= (sg[v] + 1);}} } void Solve() {read(N);int x,y;for(int i = 1 ; i < N ; ++i) {read(x);read(y);add(x,y);add(y,x);}dfs(1,0);if(sg[1]) puts("Alice");else puts("Bob"); } int main() { #ifdef ivorysifreopen("f1.in","r",stdin); #endifSolve(); }

E - Jigsaw

把連在地面上的高度設為正數,不連在地面上的高度設為負數
然后一個點相當于一條邊\((l,r)\)
相當于把整個圖拆成負數點到正數點的若干路徑
只要判斷一個弱聯通圖,負數點是否全為度數是否全負,正數點度數是否全正,然后至少有一個點點度不為0
證明就是歐拉回路,兩兩匹配負數點和正數點,一定會有歐拉回路,斷掉這些邊就是答案

#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); } int g[405][405],f[405][405],ind[405],H,col[405],all; int N,A[100005],B[100005],C[100005],D[100005],cnt; bool vis[405],flag,has[405]; void dfs(int u) {all += ind[u];if(ind[u]) flag = 1;vis[u] = 1;for(int i = 1 ; i <= 2 * H ; ++i) {if(f[u][i] && !vis[i]) dfs(i);} } void Solve() {read(N);read(H);for(int i = 1 ; i <= N ; ++i) {read(A[i]);read(B[i]);read(C[i]);read(D[i]);int s,t;if(C[i]) s = C[i];else s = A[i] + H;if(D[i]) t = D[i] + H;else t = B[i];g[s][t]++;ind[t]++;ind[s]--;f[s][t]++;f[t][s]++;has[s] = has[t] = 1;}for(int i = 1 ; i <= H ; ++i) {if(ind[i] < 0) {puts("NO");return;}}for(int i = H + 1 ; i <= 2 * H ; ++i) {if(ind[i] > 0) {puts("NO");return;}}for(int i = 1 ; i <= 2 * H ; ++i) {if(!has[i]) continue;if(!vis[i]) {flag = 0;dfs(i);if(all != 0 || !flag) {puts("NO");return;}}}puts("YES"); } int main() { #ifdef ivorysifreopen("f1.in","r",stdin); #endifSolve(); }

F - Zigzag

dp[i][j][mask]表示第i條邊走了第j步,左邊界是什么
根據限制和每一步的選擇修改左邊界即可
復雜度\(O(n^{2}2^{n})\)

#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 int MOD = 1000000007; int N,M,K; int A[405],B[405],C[405],w[25][25]; int dp[2][21][(1 << 19) + 5]; 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 lowbit(int x) {return x & (-x); } void Solve() {read(N);read(M);read(K);memset(w,-1,sizeof(w));for(int i = 1 ; i <= K ; ++i) {read(A[i]);read(B[i]);read(C[i]);w[A[i]][B[i]] = C[i];}int cur = 0;dp[0][1][0] = 1;for(int i = 1 ; i <= M ; ++i) {for(int j = 1 ; j < N ; ++j) {for(int s = 0 ; s < (1 << N - 1) ; ++s) {if(!dp[cur][j][s]) continue;if(w[i][j] != 1 && !(s >> (j - 1) & 1)) update(dp[cur][j + 1][s],dp[cur][j][s]);if(w[i][j] != 0) {if(s >> (j - 1) & 1) update(dp[cur][j + 1][s],dp[cur][j][s]);else {int t = s - (s & (1 << j) - 1);t -= t & (-t);t ^= (1 << j - 1);t += s & (1 << j) - 1;update(dp[cur][j + 1][t],dp[cur][j][s]);}}}}memset(dp[cur ^ 1],0,sizeof(dp[cur ^ 1]));for(int s = 0 ; s < (1 << N - 1) ; ++s) dp[cur ^ 1][1][s] = dp[cur][N][s];cur ^= 1;}int ans = 0;for(int s = 0 ; s < (1 << N - 1) ; ++s) ans = inc(ans,dp[cur][1][s]);out(ans);enter; } int main() { #ifdef ivorysifreopen("f1.in","r",stdin); #endifSolve(); }

轉載于:https://www.cnblogs.com/ivorysi/p/10554163.html

總結

以上是生活随笔為你收集整理的【AtCoder】AGC017的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。