前言
OIer 们好呀,这里是 2026酷丁CSP-S复赛模拟赛1 复盘&题解,欢迎大家观看。(我的洛谷账号)
有需要的 OIer 可以点击蓝字查看题面。
复盘
本次比赛感觉出锅了啊,T2 的题解被我 hack 了,甚至数据大小还没拉满就 TLE。
这个 T3 比较难评,感觉题解应该是 AI 生成的,不过后缀数组好像不在 CSP-S 的考纲里。
对于本次比赛,gu_ga 给出了以下的难度评价:
| 题目 | 预估难度 | 知识点 |
|---|---|---|
| T1 | 贪心 | |
| T2 | 疑似错题? | |
| T3 | 后缀数组、LCP、字符串哈希 | |
| T4 | dfs序、线段树/树状数组 | |
题解
T1
很经典的贪心问题啊,下面给出贪心推导。
如何比较 和 哪个最优?
设已经花费了 的时间,那么:
- 若先处理
- 若先处理
等同于比较 和 。
AC code
#include <iostream>
#include <algorithm>
#define int long long
using 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
AC code
#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;
}