Mex Tower (Easy version)
做法
可以发现 的最大值必然为 ,故发现可能与 有关,那么可以从 的时候开始递推。递推过程大致如下:
2 1 0 2
0 2 1
1 0
2故可以发现规律为
code
懒得放了,🧠没有进🚾的人应该都能写得出来
Sunset
做法
我们从大到小,从 枚举当前数组的最大值,由于一次最多只能用 次操作 () ,故采用二分。一个数是最大值当且仅当 ,其中 代表 中 不同的数量,那么我们只用找到第一个位置和 相等的位置即可。
为什么可以二分
在一个数组 中,令最大值是 ,那么 以后所有的前缀最大值都相等,而越往前,由于最大值变小,则 严格小于 ,由于 是数组 的子数组,所以 数组也是严格递增的。
那么对于 ,由于 是最大值,那么后面的 都会相等,所以 不会变化。古我们只用找到最前面的 使得 即可。
我们找到我们当前枚举的的最大值位置后直接删除即可。
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
ll t,n;
ll a[505];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
cin>>n;
ll l=1,r=n;
for(int i=1;i<=n;++i){
a[i]=0;
}
for(int i=n;i>=1;--i){
ll num;
cout<<"?"<<" "<<"1"<<" "<<n<<endl;
cin>>num;
ll l=1,r=n;
ll ans=n;
while(l<=r){
ll mid=(l+r)>>1;
cout<<"?"<<" "<<"1"<<" "<<mid<<endl;
ll sb;
cin>>sb;
if(sb==num){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
cout<<"?"<<" "<<2<<" "<<ans<<endl;
a[ans]=i;
}
cout<<"!"<<" ";
for(int i=1;i<=n;++i){
cout<<a[i]<<" ";
}
cout<<endl;
}
}
Omkar and Duck
[题面](https://www.luogu.com.cn/problem/CF1392E
做法
构造方式形如:
那么对于一个 ,我们把它变成二进制,然后对于 的位模拟即可。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[30][30];
signed main(){
int n, q;
cin >> n;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i & 1){
a[i][j] = 0;
}
else{
a[i][j] = 1ll << (i + j);
}
cout << a[i][j] << " ";
}
cout << endl;
}
cin >> q;
while (q--){
ll k;
cin >> k;
int x = 0, y = 0;
for (int i = 1; i < 2 * n; i++){
cout << x + 1 << " " << y + 1 << endl;
if (k >> i & 1){
if (x & 1){
x++;
}
else{
y++;
}
}
else{
if (x & 1){
y++;
}
else{
x++;
}
}
}
}
return 0;
}CF2036G Library of Magic
做法
第一眼看到很容易发现两个相同的数异或起来是 ,所以我们为了求出 ,我们可以二分第一个位置 ,使得 ,那么 的位置就是 ,后面的数也可以这样, 不需要二分,只需要先问一次 然后异或上 和 即可。但是我们很快会发现, 有可能等于 ,为了使他们的异或值为 ,只有一种可能:
那我们只用从小到大枚举这个 即可先求出 ,然后 和 也就根据上面的式子推出来了。
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
ll ask(ll l,ll r){
cout<<"xor "<<l<<" "<<r<<endl;
cout.flush();
ll res;
cin>>res;
if(res==-1) exit(0);
return res;
}
ll find_first(ll L,ll R)
{
ll l=L-1;
ll r=R;
while(l<r)
{
ll mid=l+(r-l+1)/2;
ll num=ask(L,mid);
if(num!=0)
{
r=mid-1;
}
else
{
l=mid;
}
}
return l+1;
}
ll t,n;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
cin>>n;
if(n==3){
cout<<"ans"<<" "<<1<<" "<<2<<" "<<3<<endl;
cout.flush();
continue;
}
ll S;
S=ask(1,n);
ll a,b,c;
if(S!=0){
ll posa=find_first(1,n);
ll posb=find_first(posa+1,n);
c=S^posa^posb;
a=posa;
b=posb;
}
else{
ll x=(1LL << (63 - __builtin_clzll(n)))-1;
ll a_val,st;
while(true)
{
if(x==1)
{
a_val=1;
st=2;
break;
}
ll v=ask(1,x);
if(v!=0)
{
a_val=v;
st=x+1;
break;
}
x=x>>1;
}
a=a_val;
b=find_first(st,n);
c=a^b;
}
cout<<"ans"<<" "<<a<<" "<<b<<" "<<c<<endl;
cout.flush();
}
return 0;
}P17121 [ICPC 2025 Shanghai R] Menji, we miss you!
做法
首先我们可以二分找到 所在的深度,这十分好实现,我们不断地问 ask(1,mid) 即可。接下来,有了 的深度,我们就可以在 的上部查询了。由于这是一棵二叉树,保证联通,我们可以使用 在所有删去某个结点后得到的最大连通分量大小中,删去结点 𝑣
时所得到的值最小 这一条件在查询重心 ,假如我们当前求出的重心 进行询问后发现 ask(root,dep[x]-dep[root]) 返回值是 ,我们就往 的子树里面继续找,否则把这个节点标记为已访问,让后继续往上找即可。
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
const ll N=3e4+5;
ll n,dep[N],root,tot_size,siz[N],xd,mn,p;
bool vis[N];
vector<ll> g[N];
ll ask(ll u,ll k){
cout<<"?"<<" "<<u<<" "<<k<<endl;
ll x;
cin>>x;
return x;
}
void dfs(ll u){
siz[u]=1;
for(ll v:g[u]){
if(!vis[v]){
dfs(v);
siz[u]+=siz[v];
}
}
if(dep[u]<=xd&&max(siz[u],tot_size-siz[u])<mn){
mn=max(siz[u],tot_size-siz[u]),p=u;
}
}
void solve(){
cin>>n;
root=1;
tot_size=n;
for(int i=1;i<=n;++i){
vis[i]=0;
g[i].clear();
}
ll max_dep=0;
for(int i=2;i<=n;++i){
ll f;
cin>>f;
g[f].push_back(i);
dep[i]=dep[f]+1;
max_dep=max(max_dep,dep[i]);
}
ll l=0,r=max_dep;
while(l<=r){
ll mid=(l+r)>>1;
if(ask(1,mid)){
r=mid-1;
xd=mid;
}
else{
l=mid+1;
}
}
while(1){
mn=n+1;
dfs(root);
if(ask(p,xd-dep[p])){
root=p;
tot_size=siz[p];
if(dep[p]==xd){
break;
}
}
else{
vis[p]=1;
}
}
cout<<"!"<<" "<<p<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--){
solve();
}
}