[ICPC 2025 APC] Secret Lilies and Roses
[ICPC 2025 APC] Secret Lilies and Roses
神秘的题,神秘的观察。
- 假设我们确定了玫瑰的总数为 \(t\),此时
answer t即可
能这样做的原因很经典,我们看成初始时玫瑰在左,百合在右。此时可以看成左边的 \(n\) 朵玫瑰喝右边的 \(n\) 朵百合交换,左边仍然有 \(x\) 朵花,故此时左边的百合与右边的玫瑰等量。
考虑如何解出 \(x\),我们需要用到乘法操作。
假设我们可以找到两朵相邻的不同花朵,设两者位置分别为 \(x\) 和 \(x+1\),询问 \(l_{x-1} \times r_{x-1} = A\) 和 \(l_{x+1} \times r_{x+1} = B\),此时有 \(l_{x+1} = l_{x-1} + 1\),\(r_{x+1} = r_{x-1} - 1\)。
考虑 \(t = x + r_x - l_x\),此时我们有 \(l_{x-1} \times r_{x-1} = A\) 且 \((l_{x-1} + 1) \times (r_{x-1} - 1) = B\),即 \(l_{x-1} \times r_{x-1} - l_{x-1} + r_{x-1} - 1 = B\),即 \(r_{x-1} - l_{x-1} = B - A + 1\),\(t = B - A + x\)。接下来的问题就变成了找到两朵相邻的不同花朵。
然而这可能不能实现,所以我们要在 \(0\) 处和 \(n+1\) 处插入两朵花。同时为了不影响询问结果,我们钦定 \(0\) 处的花为玫瑰,\(n+1\) 处的花为百合。
接下来二分找到左玫瑰右百合的对即可,次数可以通过。
//Ad astra per aspera
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int n;
int query(int pos){
if(pos<0 || pos>n){
return 0;
}
cout<<"multi "<<pos<<endl;
int ans;
cin>>ans;
return ans;
}
int main(){
int T;
cin>>T;
while(T--){
cin>>n;
int l=0,r=n+1;
while(r-l>=2){
int mid=(l+r)>>1;
cout<<"type "<<mid<<endl;
string s;
cin>>s;
if(s=="lily"){
r=mid;
}
else{
l=mid;
}
}
int A=query(l-1);
int B=query(r);
cout<<"answer "<<B-A+l<<endl;
}
return 0;
}

浙公网安备 33010602011771号