codeforces 1039B Subway Pursuit【二分+随机】
题目:戳这里
题意:一个点在[1,n]以内,我们可以进行4500次查询,每次查询之后,该点会向左或向右移动0~k步,请在4500次查询以内找到该点。
解题思路:一边二分,一边随机。
交互题似乎有好多是二分,上次交互题的二分调了好久,这次也是。我出数据水平又很低,所以至今不知道哪些数据可以卡哪些二分的写法,只能改一次交一次。。。
1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <cmath>
8 #include <iostream>
9 #include <random>
10 #include <ctime>
11 using namespace std;
12 typedef long long ll;
13 const int maxn = 1e6 + 10;
14 char ans[11];
15 bool ask(ll l, ll r)
16 {
17 //printf("$$$$");
18 printf("%lld %lld\n", l, r);
19 fflush(stdout);
20 scanf("%s", ans);
21 if(ans[0] == 'Y')
22 {
23 if(l == r) exit(0);
24 else return 1;
25 }
26 else return 0;
27 }
28 int na[maxn];
29 int main()
30 {
31 ll n, k;
32 srand(time(NULL));
33 scanf("%lld %lld", &n, &k);
34 ll l = 1, r = n;
35 while(l <= r)
36 {
37 ll mid = l + (r - l) / 2;
38 if(ask(l, mid))
39 {
40 r = mid;//注意这点,我的理解是mid可能就是我们要找的点,如果用常规写法r=mid-1会把这个点跳过去
41 //但我试了几个样例,发现在区间不断变化且有随机数的情况下,及时r=mid-1也能找到答案。。
42 }
43 else
44 {
45 l = mid + 1;
46 }
47 l = max(1ll, l - k);
48 r = min(n, r + k);
49 mid = l + rand()%(r - l + 1);
50 ask(mid, mid);
51 l = max(1ll, l - k);
52 r = min(n, r + k);
53 }
54 return 0;
55 }
codeforces 1039B Subway Pursuit【二分+随机】的更多相关文章
- Subway Pursuit (二分)(交互题)
题目来源:codeforces1039B Subway Pursuit 题目大意: 在1到n里有一个运动的点,要求找到这个点,每次可以查询一个区间内有没有这个点,每次这个点往左或者往右移动1到k个位置 ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- Codeforces 1114E - Arithmetic Progression - [二分+随机数]
题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]
题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...
随机推荐
- SpringBoot 好“吃”的启动原理
原创:西狩 编写日期 / 修订日期:2020-12-30 / 2020-12-30 版权声明:本文为博主原创文章,遵循 CC BY-SA-4.0 版权协议,转载请附上原文出处链接和本声明. 不正经的前 ...
- linux设备文件
一.前言 在调用了alloc_chrdev_region函数或register_chrdev_region函数之后可以在/proc/devices中看到该设备的主设备号,比如我注册的hello模块的主 ...
- # Set the asyncio reactor's event loop as global # TODO: Should we instead pass the global one into the reactor?
daphne/server.py at master · django/daphne https://github.com/django/daphne/blob/master/daphne/serve ...
- kettle 连接oracle12c问题解决办法:
在oracle的安装文件目录......\NETWORK\ADMIN\sqlnet.ora 文件中添加该语句:SQLNET.ALLOWED_LOGON_VERSION_SERVER = 8window ...
- p2p nat 穿透原理
nat 打洞穿透原理,需要服务端. 假设有A.B两个客户端和S一个服务器 Step 1 : A.B发送UDP请求给S,S知道了A.B在公网的IP和端口. Step 2: A从S中取B在公网的IP和端口 ...
- mysql中int型的数字怎么转换成字符串
字段:number 是integer类型 在表test中 select cast(number as char) as number from test; 或者convert()方法.因为转换 ...
- cdq分治 笔记
算法讲解 这个算法用于解决三维偏序问题. 三维偏序:给定 \(n\) 个三元组: \((a_i,b_i,c_i)\),求同时满足满足 \(a_i\le a_j,b_i\le b_j,c_i\le c_ ...
- 五:SpringBoot-多个拦截器配置和使用场景
SpringBoot-多个拦截器配置和使用场景 1.拦截器简介 1.1 拦截器中应用 2.拦截器用法 2.1 编写两个拦截器 2.1.1 OneInterceptor 拦截器 2.1.2 TwoInt ...
- docker版mysql的使用和配置(1)——docker的基本操作
最近实在是忙成狗,其他的内容等稍微闲一点了一起更新. 这篇主要是讲docker版的mysql的使用和配置信息.因为实习公司需要搞一个docker做测试环境用,还需要包括基本的依赖.最重要的是,因为这个 ...
- Django (auth模块、User对象、用户认证、线上-用户认证)
一.auth模块 django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: authenticate() 提供了用户认证,即验证用户名以及密码是否正确,一般需要usern ...