1. 程式人生 > >Vacant Seat(Atcoder-C-交互式題目)

Vacant Seat(Atcoder-C-交互式題目)

tdi order ict selected ice format minus min long

C - Vacant Seat


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

This is an interactive task.

Let N be an odd number at least 3.

There are N seats arranged in a circle. The seats are numbered 0 through N−1. For each i (0≤iN−2), Seat i and Seat i

+1 are adjacent. Also, Seat N−1 and Seat 0 are adjacent.

Each seat is either vacant, or oppupied by a man or a woman. However, no two adjacent seats are occupied by two people of the same sex. It can be shown that there is at least one empty seat where N is an odd number at least 3.

You are given N

, but the states of the seats are not given. Your objective is to correctly guess the ID number of any one of the empty seats. To do so, you can repeatedly send the following query:

  • Choose an integer i (0≤iN−1). If Seat i is empty, the problem is solved. Otherwise, you are notified of the sex of the person in Seat i
    .

Guess the ID number of an empty seat by sending at most 20 queries.

Constraints

  • N is an odd number.
  • 3≤N≤99 999

Input and Output

First, N is given from Standard Input in the following format:

N

Then, you should send queries. A query should be printed to Standart Output in the following format. Print a newline at the end.

i

The response to the query is given from Standard Input in the following format:

s

Here, s is Vacant, Male or Female. Each of these means that Seat i is empty, occupied by a man and occupied by a woman, respectively.

Notice

  • Flush Standard Output each time you print something. Failure to do so may result in TLE.
  • Immediately terminate the program when s is Vacant. Otherwise, the verdict is indeterminate.
  • The verdict is indeterminate if more than 20 queries or ill-formatted queries are sent.

Sample Input / Output 1

In this sample, N=3, and Seat 0, 1, 2 are occupied by a man, occupied by a woman and vacant, respectively.

InputOutput
3
0
Male
1
Female
2
Vacant


題意:給你大於等於3的奇數個座位,編號0到N-1,座位情況為空,男,女,其中同性不相鄰,至少存在一個空座位,然後你詢問一個座位編號,它告訴你現在那個地方是不是空,時空就結束,如果不是就告訴你這個地方的人的性別。要在20次查詢以內得出結果!所以必然需要二分! 分析:這是第一次看到交互式題目,真的是看的我很懵逼!覺得很有必要寫個博客!交互式,和我們原來做題目的模式是剛好相反的!像題目中給出的案例:輸入N以後,輸出0,然後我們輸入Male,代表0號位置上面坐的男性,下面同樣!但是以往我們做的非交互題目都是輸入座位編號,然後電腦輸出座位的信息! (註意:這一所謂的性別相同的不能相鄰是說中間假設存在空座位的兩個性別相同的人之間是相鄰的!或者直接相鄰! 舉個例子:5和7 N=5時,假設0位置為男性,mid=2,如果2號位置與0號位置同性別(假設為男),則一號位置必為女,所以去另外一邊查找,left=mid;在left和mid位置同性別的情況下,只要mid-left為偶數就意味著中間不存在空座位;如果left與mid位置性別不同,即本例中的0號和2號位置的性別不同,那麽他們中間必然為空; N=7時,同上假設,mid=3,如果0號和3號位置同性別,就意味著中間兩個必然有一個為空,所以right=mid,如果性別不同就意味著中間必然是兩種性別交替出現,必然不存在空座位,所以此時left=mid,要註意此時的性別也要換成mid位置的性別。可以畫圖認真體會! AC代碼:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<vector>
#include<queue>
#define N 15
using namespace std;
typedef long long ll;
char s[N];
int main()
{
    ios::sync_with_stdio(false);
    int n,left,right,mid;
    int M1,M2;//用於標記性別
    cin>>n;
    cout << "0" << endl;
    cin>>s;
    if (s[0]==V)
       return 0;
    if (s[0]==M)  M1=1;
    else  M1=0;
    left=0,right=n;
    while (left<right)
    {
        mid=(right+left)/2;
        cout << mid << endl;
        cin>>s;
        if (s[0]==V)
          return 0;
        if (s[0]==M)  M2=1;
        else  M2=0;
        int t=(mid-left);
        if (t%2==0)
        {
            if (M1==M2) left=mid;
            else right=mid;
        }
        else
        {
            if (M1==M2) right=mid;
            else left=mid,M1=M2;
        }
    }
    cout << flush;
    return 0;
}

Vacant Seat(Atcoder-C-交互式題目)