2025牛客寒假算法基础集训营3 ptlks的题解

有点上压力了

A.智乃的博弈游戏

题意:

博弈,每次能取与当前数互质的数。

思路

偶数必输,奇数取n-2能变成2,必赢。

代码

点击查看代码

C.智乃的Notepad(Easy version)

题意:

能敲字母和退格,求打出所有单词的最小次数。

思路

其实就是字典树的大小,能够省的只有最长的那条链。

代码

点击查看代码
void solve() {
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>op[i];
	}
	int l,r;
	cin>>l>>r;
	int mx=0;
	for(int i=l;i<=r;i++){
		t.insert(op[i]);
		mx=max(mx,(int)op[i].length());
	}
	t.dfs(0);
	cout<<t.sum[0]*2-mx-2<<endl;
}

E.智乃的小球

题意:

小球的碰撞可以看做相遇但不影响速度。

思路

直接二分答案即可。

代码

点击查看代码
void solve() {
	int n,k;
	cin>>n>>k;
	vector<int>s1,s2;
	for(int i=1;i<=n;i++){
		int x,v;
		cin>>x>>v;
		if(v==1){
			s1.push_back(x*2);
		}else{
			s2.push_back(x*2);
		}
	}
	sort(s1.begin(),s1.end());
	sort(s2.begin(),s2.end());
	int l=0,r=2e9;
	int ans=-1;
	while(l<=r){
		int mid=(l+r)>>1;
		int s=0;
		for(auto x:s1){
//			cout<<upper_bound(s2.begin(),s2.end(),x+mid*2)-s2.begin()-(upper_bound(s2.begin(),s2.end(),x)-s2.begin())<<' ';
			s+=upper_bound(s2.begin(),s2.end(),x+mid*2)-s2.begin()-(upper_bound(s2.begin(),s2.end(),x)-s2.begin());
		}
//		cout<<mid<<' '<<s<<endl;
		if(s>=k){
			ans=mid;
			r=mid-1;
		}else{
			l=mid+1;
		}
	}
	if(ans!=-1){
		cout<<"Yes\n";
		cout<<fixed<<setprecision(10)<<ans/2.0<<endl;
	}else{
		cout<<"No\n";
	}
}

F.智乃的捉迷藏

题意:

判断小球数量是否合法

思路

一个球最少被1个人看到,最多被2个人看到,按此判断即可。

代码

点击查看代码
void solve() {
	int n,x,y,z;
	cin>>n>>x>>y>>z;
	if(x+y+z>n*2||x+y+z<n){
		cout<<"No\n";
	}else{
		cout<<"Yes\n";
	}
}

L.智乃的三角遍历

题意:

构造题

思路

随便找一条路然后输出即可。

代码

点击查看代码
void solve() {
	int n;
	cin>>n;
	cout<<"Yes\n";
	cout<<1<<' ';
	int p=1;
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			p+=j;
			cout<<p<<' ';
		}
		for(int j=n;j>=i+1;j--){
			cout<<p+1<<' ';
			p-=j;
			cout<<p<<' ';
		}
		p++;
		cout<<p<<' ';
//		cout<<endl;
	}
	for(int j=n+1;j>=2;j--){
		p-=j;
		cout<<p<<' ';
	}
}

M.智乃的牛题

题意:

签到

思路

签到

代码

点击查看代码
posted @ 2025-02-09 17:25  ptlks  阅读(21)  评论(0)    收藏  举报