版本比较
7.9模拟赛题解
正文
增加 92 行删除 161 行
1234565789101148 行未改动606162636264656664656667686970717273747576777879808182838485868788899091929394959697989967686970717273747576777879808182838485868788899091929394959697989915 行未改动115116117118123119120121122127123124125126127112 行未改动全部展开24024124224324424524624724824925025125225325425525625725825926021 行未改动2822832842852812862872882892902862872912922932942952962972982993003013023033043058 行未改动31431531631731031831932032132211 行未改动334335336337338339340341342331332333334335336337338339340341342343344345346347348343344345346347348349350351352353354355356357358359360361350362352353354355356357363364365359360361362363364366367368369370371372373366367368369370371372373374375376377378379380381382383384385374375376377378379380381382387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451383453384385upd on 7.9 15:56 :T3 std已经修改,目前没有找到反例。
| 题目编号 | 题目名称 | 考察知识点 | 预估难度 || :----: | :----: | :----: | :----: || $\textbf A$ | 管道系统 | ??? | 黄 || $\textbf B$ | 玩手机 | 反悔贪心 | 绿 || $\textbf C$ | 链的mex | dp | 紫 | | $\textbf C$ | 链的mex | dp | 蓝 | | $\textbf D$ | 强连通图 | ??? | 紫 |
## A.[管道系统](https://www.luogu.com.cn/problem/T780747)
48 行未改动反悔贪心板子。
```#include<bits/stdc++.h>#define int long long#define ll long long#define pb emplace_backusing namespace std;const int maxn=1e6+5;int t,n;int a[maxn],b[maxn],s;priority_queue<int>q;signed main(){ cin>>n>>t; for(int i=1;i<=n;i++) { cin>>a[i]; s+=a[i]; } if(t>=n) return cout<<s,0; for(int i=t+1;i<=n;i++) if((i-t)%2==1) b[i]=1; for(int i=n;i>=1;i--) if(b[i]) { int cnt=0; for(int j=i;j>=1 && (!b[j] || j==i);j--) { q.push(a[j]); s-=a[j]; cnt++; } cnt--; for(int j=1;j<=cnt;j++) { s+=q.top(); q.pop(); } } cout<<s; return 0;}int n;ll t,a[1000005],ans;priority_queue<ll,vector<int>,greater<int> >q;int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);// cout<<q.top(); cin>>n>>t; for(int i=1;i<=n;i++){ cin>>a[i]; if(t>0){ q.push(a[i]); // cout<<"push "<<a[i]; ans+=a[i]; t--; } else{ t++; if(!q.empty()&&q.top()<a[i]){ ans-=q.top(); ans+=a[i]; q.push(a[i]); q.pop(); } } } cout<<ans; return 0;} ```
15 行未改动
- 若 $x$ 为 $y$ 祖先:```dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s2[y][u1[x][y]]);dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[u1[y][x]][x]);dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[y][u1[x][y]]);```- 若 $y$ 为 $x$ 祖先:```dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s2[u1[x][y]][y]);dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s2[x][u1[y][x]]);dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[x][u1[y][x]]);```- 若 $x,y$ 无祖孙关系:```112 行未改动全部展开 for(int j=1;j<=n;j++) s[i][j]+=s[i][j-1]+s[i-1][j]-s[i-1][j-1]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) {// if(i==j) s1[i][j]=get1(l[i],r[i],l[j],r[j]);// else s1[i][j]=get1(l[i],r[i],l[j],r[j])+get1(l[j],r[j],l[i],r[i]); //ij为祖孙关系时不会在转移中用到故不作特殊判断 } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) {// if(i==j) s2[i][j]=get2(l[i],r[i],l[j],r[j]);// else s2[i][j]=get2(l[i],r[i],l[j],r[j]); } dfs1(1,0); for(int i=1;i<=n;i++)//解决u1祖孙 { for(int j=0;j<g[i].size();j++)21 行未改动 int y=h[i][j].second; if(f[x][y])//x为y祖先 { dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s2[y][u1[x][y]]); dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[u1[y][x]][x]); dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[y][u1[x][y]]); // cout<<1<<"\n"; } else if(f[y][x])//y为x祖先 { dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s2[u1[x][y]][y]); dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[x][u1[y][x]]); dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s2[x][u1[y][x]]); // if(dp[x][y]==9 && x==6 && y==3) cout<<"----------"<<y<<" "<<u1[x][y]<<" "<<s2[u1[x][y]][y]<<"\n"; dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s2[x][u1[y][x]]); // if(dp[x][y]==9 && x==6 && y==3) cout<<"----------"<<x<<" "<<u1[y][x]<<" "<<s2[x][u1[y][x]]<<"\n"; // cout<<2<<"\n"; } else//x,y无祖孙关系 { dp[x][y]=max(dp[x][y],dp[u1[x][y]][y]+s1[x][y]); dp[x][y]=max(dp[x][y],dp[x][u1[y][x]]+s1[x][y]);// if(dp[x][y]==19 && x==8 && y==2) cout<<"----------"<<dp[u1[x][y]][y]<<" "<<s1[x][y]<<"\n";// cout<<3<<" "<<x<<" "<<y<<" "<<s2[x][y]<<"\n"; }// cout<<x<<" "<<y<<" "<<dp[x][y]<<"\n"; }8 行未改动
## D.[强连通图](https://www.luogu.com.cn/problem/T780750)
性质0:一定是从 $x$ 到 $y$ 的一条路径再拼上若干个环性质0:从 $x$ 到 $y$ 的路径一定是从 $x$ 到 $y$ 的一条简单路径再拼上若干个环。
性质1:可以在强连通图上找到一个经过所有点的复杂环,然后在这个环上走 $z$ 遍。这样你就可以任意利用图上的任何一个环。
性质2:利用裴蜀定理转化,所有环的环长的 **gcd** 是重要的。11 行未改动注意到性质0 中只需要任取一个链即可。设该链长度为 $d$,只需 $gx+d$ 为 $z$ 的倍数,其中 $x$ 可以任取,即存在 $x,y$ 满足 $d=zy-gx$。由裴蜀定理可知只需 $d$ 是 $\gcd \{z,g\}$ 的倍数。
```#include <iostream>#include <vector>#include <queue>#define int long longtypedef long long ll;using namespace std;const int N = 5e5 + 5;struct node{ int l , r , vala , valb; bool lazya , lazyb;}t[N << 2];int n , m , a[N] , b[N];void pushup(int x){ t[x].vala = t[x << 1].vala + t[x << 1 | 1].vala; t[x].valb = t[x << 1].valb + t[x << 1 | 1].valb;}void pushdown(int x){ if (t[x].lazya){ t[x << 1].vala = t[x << 1 | 1].vala = 0; t[x << 1].lazya = t[x << 1 | 1].lazya = 1; } if (t[x].lazyb){ t[x << 1].valb = t[x << 1 | 1].valb = 0; t[x << 1].lazyb = t[x << 1 | 1].lazyb = 1;const int N = 2e5 + 5;ll n , m , dis[N] , u[N] , v[N] , w[N] , ans; bool vis[N];vector <pair <ll , ll> > e[N];void bfs(){ queue <int> q; q.push(1); vis[1] = 1; while (!q.empty()){ int x = q.front(); q.pop(); for (pair <ll , ll> y : e[x]){ if (!vis[y.first]){ vis[y.first] = 1; dis[y.first] = dis[x] + y.second; q.push(y.first); } } } t[x].lazya = t[x].lazyb = 0;}void build(int x , int l , int r){ t[x].l = l , t[x].r = r; if (l == r) return; int mid = l + r >> 1; build(x << 1 , l , mid); build(x << 1 | 1 , mid + 1 , r);ll gcd(ll x , ll y){ return y ? gcd(y , x % y) : x;}void add(int x , int pos , int d , bool flag){ int l = t[x].l , r = t[x].r; if (l == r){ if (flag) t[x].vala += d; else t[x].valb += d; return;signed main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); ll cases; cin >> n >> m >> cases; for (int i = 1;i <= m;i++) { cin >> u[i] >> v[i] >> w[i]; e[u[i]].push_back({v[i] , w[i]}); } int mid = l + r >> 1; pushdown(x); if (pos <= mid) add(x << 1 , pos , d , flag); else add(x << 1 | 1 , pos , d , flag); pushup(x);}int query(int x , int a , int b , bool flag){ int l = t[x].l , r = t[x].r; if (a <= l && r <= b) return flag ? t[x].vala : t[x].valb; int mid = l + r >> 1 , ret = 0; pushdown(x); if (a <= mid) ret += query(x << 1 , a , b , flag); if (mid < b) ret += query(x << 1 | 1 , a , b , flag); return ret; }void updatea(int x , int a , int b){ int l = t[x].l , r = t[x].r; if (a == 1 && b == t[x].vala){ t[x].vala = 0 , t[x].lazya = 1; return; bfs(); for (int i = 1;i <= m;i++) ans = gcd(ans , dis[u[i]] - dis[v[i]] + w[i]); ll x , y , z; while (cases--){ cin >> x >> y >> z; z = gcd(z , ans); if ((dis[x] - dis[y]) % z == 0) cout << "Yes\n"; else cout << "No\n"; } if (l == r){ t[x].vala -= b - a + 1; return; } pushdown(x); int mid = l + r >> 1 , tmp = t[x << 1].vala; if (b <= t[x << 1].vala) updatea(x << 1 , a , b); else if (a > t[x << 1].vala) updatea(x << 1 | 1 , a - tmp , b - tmp); else{ updatea(x << 1 , a , tmp); updatea(x << 1 | 1 , 1 , b - tmp); } pushup(x); }void updateb(int x , int a , int b){ int l = t[x].l , r = t[x].r; if (a == 1 && b == t[x].valb){ t[x].valb = 0 , t[x].lazyb = 1; return; } if (l == r){ t[x].valb -= b - a + 1; return; } pushdown(x); int mid = l + r >> 1 , tmp = t[x << 1].valb; if (b <= t[x << 1].valb) updateb(x << 1 , a , b); else if (a > t[x << 1].valb) updateb(x << 1 | 1 , a - tmp , b - tmp); else{ updateb(x << 1 , a , tmp); updateb(x << 1 | 1 , 1 , b - tmp); } pushup(x);}int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n; for (int i = 1;i <= n;i++){ cin >> a[i]; m = max(m , a[i]); } for (int i = 1;i <= n;i++){ cin >> b[i]; m = max(m , b[i]); } build(1 , 1 , m); long long ans = 0; for (int i = 1;i <= n;i++){ int p = query(1 , 1 , a[i] , 1); int q = query(1 , 1 , b[i] , 0); ans += i - min(p , q); if (p != q){ if (p < q){ updateb(1 , p + 1 , q); add(1 , b[i] , q - p , 0); } else{ updatea(1 , q + 1 , p); add(1 , a[i] , p - q , 1); } } add(1 , a[i] , 1 , 1); add(1 , b[i] , 1 , 0); } cout << ans << '\n'; return 0;}} ```