版本比较
雅礼兴趣小组总结-3
正文
增加 609 行删除 0 行
266 行未改动全部展开267268269270266 行未改动全部展开cout << sum;return 0;}```266 行未改动全部展开267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879266 行未改动全部展开cout << sum;return 0;}```:::## 新课### [P3367 【模板】并查集](luogu.com.cn/problem/P3367)并查集模版:::success[代码]{open}```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int fa[200005] , f[200005];int n , m , z , x , y;int find (int x){if (fa[x]==x) return x;return fa[x]=find (fa[x]);}signed main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i=1;i<=n;i++){fa[i]=i;f[i]=i;}while (m--){cin >> z >> x >> y;int dx=find (x) , dy=find (y);if (z==1){if (dx==dy) continue;fa[dy]=dx;}else{if (f[dx]==f[dy]){cout << "Y\n";}else{cout << "N\n";}}}return 0;}```:::### [Knight Tournament](codeforces.com/problemset/problem/356/A)求每个人被谁击败了,没有输出$0$。用两个数组,一个记录并查集,一个记录被谁击败了即可:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;int fa[300005] , ans[300005];int n , m , l , r , x;int find (int x){if (x==fa[x]) return x;return fa[x]=find (fa[x]);}int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i=1;i<=n+1;i++){fa[i]=i;}while (m--){cin >> l >> r >> x;for (int i=find (l);i<=r;i=find (i+1)){if (i!=x){fa[i]=i+1;ans[i]=x;}}}for (int i=1;i<=n;i++){cout << ans[i] << " ";}return 0;}```:::### [P1196 [NOI2002] 银河英雄传说](luogu.com.cn/problem/P1196)用两个数组记录并查集和是序列的第几个,再用距离相减即可:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;const int n=3e4;int fa[n+5];int dis[n+5] , cnt[n+5];int m , x , y;char k;int find (int x){if (fa[x]==x) return x;int tmp=fa[x];fa[x]=find (fa[x]);dis[x]+=dis[tmp];return fa[x];}int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> m;for (int i=1;i<=n;i++){fa[i]=i;cnt[i]=1;}while (m--){cin >> k >> x >> y;if (k=='M'){int nx=find (x) , ny=find (y);if (nx==ny) continue;dis[nx]=cnt[ny];cnt[ny]+=cnt[nx];fa[nx]=ny;}else{int nx=find (x) , ny=find (y);if (nx==ny){cout << abs (dis[y]-dis[x])-1 << "\n";}else{cout << -1 << "\n";}}}return 0;}```:::### [P2024 [NOI2001] 食物链](luogu.com.cn/problem/P2024)是同类就`merge (x , y);merge (x+n , y+n);merge (x+n*2 , y+n*2);`,是敌人就`merge (x , y+n);merge (x+n , y+n*2);merge (x+n*2 , y);`,最后看一下是否为同一类:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;int fa[150005];int n , m , k , x , y , cnt=0;int find (int x){if (fa[x]==x) return x;return fa[x]=find (fa[x]);}void merge (int a , int b){int x=find (a) , y=find (b);if (x==y) return;if (x>y) swap (x , y);fa[y]=x;}int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i=1;i<=n*3;i++){fa[i]=i;}while (m--){cin >> k >> x >> y;if ((k==2&&x==y)||x>n||y>n){cnt++;continue;}if (k==1){if (find (x)==find (y+n)||find (x+n)==find (y)){cnt++;continue;}merge (x , y);merge (x+n , y+n);merge (x+n*2 , y+n*2);}else{if (find (x)==find (y)||find (x)==find (y+n*2)){cnt++;continue;}merge (x , y+n);merge (x+n , y+n*2);merge (x+n*2 , y);}}cout << cnt;return 0;}```:::## 习题课### [Maximal AND](codeforces.com/problemset/problem/1669/H)#### 题意可以改变$n$个数转成$2$进制数后的第$i$位为$1(0 \le i \le 30)$共有$k$次,求最后的所有数与(&)在一起的$\max$#### 知识点位运算,贪心#### 正解从高位往低位依次判断是否可行并累加即可#### 错因我好像没错#### 心得与(&),或(|)。都忘得差不多了#### 复杂度* 时间复杂度:$O(30N)$* 空间复杂度:$O(6 \cdot 10^5)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;bool a[200001][31];int Q , n , k , v , ans;int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> Q;while (Q--){ans=0;cin >> n >> k;for (int i=1;i<=n;i++){cin >> v;int cnt=0;for (int j=0;j<=30;j++){a[i][j]=v%2;v/=2;}}for (int i=30;i>=0;i--){int cnt=0;for (int j=1;j<=n;j++){cnt+=(1-a[j][i]);}if (cnt<=k){k-=cnt;ans+=(1 << i);}}cout << ans << "\n";}return 0;}```:::### [Minimum Ties](codeforces.com/problemset/problem/1487/C)#### 题意选出一个$n \times n$的数组$a$,满足在区域内的所有数$\in (0,2)$且为整数,并且在$1 \le i,j \le n$的$a[i][j]+a[j][i]=0$。在每一行的数的和都相同时且用最少的$0$,输出$a$数组$(i < j)$#### 知识点找规律#### 正解找规律和推式子均可#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(N^2)$* 空间复杂度:$O(1)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;int Q , n;int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> Q;while (Q--){cin >> n;if (n%2==1){for (int i=1;i<=n;i++){for (int j=i+1;j<=n;j++){if (j-i<=n/2) cout << 1 << " ";else cout << -1 << " ";}}}else{for (int i=1;i<=n;i++){for (int j=i+1;j<=n;j++){if (j-i<n/2) cout << 1 << " ";else if (j-i==n/2) cout << 0 << " ";else cout << -1 << " ";}}}cout << "\n";}return 0;}//```:::### [Building a Fence](https://codeforces.com/problemset/problem/1469/C)#### 题意求是否存在一种情况将东西放到当前高度及$+k-1$处,且相邻的要有接触#### 知识点思想#### 正解求出当前可以放在哪里,记录成$l \sim r$,再看$l>r$就说明不可能,最后的$h[n]要在l \sim r内$#### 错因没有做#### 心得每道题都要做到#### 复杂度* 时间复杂度:$O(N)$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int h[200005];signed main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int Q;cin >> Q;while (Q--){int n , k;cin >> n >> k;for (int i=1;i<=n;i++){cin >> h[i];}bool p=1;int l=h[1] , r=h[1];for (int i=2;i<=n;i++){l=max (l-k+1 , h[i]);r=min (r+k-1 , h[i]+k-1);if (l>r){p=0;break;}}if (h[n]<l||h[n]>r){p=0;}if (p==0) cout << "NO\n";else cout << "YES\n";}return 0;}```:::### [The Delivery Dilemma](https://codeforces.com/problemset/problem/1443/C)#### 题意求出$\min(\max其他外卖员的,剩余的和)$#### 知识点二分答案#### 正解二分答案找出要自己送的快递,再看是否合规#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(N \log_2 N)$* 空间复杂度:$O(N)$:::success[代码]{open}{open}```cpp#include <bits/stdc++.h>using namespace std;int a[200005] , b[200005];int Q , n;bool check (int mid){long long sum=0;for (int i=1;i<=n;i++){if (a[i]>mid) sum+=b[i];}return (sum<=mid);}int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> Q;while (Q--){cin >> n;for (int i=1;i<=n;i++){cin >> a[i];}for (int i=1;i<=n;i++){cin >> b[i];}int l=1 , r=1e9;while (l<r){int mid=(l+r)/2;if (check (mid)==1){r=mid;}else{l=mid+1;}}cout << l << "\n";}return 0;}```:::### [Infected Tree](codeforces.com/problemset/problem/1689/C)#### 题意病毒会传染给和它相连的点,每次你可以拯救一个未被感染的点,切除,它的子树的点成功,求最后最多有多少个成功的点#### 知识点树形$dp$#### 正解先用$dfs$求出每个点的儿子和子树中的点的数量,再用树形$dp$,$dp[i]$表示$i$被感染后最多能成功几个点,输出$dp[1]$#### 错因没想到#### 心得$???$#### 复杂度* 时间复杂度:$O(N)$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;vector <int> g[300005];int son[2][300005];int dp[300005] , sz[300005];int Q , n , u , v;void dfs (int now , int lst){sz[now]=1;int cnt=-1;for (auto i:g[now]){if (i==lst) continue;cnt++;son[cnt][now]=i;dfs (i , now);sz[now]+=sz[i];}}void DFS (int now , int lst){for (auto i:g[now]){if (i==lst) continue;DFS (i , now);}int x=son[0][now] , y=son[1][now];if (x>0&&y==0){dp[now]=sz[x]-1;}else if (y>0){dp[now]=max (dp[x]+sz[y]-1 , sz[x]-1+dp[y]);}}int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> Q;while (Q--){cin >> n;for (int i=1;i<=n;i++){son[0][i]=0;son[1][i]=0;dp[i]=0;sz[i]=0;g[i].clear ( );}for (int i=1;i<=n-1;i++){cin >> u >> v;g[u].push_back (v);g[v].push_back (u);}dfs (1 , 0);DFS (1 , 0);cout << dp[1] << "\n";}return 0;}```:::### [Palindrome Basis](codeforces.com/problemset/problem/1673/C)#### 题意求$Q$个数$n$能分成几种回文数的和#### 知识点回文数,完全背包$dp$#### 正解先求出所有在$\in (1,4 \times 10^4)$中的回文数,再用完全背包$dp$模版即可~~总共有498个回文数~~#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(2 \times 10^7)$* 空间复杂度:$O(4 \times 10^4)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;const int MOD=1e9+7;int dp[40005];int a[505];int Q , n , tot;int fan (int x){int res=0;while (x>0){res=res*10+x%10;x/=10;}return res;}int main ( ){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);for (int i=1;i<=40000;i++){int j=fan (i);if (i==j){a[++tot]=i;}}dp[0]=1;for (int i=1;i<=tot;i++){int k=a[i];for (int j=k;j<=40000;j++){dp[j]+=dp[j-k];dp[j]%=MOD;}}cin >> Q;while (Q--){cin >> n;cout << dp[n] << "\n";}return 0;}```:::