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 VacantMale 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 012 are occupied by a man, occupied by a woman and vacant, respectively.

Input Output
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 << "" << endl;
cin>>s;
if (s[]=='V')
return ;
if (s[]=='M') M1=;
else M1=;
left=,right=n;
while (left<right)
{
mid=(right+left)/;
cout << mid << endl;
cin>>s;
if (s[]=='V')
return ;
if (s[]=='M') M2=;
else M2=;
int t=(mid-left);
if (t%==)
{
if (M1==M2) left=mid;
else right=mid;
}
else
{
if (M1==M2) right=mid;
else left=mid,M1=M2;
}
}
cout << flush;
return ;
}
 
 
 
 
 
 
 
 
 

Vacant Seat(Atcoder-C-交互式题目)的更多相关文章

  1. 【AtCoder】AtCoder Petrozavodsk Contest 001

    A - Two Integers 如果\(X\)是\(Y\)的倍数的话不存在 可以输出\(X \cdot (\frac{Y}{gcd(X,Y)} - 1)\) 代码 #include <bits ...

  2. AtCoder Petrozavodsk Contest 001

    第一场apc,5H的持久战,我当然水几个题就睡了 A - Two Integers Time limit : 2sec / Memory limit : 256MB Score : 100 point ...

  3. AtCoder 杂题训练

    前言: 因为要普及了,今年没一等就可以退役去学文化课了,所以暑假把历年noip普及组都刷了一遍,离noip还有50+天,想弄点强化训练什么的. 想了想,就这些天学文化课之余有空就把AtCoder之前那 ...

  4. 洛谷——P2071 座位安排 seat.cpp/c/pas

    P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...

  5. [Atcoder Regular Contest 060] Tutorial

    Link: ARC060 传送门 C: 由于难以维护和更新平均数的值: $Average->Sum/Num$ 这样我们只要用$dp[i][j][sum]$维护前$i$个数中取$j$个,且和为$s ...

  6. 洛谷 P2071 座位安排 seat.cpp/c/pas

    P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...

  7. ZJOI2017 Day1

    私のZJOI Day1 2017-3-21 07:52:53 有人在暴力膜 苟-- 富贵 无相忘 ZJOI2017交流群 133135071 如果你足够厉害 如果你足够厉害 如果你足够厉害 其实完全可 ...

  8. English_word_learning

    这次报名参加了学院的21天打卡活动,说实话,也是想给自己一个积累的平台. 毕竟,真的有时候感觉挺弱的 有的人用了一年考完了四六级,而有人却用四年还未考完. 听到有一位学长因为自己的四级成绩没有达到48 ...

  9. Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem

    D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...

随机推荐

  1. python导入自定义的库

    一.导入项目文件夹下的模块 1.导入整个模块 import 模块名 2.导入模块的某个函数 from 模块名 import 函数名 示例 untitled是项目文件夹,文件结构如下 ①在a.py导入c ...

  2. Java虚拟机(JVM) - 学习总结(全)

    深入理解java虚拟机---学习总结: 1.Java内存区域 1.1 java运行时数据区 Java 虚拟机所管理的内存如下图所示,基于JDK1.6. 基于jdk1.8画的JVM的内存模型 (1) 程 ...

  3. Patroni 修改配置

    Patroni 修改配置 背景 使用 Patroni 部署 postgresql 集群的时候,不能单独修改单点的配置,这里需要通过 Patroni 来修改配置. 修改步骤 1. 修改 postgres ...

  4. 拾起HTML+CSS的一天

    今天在w3school看了下HTML5和CSS3的新特性. 本来觉得自己对CSS方面基础还挺有把握的,就之前自学都是跳过这个模块的,但经过昨天会友的面试,感觉自己好像太忽视基础了,很多基本的东西很模糊 ...

  5. 缩写: i = i + 1 和 i += 1,可以看做是 i 自加的值 是1。

    i +=  1; 这样有助于记忆: i自加的值等于1

  6. QT程序中显示中文字体解决办法

    Qt4.7.1 默认没有中文字体库,迅为给用户提供“文泉驿”字体和配置方法.本节需要的 文件在网盘: 用一个简单测试程序说明“文泉驿”字体的配置方法. 在 Qt Creater 新建工程“nihao” ...

  7. Mysql计算时间最近多久

    -- DATE_SUB(CURDATE(), INTERVAL 3 MONTH)计算结果为当前时间的前推三个月,time字段可为时间型字符串 select * form t_user where ti ...

  8. SaaS|PaaS|iaas|

    生物医疗大数据:云物移大智 云计算的三种模式:SaaS|PaaS|iaas 互联网:计算机之间的网络 物联网:物品之间的网络 移动:5G的三个特点:快:密:稳 大数据:4v:volume数据量大:ve ...

  9. Office VBA开发经典-中级进阶卷 配套资源下载

    本书源代码请到如下页面寻找: https://www.cnblogs.com/ryueifu-VBA/p/8982192.html

  10. [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

    前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...