1. 程式人生 > >EOJ Monthly 2018.11 D. 猜價格

EOJ Monthly 2018.11 D. 猜價格

猜價格

分兩種情況討論:

kn,先猜至多 k 次 1,由於回答 <1 肯定是假的,所以可以把剩餘系下是哪次錯試出來,然後用至多 n 次搞定。

k>n,每個數都猜兩次,如果兩次結果不一樣,再猜第三次確定哪個是真的,之後就可以當它一直說真話了(n 次內不會再說假話)

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace
std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define
piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head char s[10], t[10]; int main() { int n, k; scanf("%d %d", &n, &k); LL l
= 1, r = (1LL<<n)-1, m = l+r >> 1; if(k <= n) { for (int i = 1; i <= k; i++) { printf("1\n"); fflush(stdout); scanf("%s", s); if(s[0] == '=') exit(0); else if(s[0] == '>') break; } int cnt = 0; while(l < r) { ++cnt; printf("%lld\n", m); fflush(stdout); scanf("%s", s); if(cnt%k == 0) { if(s[0] == '=') exit(0); else if(s[0] == '>') l = m+1; else r = m-1; } else { if(s[0] == '=') exit(0); else if(s[0] == '>') r = m-1; else l = m+1; } m = l+r >> 1; } printf("%lld\n", m); fflush(stdout); } else { bool f = true; while(l < r) { if(f) { printf("%lld\n", m); fflush(stdout); scanf("%s", s); printf("%lld\n", m); fflush(stdout); scanf("%s", t); if(s[0] != t[0]) { f = false; continue; } else if(s[0] == '=') exit(0); else if(s[0] == '>') r = m-1; else l = m+1; } else { printf("%lld\n", m); fflush(stdout); scanf("%s", s); if(s[0] == '=') exit(0); else if(s[0] == '>') r = m-1; else l = m+1; } m = l+r >> 1; } printf("%lld\n", m); fflush(stdout); } return 0; }