雅礼兴趣小组总结-4
wuweize150815 · · 个人记录
总结
习题课(兴趣小组)
Not Adjacent Matrix
题意
将一个
知识点
构造
正解
设行为
注意
错因
我好像没错
心得
复杂度
- 时间复杂度:
O(N^2) - 空间复杂度:
O(N^2) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; int a[105][105]; int Q , n; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { memset (a , 0 , sizeof (a)); cin >> n; if (n==2) { cout << -1 << "\n"; continue; } int now=0; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { if ((i+j)%2==0) { now++; a[i][j]=now; } } } for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { if (a[i][j]==0) { now++; a[i][j]=now; } cout << a[i][j] << " "; } cout << "\n"; } } return 0; }:::
Same Differences
题意
求有几对
(i,j) 满足a[i]-a[j]=i-j 知识点
方程,
map 正解
化简:
a[i]-i=a[j]-j ,统计每个只出现多少次,再每个的数量选出两个的方案数的总和错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N \log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; map <int , int> a; int Q , n , v; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { a.clear ( ); cin >> n; for (int i=1;i<=n;i++) { cin >> v; a[v-i]++; } long long cnt=0; for (auto i:a) { cnt+=(long long) i.second*(i.second-1)/2; } cout << cnt << "\n"; } return 0; }:::
Arranging The Sheep
题意
每个
*可以往左边或者右边的.挪一个。求至少要挪几下才能使所有的*连在一起知识点
贪心
正解
有
tot 个*那么所有的*全部往第\lceil \frac{tot}{2} \rceil 点靠拢,再计算即可错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; string s; int cnt[1000005]; int Q , n; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> n >> s; s=" "+s; int tot=0; for (int i=1;i<=n;i++) { if (s[i]=='*') { cnt[++tot]=i; } } long long ans=0 , S=(tot+1)/2; int res=cnt[S]; for (int i=1;i<=S-1;i++) { ans+=(res-(S-i))-cnt[i]; } for (int i=S+1;i<=tot;i++) { ans+=cnt[i]-(res+(i-S)); } cout << ans << "\n"; } return 0; }:::
Corrupted Array
题意
先有一个
a 数组,有n 个数下标分别为1 \sim n ,a[n+1] =\sum_{i=1}^n a[i] ,a[n+2] 为随机抽取。将a 打乱得到b 数组。已知b 数组,求任意一个可能的a 数组知识点
思想,
map 正解
#### 错因 我好像没错 #### 心得 $??? 复杂度
- 时间复杂度:
O(N \log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> #define int long long using namespace std; int a[200005]; map <int , int> mp1 , mp2; int Q , n; signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { mp1.clear ( ); mp2.clear ( ); cin >> n; n+=2; long long sum=0; for (int i=1;i<=n;i++) { cin >> a[i]; sum+=a[i]; } sort (a+1 , a+n+1); for (int i=1;i<=n-1;i++) { mp1[a[i]]++; } for (int i=1;i<=n-2;i++) { mp2[a[i]]++; } mp2[a[n]]++; if (mp1.count (sum-a[n]*2)) { mp1[sum-a[n]*2]--; for (auto i:mp1) { for (int j=1;j<=i.second;j++) { cout << i.first << " "; } } } else if (mp2.count (sum-a[n-1]*2)) { mp2[sum-a[n-1]*2]--; for (auto i:mp2) { for (int j=1;j<=i.second;j++) { cout << i.first << " "; } } } else { cout << -1 << " "; } cout << "\n"; } return 0; }:::
Permutation by Sum
题意
构造一个
n 的排列使\sum_{i=l}^r=s ,无法构造输出-1 知识点
思想
正解
毕竟
n \ge 500 ,500^2=2.5 \times 10^5 ,完全可以用O(N^2) 的方法。如果填最小的还是多了或者填最大的还是小了,输出-1 ,其他情况都能满足。先将l \sim r 中的每一个数分别1,2, \cdots r-l+1 ,s 次操作,每次操作最后一个满足条件的数,就+1 ,最后将没出现的数分配到两边即可错因
两边都要来一次,我的数没增加。发
WA 了,这时:比赛已经结束了心得
应该要写出来的
\cdots 复杂度
- 时间复杂度:
O(N^2) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; bool vis[505]; int b[505]; int Q , n , l , r , s , tot; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> n >> l >> r >> s; long long sum=0; for (int i=l;i<=r;i++) { b[i]=i-l+1; sum+=b[i]; } if (sum>s) { cout << -1 << "\n"; } else { int now=r; int len=r-l+1; while (sum<s) { if (b[now]==n-(r-now)) now--; if (now<l) break; b[now]++; sum++; } if (sum==s) { memset (vis , 0 , sizeof (vis)); for (int i=l;i<=r;i++) { vis[b[i]]=1; } int tot=1; for (int i=1;i<=l-1;i++) { while (vis[tot]==1) { tot++; } cout << tot << " "; tot++; } for (int i=l;i<=r;i++) { cout << b[i] << " "; } for (int i=r+1;i<=n;i++) { while (vis[tot]==1) { tot++; } cout << tot << " "; tot++; } cout << "\n"; } else { cout << -1 << "\n"; } } } return 0; }:::
Education
题意
等级为
x 是一天赚a[x] 元,每天可以扣b[x] 升一级,求最少要几天能让手中的钱\ge c 知识点
贪心
正解
记下至少要多少天才能升到
i 级和剩余的钱数,再递推取\min 错因
没有做
心得
每道题都要做到
复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#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
题意
设
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}#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 【模板】最小生成树
:::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
还是一样的,只不过每两个点都连边了。套
kruskal 或prim 板子就行 :::success[代码]{open}#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
题意
开始
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}#include <bits/stdc++.h> #define int long long using 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
题意
每次格子上都会
+1 ,求最多能拿到几个知识点
分类讨论
正解
简单分析即可
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include<bits/stdc++.h> #define int long long using 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
题意
#### 知识点 预处理,$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}#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
题意
1.
依次输出买单的人的编号(第几个来的)
知识点
复杂度
- 时间复杂度:
O(N \log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#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
题意
求
a,b 是否能经过k 次相同知识点
质因数分解
正解
求出最多最少要几次即可
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(\sqrt{ab}) - 空间复杂度:
O(1) :::success[代码]{open}#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; } //:::