| A | B | C | D | E | 总 |
|---|---|---|---|---|---|
| AC | 0pts | 10pts | 0pts | 10pts | 120 |
看了十分钟题
A:P15097 [ICPC 2025 LAC] Coatless in Yakutsk
- 本题得分:
- 开始时间:
- 结束时间:
- 总用时:分钟
初始思路
单调性mid越小越容易满足
- 如果比mid小:就穿衣服,如果连穿超过c天就 return false
- 如果比mid大:就洗衣服,一定最优解
考场解题过程
一开始写了个的,后边感觉不够稳妥就写二分了
考场代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int c,n,ans=-1e18,a[N];
int check(int mid){
int sum=0;
for(int i=1;i<=n;i++){
if(a[i]<mid){
sum++;
if(sum>c) return 0;
}else sum=0;
}
return 1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>c>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int l=-1e9,r=1e9;
while(l+1<r){
int mid=l+r>>1;
if(check(mid)) l=mid;
else r=mid;
}
cout<<l;
return 0;
}时间复杂度:
B:P13734 [JOIGST 2025] 雪球 2 / Snowball 2
- 本题得分:
- 开始时间:
- 结束时间:
- 总用时:分钟
初始思路
不知道从哪合并
考场代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,f,a[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
if(i==1) continue;
if(a[i]!=a[i-1]) f=1;
}
if(f==0){
if(a[1]==1) cout<<"Yes";
else cout<<"No";
return 0;
}
cout<<"No";
return 0;
}赛后题解
- 整个合并的过程,看作是一个件数的过程,两个相邻的节点,建一个父节点父节点的权值是两个节点之和
- 两个子节点,节点x和y合并条件是 或
- 考虑任意区间 ,其区间和 ,则x是一个固定值
- 在前缀和数组上,lower_bound找到只为 的位置 ,找得到就继续递归
补题代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,a[N],pre[N];
int dfs(int l,int r){
if(l>=r) return 1;
int ans=pre[r]-pre[l-1];
int x=(ans+1)/2;
int t=pre[l-1]+x;
int pos=lower_bound(pre,pre+n+1,t)-pre;
if(pos<l||pos>r-1||pre[pos]!=t) return 0;
return dfs(l,pos)&&dfs(pos+1,r);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
if(dfs(1,n)) cout<<"Yes";
else cout<<"No";
return 0;
}时间复杂度:
C:P8794 [蓝桥杯 2022 国 A] 环境治理
初始思路
骗分
赛后题解
- 当总共有天时,每个城市被改写的次数是确定的:轮次为 t/n,剩余 [0, t%n-1] 再多改一次 a[i]
- 对于城市 i,j,t 天后 i 和 j 的边权是 max(w[i][j], d[i][j] - cnt[i] - cnt[j])
- floyd 求出任意两点之间的距离 dis(i,j) 并求和
- 显然天数具有单调性,二分加速这个过程
补题代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,a[N],pre[N];
int dfs(int l,int r){
if(l>=r) return 1;
int ans=pre[r]-pre[l-1];
int x=(ans+1)/2;
int t=pre[l-1]+x;
int pos=lower_bound(pre,pre+n+1,t)-pre;
if(pos<l||pos>r-1||pre[pos]!=t) return 0;
return dfs(l,pos)&&dfs(pos+1,r);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
if(dfs(1,n)) cout<<"Yes";
else cout<<"No";
return 0;
}时间复杂度:
D:P8161 [JOI 2022 Final] 自学 / Self Study
初始思路
骗分
赛后题解
题意:有 m 周,n 门课,共 m*n 小时。每一周每门课都有 1 小时上课机会。如果选择上课,拿到 A[i] 的分数;如果选择自学,拿到 B[j] 的分数。求最低分的课程最大化。
- 对于课程i,,则一定会上课,否则一定是轮到课程的时候自学,甚至在课程时再补充自学;
- 最低分具有单调性,考虑二分;
- check 时计算每门课不低于 mid 分需要的小时,并求和 sum,检查 sum是否小于n*m。
补题代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+5;
int n,m,a[N],b[N];
int cl(int x,int y){
return (x+y-1)/y;
}
int check(int x){
int ans=0;
for(int i=1;i<=n;i++){
int cost=0;
if(a[i]<b[i]) cost=cl(x,b[i]);
else{
if(x>m*a[i]){
int sum=x-m*a[i];
cost=m+cl(sum,b[i]);
}else cost=cl(x,a[i]);
}
ans+=cost;
if(ans>n*m) return 0;
}
return ans<=n*m;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
int maxi=0;
for(int i=1;i<=n;i++){
cin>>a[i];
maxi=max(maxi,a[i]);
}
for(int i=1;i<=n;i++){
cin>>b[i];
maxi=max(maxi,b[i]);
}
int l=-1,r=m*maxi+1;
while(l+1<r){
int mid=l+r>>1;
if(check(mid)) l=mid;
else r=mid;
}
cout<<l;
return 0;
}
时间复杂度:
E:P8161 [JOI 2022 Final] 自学 / Self Study
初始思路
写了个双指针,但是这个双指针并不能一定找到最优解
赛后题解
此题一共其实只有4种情况
- 一直向右
- 一直向左
- 向右后向左
- 向左后向右
又因为最少一个点不到,最多到n个点,点越少越容易到,越多越不容易到的单调性,考虑二分答案再把四种情况判一下就可以了
补题代码
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5;
int t,n,a[N];
int check(int x){
for(int r=x;r<=n;r++){
int l=r-x+1;
if(a[r]<=0) if(-a[l]<=t) return 1;
if(a[l]>=0) if(a[r]<=t) return 1;
if(a[l]<=0&&a[r]>=0){
if(-a[l]+a[r]-a[l]<=t) return 1;
if(a[r]+a[r]-a[l]<=t) return 1;
}
}
return 0;
}
int main(){
cin>>t>>n;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+1+n);
int l=-1,r=n+1;
while(l+1<r){
int mid=l+r>>1;
if(check(mid))l=mid;
else r=mid;
}
cout<<l;
return 0;
} 时间复杂度: