跳到正文

个人记录

2-暑假三期上午训练课-410谭

AK_lwh_888约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年8月27日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 5 分钟

版本

  1. v1当前

2-暑假三期上午训练课-410谭

[USACO17FEB] Why Did the Cow Cross the Road II S

这道题我们可以用前缀和来写,如果这个位置有坏灯,就将上一个位置加一,这样只要求出左端点和右端点就可以直接求出这个区间里有多少个坏灯。

已知区间长度为 kk,所以我们只要枚举左端点就可以知道右端点为左端点 ++ 长度 −- 11。而不能出界所以左端点的范围是右端点 <=<= nn。

然后我们只要用前缀和公式求出这个区间有多少坏灯,我们只要找一个坏灯最少的区间就行了。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N],n,m,k,ans=1e9,sum[N];
bool vis[N];
signed main(){
	cin>>n>>k>>m;
	for(int i=1;i<=m;++i){
		cin>>a[i];
		vis[a[i]]=true;
	}
	for(int i=1;i<=n;++i){
		sum[i]=sum[i-1];
		if(vis[i])sum[i]++;
	}
	for(int l=1;l+k-1<=n;++l){
		int r=l+k-1;
		ans=min(ans,sum[r]-sum[l-1]);
	}
	cout<<ans<<"\n";
	return 0;
}

[语言月赛202306] 演唱会

这道题直接模拟就行,因为输入量较大,所以要关闭同步流。

因为输入量大,而且那么大的数后面也用不上,所以可以用一维的存起来。然后再输入的时候就记录一下她最喜欢的歌的编号,然后输入的时候也要记得记录一下每首歌学生喜爱度总和和编号。

然后从大到小排个序,这样前 mm 首歌就是本该输出的,然而题目说了,如果前 mm 首有她最喜欢的歌则将那首歌放在第一位,没有则将那首歌放在最后。所以我就直接判断有没有,有的话直接输出那首歌的编号,然后照常输出,只要不重复输出那首歌就行,如果没有就直接将最后一首歌替换成她最喜欢的一首歌。

C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N=1e5+10;
int n,m,a1,b,a[N];
int x,maxx=-1e9;
struct node{
	int id,val;
};
node sum[N];
bool cmp(node x,node y){
	return x.val>y.val;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>m>>a1>>b;
	for(int i=1;i<=a1;++i){
		for(int j=1;j<=n;++j){
			cin>>a[j];
			if(i==b){
				if(maxx<=a[j]){
					maxx=a[j];
					x=j;
				}
			}
			sum[j].val+=a[j],sum[j].id=j;
		}
	}
	sort(sum+1,sum+1+n,cmp);
	bool flag=false;
	for(int i=1;i<=m;++i){
		if(sum[i].id==x)flag=true;
	}
	if(flag==false){
		sum[m].id=x;
		for(int i=1;i<=m;++i){
			cout<<sum[i].id<<" ";
		}
		cout<<"\n";
	}
	else{
		cout<<x<<" ";
		for(int i=1;i<=m;++i){
			if(sum[i].id!=x){
				cout<<sum[i].id<<" ";
			}
		}
		cout<<"\n";
	}
	return 0;
}

[蓝桥杯 2024 国 Python B] 切木棒

这道题要我们求可以任意切 mm,最后要保证切后木棒最大值最小。这道题我们可以直接用二分答案来二分最短值。

而我们的 checkcheck 函数很重要:

先枚举每个木棒,求出切成最大不超过 xx 的木棒要多少次,如果总共也不会超过 mm 次那么则是答案正确,否则答案错误。

C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N=3e5+10;
int a[N],n,m,maxx=-1e9;
bool check(int x){
	int sum=0;
	for(int i=1;i<=n;++i){
		sum+=a[i]/x;
		if(a[i]%x==0){
			sum--;
		}
	}
	return sum<=m;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		cin>>a[i];
        maxx=max(maxx,a[i]);
	} 
	int l=0,r=maxx;
	a[0]=-1e9,a[n+1]=1e9;
	while(l+1<r){
		int mid=(l+r)/2;
		if(check(mid))r=mid;
		else l=mid;
	}
	cout<<r<<"\n";
	return 0;
} 
原文