版本比较
2026酷丁CSP-S复赛模拟赛1 复盘&题解
正文
增加 125 行删除 2 行
14 行未改动15161718192019202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015114 行未改动这个 T3 比较难评,感觉题解应该是 AI 生成的,不过后缀数组好像不在 CSP-S 的考纲里。
对于本次比赛,gu_ga 给出了以下的难度评价:
T1 < T4 < T3
|题目|预估难度|知识点||:-:|:-:|:-:||T1|${\color{#F39C11}\text{普及}-}$|贪心||T2|疑似错题?|<||T3|${\color{#9C3DCF}\text{省选/NOI}-}$|后缀数组、LCP、字符串哈希||T4|${\color{#13C2C2}提高}$|dfs序、线段树/树状数组|
## 题解
### T1
很经典的贪心问题啊,下面给出贪心推导。
如何比较 $(w_i,t_i)$ 和 $(w_j,t_j)$ 哪个最优?
设已经花费了 $k$ 的时间,那么:
- 若先处理 $(w_i,t_i)$ $$\begin{aligned}
成本 &= w_i(k+t_i) + w_j(k+t_i+t_j) \\ &= k(w_i+w_j) +t_iw_i +t_jw_j + t_iw_j
\end{aligned}$$
- 若先处理 $(w_j,t_j)$ $$\begin{aligned}
成本 &= w_i(k+t_i+t_j) + w_j(k+t_j) \\ &= k(w_i+w_j) +t_iw_i + t_jw_j + t_jw_i
\end{aligned}$$
等同于比较 $t_iw_j$ 和 $t_jw_i$。
::::success[AC code]```cpp#include <iostream>#include <algorithm>#define int long longusing namespace std;const int N=1e5+5;struct abc{ int t,w;}a[N];int n,t,ans;signed main(){ freopen("schedule.in","r",stdin); freopen("schedule.out","w",stdout); scanf("%lld",&n); for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i].t,&a[i].w); sort(a+1,a+n+1,[](abc p,abc q){ return p.t*q.w<q.t*p.w; } ); for(int i=1;i<=n;i++){ t+=a[i].t; ans+=t*a[i].w; } printf("%lld",ans); return 0;}```::::
### T2&T3
T2 被 hack 了,就没有题解。
T3 难度超纲,后面专门拉出来一栏去讲。
### T4
::::success[AC code]```cpp#include <iostream>#include <algorithm>#include <vector>#define mid (l+r>>1)using namespace std;const int N=5e5+5;int n,q,tot;int a[N],dfn[N],out[N],s[N<<2],ans[N];vector<int> g[N];struct node{ int v,id; }b[N];struct ask{ int u,x,id; }c[N];void dfs(int u,int fa){ dfn[u]=++tot; for(int v:g[u]) if(v!=fa) dfs(v,u); out[u]=tot;}void up(int p){ s[p]=s[p<<1]+s[p<<1|1]; }void update(int p,int l,int r,int x){ if(x<l||x>r) return ; if(l==r){ s[p]++; return; } update(p<<1,l,mid,x); update(p<<1|1,mid+1,r,x); up(p);}int query(int p,int l,int r,int x,int y){ if(r<x||l>y) return 0; if(x<=l&&r<=y) return s[p]; return query(p<<1,l,mid,x,y)+query(p<<1|1,mid+1,r,x,y);}int main(){ freopen("city.in","r",stdin); freopen("city.out","w",stdout); scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1,u,v;i<n;i++){ scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } dfs(1,0); for(int i=1;i<=n;i++) b[i]={a[i],i}; sort(b+1,b+n+1,[](node p,node q){ return p.v>q.v; } ); for(int i=1;i<=q;i++) scanf("%d%d",&c[i].u,&c[i].x),c[i].id=i; sort(c+1,c+q+1,[](ask p,ask q){ return p.x>q.x; } ); int p=1; for(int i=1;i<=q;i++){ while(p<=n&&b[p].v>=c[i].x) update(1,1,n,dfn[b[p++].id]); ans[c[i].id]=query(1,1,n,dfn[c[i].u],out[c[i].u]); } for(int i=1;i<=q;i++) printf("%d\n",ans[i]); return 0;}
```::::