Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分

cf1020D

题意:

交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同。数列保证相邻的两个差值为1或-1;

思路:

构造函数f(x) = a[x] - a[x + n/2] ,由于a数列差值为1或-1,所以可以发现f(x)是连续的。然后就可以用二分了,这种二分的check方式是自己第一次见的。就是通过f(mid)和f(d)的正负来判断区间的移动。其中d是任选的。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/
                const int maxn = 1e5+9;
                ll f[maxn];
                int n;
                void get(int m){
                    int x,y;
                    // cout<<"? "<<m<<endl;
                    printf("? %d\n",m);
                    fflush(stdout);
                    scanf("%d", &x);

                    // cout<<"? "<<m+n/2<<endl;
                    // cin>>y;
                    printf("? %d\n",m + n/2);
                    fflush(stdout);
                    scanf("%d", &y);
                    f[m] = y - x;
                }
int main(){

                // cin>>n;
                scanf("%d", &n);
                int le = 1, ri = n/2;
                get(1);
                if(f[1]==0){
                    printf("! 1\n");
                    fflush(stdout);
                }
                while(le<=ri){
                    int mid = (le + ri) / 2;
                    get(mid);
                    if(f[mid]==0){
                        printf("! %d\n",mid);
                        fflush(stdout);
                        return 0;
                    }
                    if(f[1]>0){
                        if(f[mid]>0){
                            le = mid+1;
                        }
                        else ri = mid - 1;
                    }
                    else {
                        if(f[mid]<0){
                            le = mid+1;
                        }
                        else ri = mid - 1;
                    }
                }
                printf("! -1\n");
                fflush(stdout);

                return 0;
}

cf1020D

   

原文地址:https://www.cnblogs.com/ckxkexing/p/9465492.html

时间: 2024-08-01 21:19:32

Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分的相关文章

Codeforces Round #503 (by SIS, Div. 2) D. The hat

有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cstring> #include <s

Codeforces Round #503 (by SIS, Div. 2)

Codeforces Round #503 (by SIS, Div. 2) https://codeforces.com/contest/1020 A 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <s

Codeforces Round #503 (by SIS, Div. 2) Partial Solution

从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat Problem E Sergey's problem 瞎扯 例行快速切A.B.C. 然后发现D是交互.E也不像是我能做的题,感觉完蛋了. 最后8分钟想出D.狂码代码,然后比赛结束后1分钟过样例. 第二天早上再花4分钟AC.我真是个大菜逼.. 于是这场cf比赛变成了真·手速场.几个friends手速比我

Codeforces Round #503 (by SIS, Div. 2) C. Elections

气死我了人生中第一次打cf就掉分了 A题大水题浪费太多时间囧明明都是A两题亮老师还上分了.. 表示C题打的时候就想到正解啊(而且还更加优秀,因为家里老爷机暴力跑的超龟以为不行 其实是没认真算复杂度),虽然不会证三分性,但是最后还是AC了,暴力1000ms+ 三分40ms+ 看着就很奇淫的题,猛然脑海里就想到二分政党的最后得到的选票,然后发现没有单调性,但好像满足三分 然后每次就贪心,for一遍把那些比1政党本来就有的选票+要多拿的选票还要多的拿到比这个值少1 假如还不够要多拿的,那就在剩下的拿最

Codeforces Round #503 (by SIS, Div. 1)第四题 the hat

原题链接:B. The hat 题意:有n(偶数)个人围成一个圈,每个人身上有一个数字,保证相邻两个人的数字差为1, 现在要把第i个人和第i+n/2个人面对面站着,例如现在有8个人,站好后如下: 1 2 1 2 3 4 3 2 第1个人和第5个人面对面,第2个人和第6个人面对面,以此类推... 现在的问题是你可以询问q(q<=60)次,在n(2<=n<=100000)个人中 找出一对面对面站着且数字相同的人,输出这两个人任意一个人的位置,没找到输出-1: 首先当n%4!=0的时候我们直接

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, i

Educational Codeforces Round 85 (Rated for Div. 2)

Educational Codeforces Round 85 (Rated for Div. 2) A. Level Statistics 签到题, 要求第一位维单增, 第二维变化比第一维慢, 直接模拟即可 B. Middle Class 题目大意 每个人有一些财富, 财富值超过x的人称为富人, 由于实行共产主义, 你可以将某些人的财产重新分配使得富人最多 解题思路 直接按财富值排序, 从大到小加入富人集合, 如果当前富人集合财富平均值小于x就break掉 C. Circle of Monst

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.