cf1556D. Take a Guess
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf1556D. Take a Guess相关的知识,希望对你有一定的参考价值。
题意:
交互题
有n个数和k个询问,你最多只能询问2n次,可以询问任意两个位置数的or或者是and,然后输出这n个数的第k大数
题解:
先说个结论:
x + y = (x or y) + (x and y)
(嘶,加法器的感觉)
如果我们知道x和y的or和and,就可以得到x+y的值
那我们可以这样做,询问a[1]与后面所有数的or和and,这样就知道了所有数与a[1]的和,此时c[i]=a[i]+a[1]
然后我们再询问a[2]和a[3]的or和and,得到了 sum=a[2]+a[3],而c[2]+c[3]=a[2]+a[1]+a[3]+a[1]=sum+2*a[1],这样a[1]就求出来了
从来后面所有数都得到,直接排序输出第k位
代码:
// Problem: D. Take a Guess
// Contest: Codeforces - Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1556/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Data:2021-09-01 00:33:48
// By Jozky
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
read(Ar...);
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
startTime= clock();
freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
endTime= clock();
printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 2e5 + 9;
ll b[maxn], c[maxn], F[maxn];
int main()
{
//rd_test();
int n, k;
read(n, k);
for (int i= 2; i <= n; i++) {
int x;
int sum= 0;
printf("and %d %d\\n", 1, i);
cin >> x;
sum+= x;
printf("or %d %d\\n", 1, i);
cin >> x;
sum+= x;
b[i]= sum;
}
int sum= 0, x;
printf("and %d %d\\n", 2, 3);
cin >> x;
sum+= x;
printf("or %d %d\\n", 2, 3);
cin >> x;
sum+= x;
b[1]= (b[2] + b[3] - sum) / 2;
for (int i= 2; i <= n; i++)
b[i]= b[i] - b[1];
sort(b + 1, b + 1 + n);
printf("finish %d", b[k]);
//rd_test();
//Time_test();
}
以上是关于cf1556D. Take a Guess的主要内容,如果未能解决你的问题,请参考以下文章
cf1556A. A Variety of Operations
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2) D. Take a Guess (交互,位运算性质)(代码片