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.

Input

The first line contains three integers nstartx (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.

Output

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.

Interaction

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 ≤ nnexti ≠ 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 nstartx (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 ≤ nnexti ≠ 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.

Example
input
5 3 80
97 -1
58 5
16 2
81 1
79 4
output
? 1
? 2
? 3
? 4
? 5
! 81
Note

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 ;
}

最新文章

  1. JS组件系列——使用HTML标签的data属性初始化JS组件
  2. (转)解决Mac OS X上PhpStorm不能输入中文
  3. C#遍历XML文件动态加载菜单
  4. JeeSite环境搭建及运行和打包(master20161117)
  5. APP落地页开发中的一些小经验~
  6. c# FastReport开发报表
  7. PHP程序员的技术成长规划(送给迷茫的你)
  8. swift 录制多个音频 并将音频转换为mp3 并合成多个mp3文件为一个文件
  9. webView调用系统地图,电话,和跳转链接的方法
  10. Asp.NetCore+Microsoft.AspNetCore.SignalR前后端分离
  11. eclipse运行无错的ssm项目,迁移到idea出错
  12. DSO windowed optimization 代码 (3)
  13. Kali linux 2018安装后全屏乱码解决
  14. Software-Defined Networking A Comprehensive Survey --阅读_day1
  15. DeepID1,DeepID2
  16. UINavigationController出现nested push animation can result in corrupted navigation bar的错误提示
  17. android------2018 年初值得关注的 16 个新 Android 库和项目
  18. 语法糖----C#的async和await
  19. 数据库表,id自动递增重置,从1开始
  20. (一)自定义ViewGroup绘制出菜单

热门文章

  1. Linux 重启nginx
  2. 让bat以管理员权限运行
  3. sourceInsight工具移除不掉项目 source Insight Add and Remove Project Files
  4. LeetCode83.删除排序链表中的重复的元素
  5. input 滑块功能range javascript方法使用
  6. beego 初体验 - 环境搭建
  7. 检索系统向量化计算query-doc相似度
  8. C++的类型转换
  9. Visible Lattice Points (莫比乌斯反演)
  10. hdu1762 树的上的查询