雅礼兴趣小组总结-2
wuweize150815 · · 个人记录
雅礼兴趣小组总结-2
习题课
Pipes
题意
是否能通过管道(可以旋转任意次
知识点
正解
复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; short k[3][200005]; bool f[3][200005]; int Q , n; char p; void dfs (int a , int b , int c) { if (a<1||a>2||b<1||b>n+1) return; if (f[a][b]==1) return; f[a][b]=1; // cout << a << " " << b << " " << c << " " << k[a][b] << "\n"; int s=k[a][b]; if (c==1) { if (s<=2) { dfs (a , b+1 , 1); } else if (s<=6) { if (a==1) { dfs (a+1 , b , 2); } else { dfs (a-1 , b , 2); } } } else { if (s>2&&s<=6) { dfs (a , b+1 , 1); } } } int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> n; for (int i=1;i<=2;i++) { for (int j=1;j<=n;j++) { cin >> p; k[i][j]=p-'0'; f[i][j]=0; } } f[1][n+1]=f[2][n+1]=0; k[1][n+1]=0; dfs (1 , 1 , 1); if (f[2][n+1]==0) { cout << "NO\n"; } else { cout << "YES\n"; } } return 0; }:::
Quests
题意
设数为
x ,那么每个数每x 天才能加一次,在d 天和\ge c 的情况下输出x_{min} 知识点
贪心,二分答案
正解
按数从大往下选,在二分答案枚举
x ,再选数比较错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; long long s[200005]; int a[200005]; long long c; int Q , n , d; bool check (int mid) { mid++; // cout << mid << " " << s[min (n , mid)] << " " << (d/mid) << " " << s[min (n , d%mid)] << "\n\n"; return ((s[min (n , mid)]*(d/mid)+s[min (n , d%mid)])>=c); } int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> n >> c >> d; for (int i=1;i<=n;i++) { cin >> a[i]; } sort (a+1 , a+n+1); for (int i=1;i<=n;i++) { s[i]=s[i-1]+a[n-i+1]; } if (check (d)==1) { cout << "Infinity\n"; } else if (check (0)==0) { cout << "Impossible\n"; } else { int l=0 , r=d; while (l<r) { int mid=(l+r+1)/2; if (check (mid)==1) { l=mid; } else { r=mid-1; } } cout << l << "\n"; } } return 0; }:::
Shuffle
题意
每次操作可以将区间内的两个数交换,求
1 是否能到x 知识点
标记数组
正解
每一次操作分类
p =\sum_{i=l}^r f[i] |p \begin{cases} p=0 &任意一个都不行\\ p=1 &任意一个都行 \end{cases} 最后输出标记数组
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(M \cdot log_2 M) - 空间复杂度:
O(M) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; int L[105] , R[105]; bool f[1005]; int a[205]; int Q , n , x , m , tot , cnt=0; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { memset (f , 0 , sizeof (f)); tot=0; cin >> n >> x >> m; a[++tot]=x; a[++tot]=x+1; for (int i=1;i<=m;i++) { cin >> L[i] >> R[i]; R[i]++; a[++tot]=L[i]; a[++tot]=R[i]; } sort (a+1 , a+tot+1); tot=unique (a+1 , a+tot+1)-(a+1); tot--; int pos=lower_bound (a+1 , a+tot+1 , x)-a; f[pos]=1; for (int i=1;i<=m;i++) { L[i]=lower_bound (a+1 , a+tot+1 , L[i])-a; R[i]=lower_bound (a+1 , a+tot+1 , R[i])-a; bool p=0; for (int j=L[i];j<=R[i]-1;j++) { p|=f[j]; } if (p==0) continue; for (int j=L[i];j<=R[i]-1;j++) { f[j]=p; } } cnt=0; for (int i=1;i<=tot;i++) { if (f[i]==1) { cnt+=a[i+1]-a[i]; } } cout << cnt << "\n"; } return 0; }:::
Special Numbers
题意
求第
k 个可以表示成n^{a_1}+n^{a_2}+ \cdots n^{a_i} 的数知识点
模拟,位运算
正解
在
n 进制中求出每个位1或0错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(log_2 k) - 空间复杂度:
O(1) :::success[代码]{open}#include <bits/stdc++.h> #define int long long using namespace std; const int MOD=1e9+7; string s; int Q , n , k , now , sum=0 , mul=1; signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { now=0 , sum=0 , mul=1; s=""; cin >> n >> k; while ((1 << now+1)-1<k) { now++; } for (int i=now;i>=0;i--) { if (k>=(1 << i)) { s+='1'; k-=(1 << i); } else { s+='0'; } } for (int i=0;i<=now;i++) { if (s[now-i]=='1') { sum=(sum+mul)%MOD; } mul=(mul*n)%MOD; } cout << sum << "\n"; } return 0; }:::
Add One
题意
数字
i 会变成i+1 ,求n 过了k 次的长度是多少知识点
序列
DP ,预处理正解
::::info[提示] :::info[提示]{open} 很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久很久 ::: 以前,我们做过这道题 :::: 就不多讲了
见提示错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(log_{10} N+1.6 \cdot 10^6) - 空间复杂度:
O(1.6 \cdot 10^6) :::success[代码]{open}#include <bits/stdc++.h> #define int long long using namespace std; const int MOD=1e9+7; int dp[200005][10]; int Q , n , m , sum=0; signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i=0;i<=9;i++) { dp[0][i]=1; } for (int i=1;i<=200000;i++) { for (int j=0;j<=8;j++) { dp[i][j]=dp[i-1][j+1]; } dp[i][9]=(dp[i-1][1]+dp[i-1][0])%MOD; } cin >> Q; while (Q--) { sum=0; cin >> n >> m; while (n!=0) { sum=(sum+dp[m][n%10])%MOD; n/=10; } cout << sum << "\n"; } return 0; }:::
Mark and His Unfinished Essay
题意
每次将字符串中一些字符复制到末尾,求最后第
x 位的字符是什么知识点
思想
正解
其实草做就是将
len +1 \sim len+(r-l+1) 变成l[i] \sim r[i] ,输出时从后往前看一下是否在范围内,往前推,最后输出字符错因
无从下手
更正时还将一个函数的参数写反了心得
:::::info[
有点不知道说啥] ::::info[还是有点不知道说啥] :::info[终于知道说啥了]有点不知道说啥:::
有点不知道说啥::::
有点不知道说啥:::::
有点不知道说啥复杂度
- 时间复杂度:
O(QC) - 空间复杂度:
O(C) :::success[代码]{open}#include<bits/stdc++.h> #define int long long using namespace std; string s; int l[200005] , r[200005] , SSS[200005]; int T , n , c , q , x , y , len; bool sf (int x , int p) { return (x>=l[p]+SSS[p]&&x<=r[p]+SSS[p]); } signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> n >> c >> q >> s; len=n; for(int i=1;i<=c;i++) { cin >> l[i] >> r[i]; SSS[i]=len-l[i]+1; len+=r[i]-l[i]+1; } while (q--) { cin >> x; for(int i=c;i>=1;i--) { if (sf (x , i)==1) { x-=SSS[i]; } } cout << s[x-1] << "\n"; } } return 0; }:::
习题课(兴趣小组)
Dense Array
题意
插入一些书,使
\max (a[i] , a[i-1]) \le \min (a[i] , a[i-1]) \times 2 知识点
模拟
正解
每次在两数中间插入即可
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]include <bits/stdc++.h>
using namespace std; int a[55]; int Q , n , cnt; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cnt=0; cin >> n; for (int i=1;i<=n;i++) { cin >> a[i]; if (i==1) continue; int now=max (a[i-1] , a[i]); while (now>min (a[i-1] , a[i])*2) { cnt++; now=(now+1)/2; } } cout << cnt << "\n"; } //// return 0; } :::
Balanced Remainders
题意
每次+
1 使/3 余0,1,2 的数量相同的最少操作知识点
模拟
正解
直接找每种的数量,再分别求
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; int k[10]; int Q , n , v , cnt; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cnt=0; memset (k , 0 , sizeof (k)); cin >> n; for (int i=1;i<=n;i++) { cin >> v; k[v%3]++; } n/=3; for (int i=1;i<=2;i++) { for (int j=0;j<=2;j++) { if (k[j]>n) { k[(j+1)%3]+=k[j]-n; cnt+=k[j]-n; k[j]=n; } } } cout << cnt << "\n"; } return 0; }:::
Sum of Cubes
题意
求每个
x 是否能表示成a^3+b^3(ab \ne 0) 知识点
cbrt 正解
直接枚举
错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(\sum_{i=1}^Q \sqrt[3]{N[i]}) - 空间复杂度:
O(1) :::success[代码]#include <bits/stdc++.h> #define int long long using namespace std; int Q , x; signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { bool p=0; cin >> x; for (int i=1;i<cbrt (x);i++) { int a=x-i*i*i , b=cbrt (a); if (b*b*b==a) { p=1; break; } } if (p==0) cout << "NO\n"; else cout << "YES\n"; } return 0; }:::
Permutation Transformation
题意
构建笛卡尔树
一
知识点
单调栈
正解
#include <bits/stdc++.h> using namespace std; stack <int> st; int L[105] , R[105]; int v[105] , ans[105]; int Q , n , k; void dfs (int now) { if (L[now]>0) { ans[L[now]]=ans[now]+1; dfs (L[now]); } if (R[now]>0) { ans[R[now]]=ans[now]+1; dfs (R[now]); } } 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++) { L[i]=0; R[i]=0; ans[i]=0; } while (!(st.empty ( ))) st.pop ( ); cin >> v[1]; st.push (1); k=1; for (int i=2;i<=n;i++) { cin >> v[i]; int lst=-1; while (!(st.empty ( ))&&v[st.top ( )]<v[i]) { lst=st.top ( ); st.pop ( ); } if (lst>-1) { if (st.empty ( )) { L[i]=lst; k=i; } else { R[st.top ( )]=i; L[i]=lst; } } else { int fa=k; while (R[fa]>0) { fa=R[fa]; } R[fa]=i; } st.push (i); } dfs (k); for (int i=1;i<=n;i++) { cout << ans[i] << " "; } cout << "\n"; } return 0; }二
知识点
ST$表,$DFS 正解
用
ST 表求出区间最大值和其位置,再跑DFS 错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N \cdot log_2 N) - 空间复杂度:
O(N \cdot log_2 N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; vector <int> g[105]; int st[10][105] , maxn[10][105]; int to_[105]; int v[105]; int Q , n; void dfs (int lst , int l , int r) { int len=log2 (r-l+1) , s; if (st[len][l]>=st[len][r-(1 << len)+1]) s=maxn[len][l]; else s=maxn[len][r-(1 << len)+1]; g[v[lst]].push_back (v[s]); if (s-1>=l) dfs (s , l , s-1); if (s+1<=r) dfs (s , s+1 , r); } void DFS (int id , int cnt) { to_[id]=cnt; for (auto i:g[id]) { DFS (i , cnt+1); } } 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++) { g[i].clear ( ); cin >> v[i]; st[0][i]=v[i]; maxn[0][i]=i; } for (int len=1;len<=log2 (n);len++) { for (int i=1;i<=n-(1 << len)+1;i++) { int j=i+(1 << len)-1; if (st[len-1][i]>=st[len-1][j-(1 << (len-1))+1]) { maxn[len][i]=maxn[len-1][i]; } else { maxn[len][i]=maxn[len-1][j-(1 << len-1)+1]; } st[len][i]=max (st[len-1][i] , st[len-1][j-(1 << (len-1))+1]); } } dfs (0 , 1 , n); DFS (n , 0); for (int i=1;i<=n;i++) { cout << to_[v[i]] << " "; } cout << "\n"; } return 0; }:::
Accidental Victory
题意
每次可以将比自己小的数加起来,求最后是否能统一
知识点
排序,前缀和,模拟
正解
先排序,在
p[i+1]=1或i=n 的情况下:\begin{cases} S[i] \ge a[i+1] &p[i]=1 \\ 否则 &p[i]=0 \end{cases} 错因
我好像没错
心得
??? 复杂度
- 时间复杂度:
O(N \cdot log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> #define int long long using namespace std; struct node { int id , w; }; node v[200005]; long long S[200005]; int a[200005]; int Q , n , tot; bool cmp (node x , node y) { return x.w<y.w; } signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { tot=0; cin >> n; for (int i=1;i<=n;i++) { cin >> v[i].w; v[i].id=i; } v[n+1]={n+1 , 0}; sort (v+1 , v+n+1 , cmp); for (int i=1;i<=n;i++) { S[i]=S[i-1]+v[i].w; } for (int i=n;i>=1;i--) { if (S[i]>=v[i+1].w) { a[++tot]=v[i].id; } else { break; } } cout << tot << "\n"; sort (a+1 , a+tot+1); for (int i=1;i<=tot;i++) { cout << a[i] << " "; } cout << "\n"; } // return 0; }:::
Equalize the Array
题意
删一些数,使有剩下的所有数的数量相同,求删掉的最少数
知识点
#### 正解 枚举剩下的数的数量,再从上一个推过来,就可以是$O(1)$而不是超时的$O(N) 错因
我好像没错
心得
极限7分钟:交代码+等待+AC复杂度
- 时间复杂度:
O(N \cdot log_2 N) - 空间复杂度:
O(N) :::success[代码]{open}#include <bits/stdc++.h> using namespace std; map <int , int> mp; int a[200005] , b[200005] , c[200005]; int Q , n , m , v , minn; int main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { minn=1e9 , m=0; mp.clear ( ); cin >> n; for (int i=1;i<=n;i++) { cin >> v; mp[v]++; } for (auto i:mp) { m++; a[m]=i.second; } sort (a+1 , a+m+1); b[0]=0; for (int i=1;i<=m;i++) { b[i]=b[i-1]+a[i]; } c[m+1]=0; for (int i=m;i>=1;i--) { c[i]=c[i+1]+a[i]; } minn=min (minn , n); for (int i=m;i>=1;i--) { int pos=lower_bound (a+1 , a+m+1 , a[i])-a; minn=min (minn , b[pos-1]+c[i+1]-a[i]*(m-i)); } cout << minn << "\n"; } return 0; }:::
Old Floppy Drive
题意
求从第1个开始,要加几个数才能使和
\ge x 知识点
前缀和,前缀最大值,二分查找,思想
正解
先看一下要转几圈整的,再用二分查找找到满足的第一个下标
错因
#### 心得 前面的题要写快一点 #### 复杂度 - 时间复杂度:
O(N+M \cdot log_2 N) - 空间复杂度:
O(1) :::success[代码]{open}#include <bits/stdc++.h> #define int long long using namespace std; int a[200005] , sum[200005] , maxn[200005]; int Q , n , m , x; signed main ( ) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> Q; while (Q--) { cin >> n >> m; for (int i=1;i<=n;i++) { cin >> a[i]; sum[i]=sum[i-1]+a[i]; maxn[i]=max (maxn[i-1] , sum[i]); } while (m--) { cin >> x; if (maxn[n]>=x) { int pos=lower_bound (maxn+1 , maxn+n+1 , x)-maxn; cout << pos-1 << " "; } else if (sum[n]<=0) { cout << -1 << " "; } else { int s=((x-maxn[n])+sum[n]-1)/sum[n]; x-=s*sum[n]; int pos=lower_bound (maxn+1 , maxn+n+1 , x)-maxn; cout << s*n+pos-1 << " "; } } cout << "\n"; } return 0; }:::