Codeforces 844D Interactive LowerBound - 随机化
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
- ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
- ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
- fflush(stdout) in C++;
- System.out.flush() in Java;
- stdout.flush() in Python;
- flush(output) in Pascal;
- For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
5 3 80
97 -1
58 5
16 2
81 1
79 4
? 1
? 2
? 3
? 4
? 5
! 81
You can read more about singly linked list by the following link: https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list
The illustration for the first sample case. Start and finish elements are marked dark.
题目大意 给定一个元素按升序排序的单向链表,给定开头的下标,查询大于等于x的最小值。每次可以询问一个位置的值和它的下一个元素的下标。询问不得超过1999次。
这是一道有趣的题目。
1)如果n不超过1999暴力for整个链表。
2)否则随机1000个位置查值,然后找到第一个小于x的位置开始往后for。直到链表结束或者找到一个大于等于x的值。
另外直接srand((unsigned) time (0))会被卡掉,所以但是随机函数就过了。
Code
/**
* Codeforces
* Problem#844D
* Accepted
* Time: 30ms
* Memory: 500k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
typedef pair<int, int> pii; typedef class Random {
public:
unsigned int pre;
unsigned int seed; Random():pre(), seed((unsigned) time (NULL)) { }
Random(int seed):pre(), seed(seed) { } /**
* Generate a random number.
* @return this function will return the random number it gernerated
*/
unsigned int rand() {
// unsigned int ret = (seed * 7361238 + seed % 20037 * 1244 + pre * 12342 + 378211) * (seed + 134543);
// unsigned int ret = (seed * 7361238 + seed % 20037 * 1244 + pre * 12342 + 378211 + time(NULL) * pre) * (seed + 134543);
unsigned int ret;
if(ret & )
ret = (seed * + seed % * + pre * + (time(NULL) * (pre * + seed * + )) + );
else
ret = (seed * + seed % * + pre * + (time(NULL) * (pre * + seed * + )) + );
pre = seed;
seed = ret;
return ret;
} /**
* Generate a random number that between a variable low and a variable high.
* @param low the variable low
* @param high the variable high
* @return if low is not more than high, it will return the random number or it will return 0
*/
unsigned int rand(int low, int high){
if(low > high) return ;
int len = high - low + ;
return rand() % len + low;
}
}Random; Random r;
int n, x, start;
pii *val;
pii ask(int p) {
if(val[p].first != -) return val[p];
pii rt;
printf("? %d\n", p);
fflush(stdout);
scanf("%d%d", &rt.first, &rt.second);
return rt;
} inline void init() {
scanf("%d%d%d", &n, &start, &x);
val = new pii[(n + )];
for(int i = ; i <= n; i++)
val[i].first = -;
} inline void solve1() {
pii data;
for(int i = , p = start; i < n; i++) {
data = ask(p);
if(data.first >= x) {
printf("! %d\n", data.first);
fflush(stdout);
return;
}
p = data.second;
}
puts("! -1");
fflush(stdout);
} const int asktime = ;
boolean *vis;
int pos[asktime + ];
inline void solve2() {
vis = new boolean[(n + )];
memset(vis, false, sizeof(boolean) * (n + ));
vis[start] = true;
for(int i = ; i <= asktime; i++) {
while(vis[(pos[i] = r.rand(, n))]);
vis[pos[i]] = true;
} int s = start;
pii dat = ask(start);
for(int i = ; i <= asktime; i++) {
pii cmp = ask(pos[i]);
if(cmp.first < x && cmp.first > dat.first)
s = pos[i], dat = cmp;
} while(s != -) {
if(dat.first >= x) {
printf("! %d\n", dat.first);
fflush(stdout);
return;
}
s = dat.second;
dat = ask(s);
}
puts("! -1");
fflush(stdout);
} int main() {
init();
if(n <= )
solve1();
else
solve2();
return ;
}
Codeforces 844D Interactive LowerBound - 随机化的更多相关文章
- cf 843 B Interactive LowerBound [随机化]
题面: 传送门 思路: 这是一道交互题 比赛的时候我看到了直接跳过了...... 后来后面的题目卡住了就回来看这道题,发现其实比较水 实际上,从整个序列里面随机选1000个数出来询问,然后从里面找出比 ...
- 【AIM Tech Round 4 (Div. 1) B】Interactive LowerBound
[链接]http://codeforces.com/contest/843/problem/B [题意] 给你一个数组模拟的单链表,放在一个长度为n的数组里面,然后告诉你表头的位置在哪里; 你可以最多 ...
- Codeforces 306D - Polygon(随机化+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 中考终于结束了--简单写道题恢复下状态罢. 首先这一类题目肯定没法用一般的方法解决,因此考虑用一些奇淫的乱搞做法解决这道题,不难发现,如果 ...
- CodeForces 753C Interactive Bulls and Cows (Hard)
题意:... 析:随机判断就即可,每次把不正确的删除,经过几次后就基本剩不下了. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...
- AIM Tech Round 4 (Div. 2)ABCD
A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 【AIM Tech Round 4 (Div. 2) D Prob】
·题目:D. Interactive LowerBound ·英文题,述大意: 有一个长度为n(n<=50000)的单链表,里面的元素是递增的.链表存储在一个数组里面,给出长度n.表 ...
- Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化
C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #358 (Div. 2) E. Alyona and Triangles 随机化
E. Alyona and Triangles 题目连接: http://codeforces.com/contest/682/problem/E Description You are given ...
随机推荐
- vue中连续点击3次且时间间隔不超过3秒,才显示div(刚开始隐藏的)
num:0,//点击次数timer0:'',//第一次点击的时间timer4:'',//第四次点击的时间centerDialogVisible: false // 连续4次点击显示模态框 change ...
- cocos2d-x C++ iOS工程集成第三方支付宝支付功能
一.在支付宝开放平台下载支付宝SDK(https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.WWgVz8&tr ...
- cocos2dx - Lua 语言
快捷注释: - -[[ print(10) - ->10 - - 不起作用(因为这是注释) - -]] 当重新启用这段代码时,只需在一次行行首添加一个连接字符即可: - - -[[ print ...
- 读书笔记_Effective C++_条款一:将C++视为一个语言联邦
C++起源于C,最初的名称为C with Classes,意为带类的C语言,然而,随着C++的不断发展和壮大,在很多功能上已经远远超越了C,甚至一些C++程序员反过来看C代码会觉得不习惯. C++可以 ...
- 48.HTML---Flex 布局教程:实例篇
你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇>.我的主要参考资料是Landon Schropp的文章和Solve ...
- sqli-labs(十一)(二次注入)
第二十四关: 这关考验的是sql的二次注入. 这关是一个登陆加注册的功能点,登陆的地方没有注入,账号密码都输入输入'",页面只会显示登陆失败. 但注册账号的地方没有做过滤可以注册带有单引符号 ...
- nodejs+react使用webpack打包时控制台报错
一.错误:Uncaught ReferenceError: process is not defined 解决方法: new webpack.DefinePlugin({ 'process.env': ...
- Mysql由浅入深
1. Mysql的安装方式 1. yum安装mysql 适合对数据库要求不太高的场合,例如:并发不大,公司内部,企业内部. 1. 官网下载yum源,wget https://dev.mysql.c ...
- 【转】通过Excel生成批量SQL语句,处理大量数据
经常会遇到这样的要求:用户给发过来一些数据,要我们直接给存放到数据库里面,有的是Insert,有的是Update等等,少量的数据我们可以采取最原始的办法,也就是在SQL里面用Insert into来实 ...
- codeforces 979A Pizza, Pizza, Pizza!!!
题意: 对一个圆形的pizza,只能用直线来切它,求把它切为n+1份的形状和size都相同的最下次数. 思路: 形状和size都相同,那么只能是扇形,分奇偶讨论. n为0还得特判,切0刀,因为这个还被 ...