Glad to see you! CodeForces - 810D (交互+二分)
This is an interactive problem. In the output section below you will see the information about flushing the output.
On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in Vičkopolis. Upon arrival, they left the car in a huge parking lot near the restaurant and hurried inside the building.
In the restaurant a polite waiter immediately brought the menu to Leha and Noora, consisting of n dishes. It is interesting that all dishes in the menu are numbered with integers from 1 to n. After a little thought, the girl ordered exactly kdifferent dishes from available in the menu. To pass the waiting time while the chefs prepare ordered dishes, the girl invited the hacker to play a game that will help them get to know each other better.
The game itself is very simple: Noora wants Leha to guess any two dishes among all ordered. At the same time, she is ready to answer only one type of questions. Leha can say two numbers x and y (1 ≤ x, y ≤ n). After that Noora chooses some dish a for the number x such that, at first, a is among the dishes Noora ordered (x can be equal to a), and, secondly, the value is the minimum possible. By the same rules the girl chooses dish b for y. After that Noora says «TAK» to Leha, if
, and «NIE» otherwise. However, the restaurant is preparing quickly, so Leha has enough time to ask no more than 60 questions. After that he should name numbers of any two dishes Noora ordered.
Help Leha to solve this problem!
Input
There are two numbers n and k (2 ≤ k ≤ n ≤ 105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered.
Output
If you want to provide an answer, output a string of the form 2 x y (1 ≤ x, y ≤ n, x ≠ y), if you think the dishes x and y was among dishes ordered by Noora. After that, flush the output and terminate your program.
Interaction
While helping Leha, you can ask queries to Noora no more than 60 times. Each query should be printed in it's own line and have the form 1 x y (1 ≤ x, y ≤ n). You have to both print the end-of-line character and flush the output. After flushing you should read the answer for this query from input.
After each query jury's program will print one line «TAK» or «NIE» (without quotes) in input stream depending on the girl's answer.
To flush you can use (just after printing an integer and end-of-line):
- fflush(stdout) in C++;
- System.out.flush() in Java;
- stdout.flush() in Python;
- flush(output) in Pascal;
- see the documentation for other languages.
Hacking
For hacking you should write numbers n and k (2 ≤ k ≤ n ≤ 105) in the first line and, for describing dishes Noora ordered, k different integers a1, a2, ..., ak (1 ≤ ai ≤ n), written in ascending order in the second line. Of course, solution you want to hack won't be able to read the numbers of ordered dishes.
Example
3 2
NIE
TAK
NIE
TAK
TAK
TAK
1 1 2
1 2 1
1 1 3
1 3 1
1 2 3
1 3 2
2 2 3
Note
There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (x = 1), then she'll choose the second dish (a = 2) as the dish with the minimum value . For the second (x = 2) and the third (x = 3) dishes themselves will be optimal, because in that case
.
Let Leha asks Noora about the next couple of dishes:
- x = 1, y = 2, then he'll recieve «NIE» answer, because |1 - 2| > |2 - 2|
- x = 2, y = 1, then he'll recieve «TAK» answer, because |2 - 2| ≤ |1 - 2|
- x = 1, y = 3, then he'll recieve «NIE» answer, because |1 - 2| > |3 - 3|
- x = 3, y = 1, then he'll recieve «TAK» answer, because |3 - 3| ≤ |1 - 2|
- x = 2, y = 3, then he'll recieve «TAK» answer, because |2 - 2| ≤ |3 - 3|
- x = 3, y = 2, then he'll recieve «TAK» answer, because |3 - 3| ≤ |2 - 2|
According to the available information, it is possible to say that Nura ordered dishes with numbers 2 and 3.
题意:从1~n中选出k个数放入一个集合。给你60次机会,每一次你可以问两个数x,y,
如果x和y满足[ min(|x−a|,a∈S)≤min(|y−a|,a∈S) ] 系统会返回给你TAK,否则返回NIE。
最后让你输出这个集合中包含的两个不同的整数。
思路:
对区间[1,n]二分,每一次问mid,mid+1,如果返回是(tak),那么集合中一定存在一个数<=mid,(为什么可以自己想一下)
数据最大时1e5,那么log1e5=16,即16次就可以确定一个数,设这个数为a,那么再以从[a+1,n]和[1,a-1]这两个区间去二分找答案。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== "<<x<<" =="<<endl;
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,k;
bool query(int x,int y)
{
char s[];
if(y>n)
return ;
printf("1 %d %d\n",x,y );
fflush(stdout);
scanf("%s",s);
return s[]=='T';
}
int Find(int l,int r)
{
if(l>r)
return ;
int mid,ans=;
while(l<=r)
{
mid=(l+r)/;
if(query(mid,mid+))
{
ans=mid;
r=mid-;
}else
{
l=mid+;
}
}
return ans;
}
int main()
{
scanf("%d %d",&n,&k);
int a=Find(,n);
int b=Find(,a-);
if(!b)
{
b=Find(a+,n);
}
printf("2 %d %d\n",a,b );
fflush(stdout); return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Glad to see you! CodeForces - 810D (交互+二分)的更多相关文章
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- Codeforces.810D.Glad to see you!(交互 二分)
题目链接 \(Description\) 有一个大小为\(k\)的集合\(S\),元素两两不同且在\([1,n]\)内.你可以询问不超过\(60\)次,每次询问你给出\(x,y\),交互库会返回\(\ ...
- Codeforces.1129E.Legendary Tree(交互 二分)
题目链接 \(Description\) 有一棵\(n\)个点的树.你需要在\(11111\)次询问内确定出这棵树的形态.每次询问你给定两个非空且不相交的点集\(S,T\)和一个点\(u\),交互库会 ...
- Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)
题目链接 \(Description\) 有一个长为\(n\)的二进制串,保证\(01\)都存在.你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串 ...
- Codeforces Round #534 (Div. 2)D. Game with modulo-1104-D(交互+二分+构造)
D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces.714D.Searching Rectangles(交互 二分)
题目链接 \(Description\) 在一个\(n*n\)的二维平面中有两个不相交的整点矩形,每次可以询问两个矩形有几个完全在你给出的一个矩形中.200次询问内确定两个矩形坐标. \(Soluti ...
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- CodeForces 163B Lemmings 二分
Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...
随机推荐
- Android Studio无线连调式android手机
两种方法: 一.打开命令行或者Terminal窗口, 运行 adb connect 192.168.10.163:5555 来通过wifi连接手机调试 IP地址查看手机wifi的ip 要求手机和 ...
- c/c++ 用普利姆(prim)算法构造最小生成树
c/c++ 用普利姆(prim)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路.这时 ...
- 第十四届智能车培训 PLL锁相环
什么是锁相环? PLL(Phase Locked Loop): 为锁相回路或锁相环,用来统一整合时脉讯号,使高频器件正常工作,如内存的存取资料等.PLL用于振荡器中的反馈技术. 许多电子设备要正常工作 ...
- HashMap源码调试——认识"put"操作
前言:通常大家都知道HashMap的底层数据结构为数组加链表的形式,但其put操作具体是怎样执行的呢,本文通过调试HashMap的源码来阐述这一问题. 注:jdk版本:jdk1.7.0_51 1.pu ...
- SQL IN 操作符
IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...
- 如何合并列表中key相同的字典?
现有list: list1 = [{a: 123}, {a: 456},{b: 789}] 合并成: list2 = [{a: [123,456]},{b: [789]}] from collecti ...
- 7.02-bs4_btc
import requests from bs4 import BeautifulSoup from lxml import etree import json class BtcSpider(obj ...
- Git 遇到的坑
ssh出错 gitlab服务器添加完公钥之后,ssh服务器然后报了这个错误 sign_and_send_pubkey: signing failed: agent refused operation ...
- 【转】实践HTTP206状态:部分内容和范围请求
原文:http://www.cyberciti.biz/cloud-computing/http-status-code-206-commad-line-test/ HTTP 2xx范围内的状态码表明 ...
- Python:Day40 html
URL包括三个部分:协议.域名.路径 htyper text markup language (html) 即超文本标记语言 前端一共包括三个内容:html.css.js html做为基础,让CSS ...