跳到正文

题解

题解:CF2162D Beautiful Permutation

MarsPlan2026约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年4月5日
更新
2026年5月10日
篇幅
约 2 分钟
题目
CF2162D

版本

  1. v1当前

题目传送门

this

背景

早上打了这场比赛,没打过交互题的我郑重宣布,此题无解 有解,看了 Y 学长的题解后深受启发,即发此文。

解题思路

首先,本题我们其实只用得上 p 数组的前缀和,以及 a 数组的前缀和。

既然是前缀和,那么就可以用二分维护,寻找具体第一个

代码
a[1]+a[2]+...+a[i]>p[1]+p[2]+...p[i]

最小的满足以上条件的 i 即为左端点 l

然后用

代码
a[1]+a[2]+...+a[n]-(p[1]+p[2]+...+p[n])

来求区间长度,实际以上式子可以缩减为

代码
a[1]+a[2]+...+a[n]-n*(n+1)/2

代码

C++
#include <bits/stdc++.h>
using namespace std;

int ask(int p,int x,int y){
	cout<<p<<" "<<x<<" "<<y<<endl;
	int res;
	cin>>res;
	return res;
}
void chk(int x,int y){
	cout<<"! "<<x<<" "<<y<<endl;
}
int t,n;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		int m=ask(2,1,n)-n*(n+1)/2;
		int l=1,r=n;
		int ans=0;
		while(l<=r){
			int mid=(l+r)>>1;
			int k=ask(2,1,mid)-ask(1,1,mid);
			if(k>0){
				ans=mid;
				r=mid-1;
			}else{
				l=mid+1;
			}
		}
		chk(ans,ans+m-1);
	}
}
原文