当前位置:网站首页>CFdiv2-Tournament Countdown-(思维+交互题套路)
CFdiv2-Tournament Countdown-(思维+交互题套路)
2022-08-08 02:36:00 【可爱美少女】
题意:
就是说,给你2n 个人,然后两个人比赛,胜的晋级输的推掉。就是1vs2,3vs4,…。然后你每次可以询问a和b,他俩谁赢的多,如果返回1a多,2b多,0一样多。然后你最多查询1/3*2n+1 次。问你最终的胜者是谁。
思考:
1.当时看了看样例的图,如果最暴力的比较方法,要比较n-1次(比如n是总人数)。但是题目最多让你查询(2n+2)/3次。刚开始没啥感觉,刚开始我想先把一半用暴力求出来,这样是n/2次查询,但是另一半怎么凑呢,如果n/4也不行,所以就不行了。
2.然后想到交互题的查询次数都是很有意义的,为什么是这个。既然我用暴力可以n-1次求出来,最多查询(2n+2)/3次,那不就是暴力次数的2/3?也就是3场比赛你要用两次查咨求出来就可以了。只要想到这,题目就好处理了,既然是3场比赛,那么就是4个人,所以4个4个的看。
3.比如1 2 3 4
如果1=3,那么1和3都是输掉的,因为如果他俩都是胜者,那么晋级后肯定要有一个比较大。所以再比较晋级的2和4就可以了。
如果1>3,那么1肯定晋级,再查询1和4,比较一下就可以了。
如果1<3,那么3肯定晋级,再查询3和2,比较一下就可以了。
4.注意细节,数组循环什么时候停止,要用最直观的写法。这样最不容易错。
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 2e5+10,M = 2010;
int T,n,m,k;
int va[N];
vector<int > v,vt;
int query(int a,int b)
{
cout<<"? "<<a<<" "<<b<<"\n";cout.flush();
int x;cin>>x;
return x;
}
void solve()
{
int anw = 0;
while(1)
{
if(v.size()%4==0)
{
vt.clear();
for(int i=0;i+3<=(int)v.size()-1;i+=4)
{
int a = v[i],b = v[i+1],c = v[i+2],d = v[i+3];
int t1 = query(a,c);
if(t1==0)
{
int t2 = query(b,d);
if(t2==1) vt.pb(b);
else vt.pb(d);
}
if(t1==1)
{
int t2 = query(a,d);
if(t2==1) vt.pb(a);
else vt.pb(d);
}
if(t1==2)
{
int t2 = query(c,b);
if(t2==1) vt.pb(c);
else vt.pb(b);
}
}
v = vt;
}
else
{
int a = v[0],b = v[1];
int t1 = query(a,b);
if(t1==1) anw = a;
else anw = b;
break;
}
}
cout<<"! "<<anw<<"\n";cout.flush();
}
signed main()
{
cin>>T;
while(T--)
{
cin>>n;
v.clear();
n = (1ll<<n);
for(int i=1;i<=n;i++) v.pb(i);
solve();
}
return 0;
}
总结:
多多思考,总结。
边栏推荐
- Detailed QML ListView
- STFW3N150管脚功能 数据表(PDF)引脚图
- PC博物馆(5) 1975 IMSAI 8080
- 意识的概念框架:我们的意识从注意图式产生?
- STM32F103C8/BT6 USART1 DMA发送失败
- Where to open a futures account with low fees and high returns
- Overseas Metaverse media must pay attention to the integrity of the propaganda
- 类和对象的深度剖析
- new和malloc的区别
- prometheus学习1部署了解
猜你喜欢
随机推荐
嵌入式面试题
PTA Exercise 1.8 Binary Search
新手使用 go channel 需要注意的问题
巧记硬链接与软链接
PAT甲级 1057 Stack
new和malloc的区别
stack push and pop sequence
PAT甲级 1060 Are They Equal
黑马jvm课程笔记d1
LeetCode二叉树系列——144.左叶子之和
PC博物馆(5) 1975 IMSAI 8080
【程序员的七夕浪漫时刻】
[Actual combat explanation] Implementation of data blood relationship
CS8630 无效的 nullable 值: C# 7.3 的“Enable”
陈强教授《机器学习及R应用》课程 第十章作业
From hardcover to smartcover, the next wave is emerging. Listen to what smart home gurus have to say?
陈强教授《机器学习及R应用》课程 第七章作业
sci 顶刊中的 3D 密度函数图
Leetcode.86 分隔链表(使用哑结点模拟)
hreg说明备忘







