1. 程式人生 > >Dwarves, Hats and Extrasensory Abilities —— 互動題

Dwarves, Hats and Extrasensory Abilities —— 互動題

This is an interactive problem.

In good old times dwarves tried to develop extrasensory abilities:

Exactly n dwarves entered completely dark cave.
Each dwarf received a hat — white or black. While in cave, none of the dwarves was able to see either his own hat or hats of other Dwarves.
Dwarves went out of the cave to the meadow and sat at an arbitrary place one after the other. When a dwarf leaves the cave, he sees the colors of all hats of all dwarves that are seating on the meadow (i.e. left the cave before him). However, he is not able to see the color of his own hat and none of the dwarves can give him this information.
The task for dwarves was to got diverged into two parts — one with dwarves with white hats and one with black hats.
After many centuries, dwarves finally managed to select the right place on the meadow without error. Will you be able to repeat their success?

You are asked to successively name n different integer points on the plane. After naming each new point you will be given its color — black or white. Your task is to ensure that the named points can be split by a line in such a way that all points of one color lie on the same side from the line and points of different colors lie on different sides. Moreover, no points can belong to the line. Also, you need to report any such line at the end of the process.

In this problem, the interactor is adaptive — the colors of the points in the tests are not fixed beforehand and the jury program can select them arbitrarily, in particular, depending on your program output.

Interaction
The first line of the standard input stream contains an integer n (1 ≤ n ≤ 30) — the number of points your program should name.

Then n times your program must print two integer coordinates x and y (0 ≤ x ≤ 109, 0 ≤ y ≤ 109). All points you print must be distinct.

In response to each coordinate pair your program will receive the string “black”, if the point is black, or “white”, if the point is white.

When all n points are processed, you need to print four integers x1, y1, x2 and y2 (0 ≤ x1, y1 ≤ 109, 0 ≤ x2, y2 ≤ 109) — coordinates of points (x1, y1) and (x2, y2), which form a line, which separates n points into black and white. Points (x1, y1) and (x2, y2) should not coincide.

Hacks

To hack solution use the following format. The first line must contain word “hack”, the second line should contain the number n and the last line should contain the sequence of 0 and 1 — colors of points, which will be reported to the solution. Unlike the jury tests, colors of points in hacks are always fixed in advance. Of course, the hacked solution wouldn’t be able to get the information about the colors in advance.

For example, the hack corresponding to sample test will look like this:

hack
5
0 0 1 1 0
Example
inputCopy
5

black

black

white

white

black
outputCopy

0 0

3 1

2 3

4 4

0 2

1 3 4 1
Note
In the sample input and output values are aligned only for simplicity of interpreting them chronologically. In real interaction no “extra” line breaks should appear.

題意:

給你一個n,讓你輸入n個座標,每輸入一個的時候會給你這個座標的顏色,讓你最後找出一條直線把所有的點分成兩部分,相同顏色的在一邊。

題解:

看到這個30的資料範圍就知道要搞事情了,我們可以設一個l,r,l=0,r=1<<29,因為第一個點在0,所以只剩下29個點了,不會超1e9.第二個點的位置就是l+r>>1,顏色如果和第一個點一樣,那麼就繼續往右,否則往左,想想就知道這是為什麼了,最後的線就可以穿過中間的隨便一條線。有一個情況就是l==0的時候,我們線就要設成1 1 和2 3,這樣就沒問題咯。

#include<bits/stdc++.h>
using namespace std;
int n,line;
int main()
{
    scanf("%d",&n);
    n-=1;
    int l=0,r=1<<29;
    line=1;
    printf("0 0\n");
    string s;
    int flag;
    fflush(stdout);
    cin>>s;
    flag=s[0];
    int turn;
    while(n--)
    {
        int mid=l+r>>1;
        printf("%d 0\n",mid);
        fflush(stdout);
        cin>>s;
        if(s[0]==flag)
            l=mid,turn=1;
        else
            r=mid,turn=0;
    }
    if(l==0)
    {
        printf("1 1 2 3\n");
    }
    else
    {
        if(turn)
            printf("%d 1 %d 3",l,l-1);
        else
            printf("%d 1 %d 3",r,r+1);

    }
    return 0;
}