码迷,mamicode.com
首页 > 其他好文 > 详细

交互题

时间:2019-02-13 22:50:21      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:ret   col   iostream   namespace   ctime   人才   个人   ||   mes   

https://vjudge.net/contest/283149#problem/I 专题

Subway Pursuit

题意:猜火车的位置,

每次区间缩小到一定程度就可以用rand函数猜一个数字,猜对了就结束,猜错了之后,在这个区间火车可能会向左走K,向右走K。4k是一个人为订的范围,指区间变得这么大的时候就才一个数。大一点也行。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;

ll n, k;
char s[10];

int main() {
    srand(time(0));
    scanf("%lld%lld", &n, &k);
    ll l = 1, r = n;
    while(1){
        if(r - l <= (ll)4 * k){
            ll x = rand() % (r - l + 1) + l;
            printf("%lld %lld\n", x, x);
            fflush(stdout);
            scanf("%s", s);
            if(s[0] == Y || s[0] == B) return 0;
            else{
                l = max(1ll, l - k);
                r = min(n, r + k);
            }
        }
        else{
            ll mid = (l + r) / 2;
            printf("%lld %lld\n", l, mid);
            fflush(stdout);
            scanf("%s", s);
            if(s[0] == B) return 0;
            else if(s[0] == Y){
                l = max(1ll, l - k);
                r = min(n, mid + k);
            }
            else{
                l = max(1ll, mid + 1 - k);
                r = min(n, r + k);
            }
        }
    }
    return 0;
}

 The hat

 尽量还是用cout

相邻的两个人相差1或-1(圆的), 求那个人与对面那个人的树枝相等。必须是4K个人才有可能相等。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxm = 1e5 + 5;
typedef long long ll;
int  n;
int fun(int x) {
int l ,r;
cout << "? " << x <<endl;
cin >> l;
cout << "? " << x + n / 2 << endl;
cin >> r;
if(r - l == 0) {
cout << "! " << x <<endl;
    exit(0);
}
return r - l;
}


int main() {
cin >> n;
if(n % 4) {
cout << "! -1" << endl;
    return 0;
}
int l = 1, r = n / 2, mid;
while(l <= r) {
    mid = (l + r) / 2;
    ll vl = fun(l);
    ll ml = fun(mid);
    if(vl * ml < 0) {
        r = mid;
    }
    else {
        l = mid + 1;
    }
}
cout << "! -1" << endl;
return 0;
}

 

交互题

标签:ret   col   iostream   namespace   ctime   人才   个人   ||   mes   

原文地址:https://www.cnblogs.com/downrainsun/p/10371978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!