# Codeforces 844 D. Interactive LowerBound （随机）

69人阅读 评论(0)

## Description

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 n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 10^9) — 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.

## Example input

5 3 80
97 -1
58 5
16 2
81 1
79 4


## Example output

? 1
? 2
? 3
? 4
? 5
! 81


## 思路

PS：记得每次输出结束以后都要 fflush(stdout);

## AC 代码

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
using namespace std;
typedef __int64 LL;
const int maxn = 2e5+10;

int ans = -1;
int main()
{
srand(time(0));
for(int i=0; i<1000; i++)
{
printf("? %d\n",(rand()%n*rand()%n)%n+1);
fflush(stdout);
int a,b;
scanf("%d%d",&a,&b);
if(a<x&&ans<a)
{
ans = a;
}
}
for(int i=0; i<999; i++)
{
break;
fflush(stdout);
int a,b;
scanf("%d%d",&a,&b);
if(a>=x)
{
printf("! %d\n",a);
return 0;
}
}
printf("! -1\n");
return 0;
}

个人资料
等级：
访问量： 36万+
积分： 1万+
排名： 2019
欢迎关注 <千与梦随> 哦！
博客专栏
 那些年,我们一起追过的算法 文章：6篇 阅读：23920
最新评论