版本比较
雅礼兴趣小组总结-4
正文
增加 691 行删除 0 行
344 行未改动全部展开3453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039344 行未改动全部展开```:::### [Education](codeforces.com/problemset/problem/1512/F)#### 题意等级为$x$是一天赚$a[x]$元,每天可以扣$b[x]$升一级,求最少要几天能让手中的钱$\ge c$#### 知识点贪心#### 正解记下至少要多少天才能升到$i$级和剩余的钱数,再递推取$\min$#### 错因没有做#### 心得每道题都要做到#### 复杂度* 时间复杂度:$O(N)$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;int a[200005] , b[200005];int Q , n , c;int CNT (int x , int y){ return (x+y-1)/y;}int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> n >> c; long long minn=1e18 , now=0 , sum=0; for (int i=1;i<=n;i++) { cin >> a[i]; } for (int i=1;i<=n-1;i++) { cin >> b[i]; } for (int i=1;i<=n-1;i++) { minn=min (minn , now+CNT (c-sum , a[i])); int cnt=CNT (b[i]-sum , a[i]); now+=cnt+1; sum=sum+cnt*a[i]-b[i]; // cout << now << " " << cnt << " " << sum << "\n"; } minn=min (minn , now+CNT (c-sum , a[n])); cout << minn << "\n"; } return 0;}```:::### [Short Task ](https://codeforces.com/problemset/problem/1512G)#### 题意设$f(x)$为$x$所有因数和,求最小的$x$使得$f(x)=c$#### 正解筛法#### 正解用筛法求出每个数的因数,再用$minn$数组记录即可#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(\sum_{i=1}^{10^7} \frac{1}{i} \times 10^7)$* 空间复杂度:$O(10^7)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;const int M=1e7;int sum[M+5];int minn[M+5];int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset (minn , 0x3f , sizeof (minn)); for (int i=1;i<=M;i++) { for (int j=i;j<=M;j+=i) { sum[j]+=i; } } for (int i=1;i<=10000000;i++) { if (sum[i]<=M&&minn[sum[i]]>i) { minn[sum[i]]=i; } } int Q , n; cin >> Q; while (Q--) { cin >> n; if (minn[n]<=1e9) cout << minn[n] << "\n"; else cout << "-1\n"; } return 0;}///```:::## 新课### [P3366 【模板】最小生成树](https://www.luogu.com.cn/problem/P3366)$kruskal$或$prim$板子题:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;struct node{ int x , y , z;};node v[200005];int fa[5005];int n , m , x , y , z;int sum=0 , cnt;int find (int x){ if (fa[x]==x) return x; return fa[x]=find (fa[x]);}bool cmp (node x , node y){ return x.z<y.z;}int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; cnt=n; for (int i=1;i<=n;i++) { fa[i]=i; } for (int i=1;i<=m;i++) { cin >> v[i].x >> v[i].y >> v[i].z; } sort (v+1 , v+m+1 , cmp); for (int i=1;i<=m;i++) { int nx=find (v[i].x) , ny=find (v[i].y); if (nx==ny) continue; fa[ny]=nx; cnt--; sum+=v[i].z; if (cnt==1) { cout << sum; return 0; } } cout << "orz"; return 0;}```:::### [P2330 [SCOI2005] 繁忙的都市](luogu.com.cn/problem/P2330)$kruskal$板子题,再加上$maxn$记录最大值即可:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;struct node{ int x , y , z;};node v[8008];int fa[305];int n , m , x , y , z;int sum=0 , cnt , p=0;int find (int x){ if (fa[x]==x) return x; return fa[x]=find (fa[x]);}bool cmp (node x , node y){ return x.z<y.z;}int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; cnt=n; for (int i=1;i<=n;i++) { fa[i]=i; } for (int i=1;i<=m;i++) { cin >> v[i].x >> v[i].y >> v[i].z; p+=v[i].z; } sort (v+1 , v+m+1 , cmp); for (int i=1;i<=m;i++) { int nx=find (v[i].x) , ny=find (v[i].y); if (nx==ny) continue; fa[ny]=nx; cnt--; sum++; if (cnt==1) { cout << sum << " " << v[i].z; return 0; } } return 0;}
/*** @runId: 67228* @language: C++* @author: 伍位泽* @submitTime: 2026-05-31 14:43:19*/```:::### [P4047 [JSOI2010] 部落划分](luogu.com.cn/problem/P4047)因为题目要让剩下的联通块距离尽量大,用$kruskal$跑一边,再找两个有边且不在同一联通快的点,输出最小值:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;struct node{ int x , y , z;};node v[1000005];int fa[1005] , x[1005] , y[1005];int n , k , tot;int find (int x){ if (fa[x]==x) return x; return fa[x]=find (fa[x]);}bool cmp (node x , node y){ return x.z<y.z;}int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i=1;i<=n;i++) { cin >> x[i] >> y[i]; fa[i]=i; for (int j=1;j<=i-1;j++) { int dx=abs (x[i]-x[j]) , dy=abs(y[i]-y[j]); v[++tot]={i , j , dx*dx+dy*dy}; } } sort (v+1 , v+tot+1 , cmp); int maxn=0 , cnt=n; for (int i=1;i<=tot;i++) { int nx=find (v[i].x) , ny=find (v[i].y); if (nx==ny) continue; fa[ny]=nx; cnt--; if (cnt==k) break; } for (int i=1;i<=tot;i++) { int dx=find (v[i].x) , dy=find (v[i].y); if (dx==dy) continue; maxn=v[i].z; break; } printf ("%.2lf" , sqrt (maxn*1.0)); return 0;}```:::### [P1546 [USACO3.1] 最短网络 Agri-Net](luogu.com.cn/problem/P1546)还是一样的,只不过每两个点都连边了。套$kruskal$或$prim$板子就行:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;struct node{ int x , y , z;};node v[10005];int fa[2005];int n , m , x , y , z , tot;int find (int x){ if (fa[x]==x) return x; return fa[x]=find (fa[x]);}bool cmp (node x , node y){ return x.z<y.z;}int 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; while (m--) { cin >> x >> y >> z; v[++tot]={x , y , z}; } sort (v+1 , v+tot+1 , cmp); long long maxn=0; for (int i=1;i<=tot;i++) { int dx=find (v[i].x) , dy=find (v[i].y); if (dx==dy) continue; maxn=v[i].z; fa[dy]=dx; } cout << maxn; return 0;}```:::## 习题课### [Make it Increasing](codeforces.com/problemset/problem/1667/A)#### 题意开始$b[i]=0$,每次可以$b[i] \pm a[i]$,求最少要多少次才能使$b[1]<b[2]< \cdots <b[n-1]<b[n]$#### 知识点枚举#### 正解枚举最后哪个点还是$0$,再求最少次数取$\min$#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(N^2)$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int a[5005];int n , ans=LONG_LONG_MAX;int CNT (int x , int y){ return x/y+1;}int cishu (int now){ int cnt=0 , lst; lst=0; for (int i=now+1;i<=n;i++) { int res=CNT (lst , a[i]); cnt+=res; lst=res*a[i]; } lst=0; for (int i=now-1;i>=1;i--) { int res=CNT (lst , a[i]); cnt+=res; lst=res*a[i]; } return cnt;}signed main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1;i<=n;i++) { cin >> a[i]; } for (int i=1;i<=n;i++) { ans=min (ans , cishu (i)); } cout << ans << "\n"; return 0;}//```:::### [The Enchanted Forest](codeforces.com/problemset/problem/1687/A)#### 题意每次格子上都会$+1$,求最多能拿到几个#### 知识点分类讨论#### 正解简单分析即可#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(N)$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include<bits/stdc++.h>#define int long longusing namespace std;int s[200005];int T , n , k , v , ans=0;signed main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> n >> k; for (int i=1;i<=n;i++) { cin >> v; s[i]=s[i-1]+v; } if (k>n) { cout << ((k-1+k-n)*n/2)+s[n] << '\n'; } else { ans=0; for (int i=k;i<=n;i++) { ans=max (ans , (s[i]-s[i-k])+((k-1)*k/2)); } cout << ans << "\n"; } } return 0;}//```:::### [Make Them Equal](codeforces.com/problemset/problem/1633/D)#### 题意$a[i]=1$,每次可以将其中一个$a[i]+=frac{a[i]}{x}(x为正整数)$,每当一个$a[i]=b[i]会获得c[i]价值$,在操作$\le k$次的情况下,求最大价值#### 知识点预处理,$01$背包$dp$#### 正解先处理出$1$至少要几次才能到$b[i]$,再套用$01$背包$dp$模版#### 注意有可能$k$较大,那么就可以直接输出价值总和,不用跑$dp$,否则会$10^6 \times 10^3=10^9$严重超时#### 错因看错题了,以为$x$是输入的一个变量#### 心得不是特别难#### 复杂度* 时间复杂度:$O(\min (nk , 3 \times 10^7))$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;int f[100005];int dp[1000005];int b[1005] , c[1005];int Q , n , k;int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset (f , 0x3f , sizeof (f)); f[1]=0; for (int i=1;i<=1000;i++) { for (int j=1;j<=i;j++) { int pos=i+i/j; f[pos]=min (f[pos] , f[i]+1); } } cin >> Q; while (Q--) { memset (dp , 0 , sizeof (dp)); cin >> n >> k; int cnt=0 , sum=0; for (int i=1;i<=n;i++) { cin >> b[i]; cnt+=f[b[i]]; } for (int i=1;i<=n;i++) { cin >> c[i]; sum+=c[i]; } if (cnt<=k) { cout << sum << "\n"; } else { for (int i=1;i<=n;i++) { for (int j=k;j>=f[b[i]];j--) { dp[j]=max (dp[j] , dp[j-f[b[i]]]+c[i]); } } cout << dp[k] << "\n"; } } return 0;}```:::### [Berpizza](codeforces.com/problemset/problem/1468/C)#### 题意$3$中输入:
1.$1 m$,新来一个人,数值为$m$2.$2$给未买单且最早来的顾客买单3.$3$给未买单且数值最大顾客买单
依次输出买单的人的编号(第几个来的)#### 知识点$priority_queue$,模拟#### 正解用堆和标记数组模拟过程即可#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(N \log_2 N)$* 空间复杂度:$O(N)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;struct node{ int id , w; bool operator < (const node &a) const { if (w!=a.w) return w<a.w; return id>a.id; }};priority_queue <node> q;bool vis[500005];int Q , op , m , tot , lst;int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; for (int i=1;i<=Q;i++) { cin >> op; if (op==1) { cin >> m; tot++; q.push ({tot , m}); } else if (op==2) { for (int j=lst+1;j<=tot;j++) { if (vis[j]==0) { cout << j << " "; vis[j]=1; lst=j; break; } } } else { while (!(q.empty ( ))) { node p=q.top ( ); if (vis[p.id]==0) { cout << p.id << " "; vis[p.id]=1; break; } q.pop ( ); } } } return 0;}//```:::## 习题课(兴趣小组)### [Another Problem About Dividing Numbers](codeforces.com/problemset/problem/1538/D)#### 题意求$a,b$是否能经过$k$次相同#### 知识点质因数分解#### 正解求出最多最少要几次即可#### 错因我好像没错#### 心得$???$#### 复杂度* 时间复杂度:$O(\sqrt{ab})$* 空间复杂度:$O(1)$:::success[代码]{open}```cpp#include <bits/stdc++.h>using namespace std;int Q , a , b , k , x , y;int minn (int a , int b){ int c=__gcd (a , b) , res=0; if (a>c) res++; if (b>c) res++; return res;}int zys (int s){ int cnt=0; for (int i=2;i*i<=s;i++) { while (s%i==0) { cnt++; s/=i; } } if (s>1) cnt++; return cnt;}int maxn (int a , int b){ return zys (a)+zys (b);}int main ( ){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> a >> b >> k; x=minn (a , b); y=maxn (a , b); if (a==b&&k==1) { cout << "No\n"; } else if (k>=x&&k<=y) { cout << "Yes\n"; } else { cout << "No\n"; } } return 0;}//```:::