雅礼兴趣小组总结-3
wuweize150815 · · 个人记录
总结
模拟赛
P10124 [USACO18OPEN] Family Tree B
题意
改定一棵树,求两头牛的关系
正解
将每头牛离散化成编号,在去
错因
心得
少用
复杂度
- 时间复杂度:
O(N \log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; vector <int> g[1005]; string v[1005]; string up[1005] , down[1005]; string a , b , ans="NOT RELATED"; int in[1005] , S[1005]; int fa[1005]; int n , tot; void dfs (int x, int cnt) { S[x]=cnt; for (auto i:g[x]) { dfs (i , cnt+1); } } int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a >> b; v[++tot]=a; v[++tot]=b; for (int i=1;i<=n;i++) { cin >> up[i] >> down[i]; v[++tot]=up[i]; v[++tot]=down[i]; } sort (v+1 , v+tot+1); tot=unique (v+1 , v+tot+1)-(v+1); for (int i=1;i<=n;i++) { int aaa , bbb; aaa=lower_bound (v+1 , v+tot+1 , up[i])-v; bbb=lower_bound (v+1 , v+tot+1 , down[i])-v; g[aaa].push_back (bbb); fa[bbb]=aaa; in[bbb]++; } for (int i=1;i<=tot;i++) { if (in[i]==0) { dfs (i , 1); break; } } int aa=lower_bound (v+1 , v+tot+1 , a)-v; int bb=lower_bound (v+1 , v+tot+1 , b)-v; if (S[aa]<S[bb]) { swap (aa , bb); swap (a , b); } if (in[aa]>0&&in[bb]>0&&fa[aa]==fa[bb]) { cout << "SIBLINGS"; return 0; } int x=0 , y=0 , kkk=aa; for (int i=1;i<=S[aa]-S[bb];i++) { kkk=fa[kkk]; x++; } aa=kkk; while (aa!=bb&&in[aa]>0&&in[bb]>0) { x++ , y++; aa=fa[aa] , bb=fa[bb]; } if (aa==bb) { if (y==0) { if (x==1) ans="mother"; else if (x==2) ans="grand-mother"; else { ans=""; for (int i=1;i<=x-2;i++) { ans+="great-"; } ans+="grand-mother"; } } else if (y==1) { if (x==1) ans="sister"; else if (x==2) ans="aunt"; else { ans=""; for (int i=1;i<=x-2;i++) { ans+="great-"; } ans+="aunt"; } } else { cout << "COUSINS"; return 0; } } if (ans=="NOT RELATED") cout << ans; else cout << b << " is the " << ans << " of " << a; return 0; }:::
P1694 [USACO19FEB] The Great Revegetation B
题意
连接的每两个草地颜色不能相同,求最后最小的字典序
正解
从前往后选择即可,看一下之前的是否出现过
错因
写了拓扑排序
心得
要按照字典序来
复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; vector <int> g[105]; bool vis[105][5]; int n , m , x , y; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; while (m--) { cin >> x >> y; g[min (x , y)].push_back (max (x , y)); } for (int i=1;i<=n;i++) { int s; for (int j=1;j<=4;j++) { if (vis[i][j]==0) { s=j; break; } } cout << s; for (auto j:g[i]) { vis[j][s]=1; } } return 0; }:::
P6149 [USACO20FEB] Triangles S
题意
选出三个点坐标分别为
(a_1,b_1),(a_2,b_2),(a_3,b_3) ,在a_1=a_2,b_1=b_3 时,求三角形面积和\times 2 正解
将和每个点行相同的和列相同的所乘起来就是那个点的贡献,加起来
错因
我打的暴力
心得
想到了一点,但立刻否定了,也没有任何依据
复杂度
- 时间复杂度:
O(N \log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; struct node { int x , y , id; }; const int MOD=1e9+7; node v[100005]; long long a1[100005] , b1[100005]; long long a2[100005] , b2[100005]; long long sum=0; int n; bool cmp1 (node x , node y) { if (x.x!=y.x) return x.x<y.x; return x.y<y.y; } bool cmp2 (node x , node y) { if (x.y!=y.y) return x.y<y.y; return x.x<y.x; } int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i=1;i<=n;i++) { cin >> v[i].x >> v[i].y; v[i].id=i; } sort (v+1 , v+n+1 , cmp1); for (int i=1;i<=n;) { int ans=i; while (ans<n&&v[ans+1].x==v[ans].x) ans++; for (int j=i+1;j<=ans;j++) { a1[v[j].id]=a1[v[j-1].id]+(j-i)*(v[j].y-v[j-1].y); } for (int j=ans-1;j>=i;j--) { b1[v[j].id]=b1[v[j+1].id]+(ans-j)*(v[j+1].y-v[j].y); } i=ans+1; } sort (v+1 , v+n+1 , cmp2); for (int i=1;i<=n;) { int ans=i; while (ans<n&&v[ans+1].y==v[ans].y) ans++; for (int j=i+1;j<=ans;j++) { a2[v[j].id]=a2[v[j-1].id]+(j-i)*(v[j].x-v[j-1].x); } for (int j=ans-1;j>=i;j--) { b2[v[j].id]=b2[v[j+1].id]+(ans-j)*(v[j+1].x-v[j].x); } i=ans+1; } for (int i=1;i<=n;i++) { sum+=(a1[i]+b1[i])*(a2[i]+b2[i]); // cout << a1[i] << " " << b1[i] << " " << a2[i]<< " " << b2[i] << "\n"; sum%=MOD; } cout << sum; return 0; }:::
新课
P3367 【模板】并查集
并查集模版 :::success[代码]{open}
#include <bits/stdc++.h> #define int long long using 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
求每个人被谁击败了,没有输出
0 。用两个数组,一个记录并查集,一个记录被谁击败了即可 :::success[代码]{open}#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] 银河英雄传说
用两个数组记录并查集和是序列的第几个,再用距离相减即可 :::success[代码]{open}
#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] 食物链
是同类就
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}#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
题意
可以改变
n 个数转成2 进制数后的第i 位为1(0 \le i \le 30) 共有k 次,求最后的所有数与(&)在一起的\max 知识点
位运算,贪心
正解
从高位往低位依次判断是否可行并累加即可
错因
我好像没错
心得
与(&),或(|)。都忘得差不多了
复杂度
- 时间复杂度:
O(30N) - 空间复杂度:
O(6 \cdot 10^5) :::success[代码]{open}#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
题意
选出一个
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}#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
题意
求是否存在一种情况将东西放到当前高度及
+k-1 处,且相邻的要有接触知识点
思想
正解
求出当前可以放在哪里,记录成
l \sim r ,再看l>r 就说明不可能,最后的h[n]要在l \sim r内 错因
没有做
心得
每道题都要做到
复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> #define int long long using 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
题意
求出
\min(\max其他外卖员的,剩余的和) 知识点
二分答案
正解
二分答案找出要自己送的快递,再看是否合规
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N \log_2 N) - 空间复杂度:
O(N) :::success[代码]{open} {open}#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
题意
病毒会传染给和它相连的点,每次你可以拯救一个未被感染的点,切除,它的子树的点成功,求最后最多有多少个成功的点
知识点
树形
dp 正解
先用
dfs 求出每个点的儿子和子树中的点的数量,再用树形dp ,dp[i] 表示i 被感染后最多能成功几个点,输出dp[1] 错因
没想到
心得
??? 复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#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
题意
求
Q 个数n 能分成几种回文数的和知识点
回文数,完全背包
dp 正解
先求出所有在
\in (1,4 \times 10^4) 中的回文数,再用完全背包dp 模版即可总共有498个回文数错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(2 \times 10^7) - 空间复杂度:
O(4 \times 10^4) :::success[代码]{open}#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; }:::