Codeforces Aim Tech Round4 (Div2) D
题目链接:
题意:
给你一个严格升序的单链表,但是是用数组来存放的。对于每一个位置来说,你可以知道这个位置的值和下一个的位置。你每一个可以询问一个位置,机器会告诉你这个位置的值,和下一个位置的指针。要你确认大于等于x的值。(询问次数不能超过2000).
题解:
由于给你的可能有5e4个数,除了随机算法,没有一种可以在2000步以内找到小于等于x。
对与随机算法:我们先随机找500个点,找到小于x的点0值中最大的一个。然后暴力询问。小于2000的直接暴力找了。
证明:
我们假设x的位置在Y, 那么在[Y-1500, Y] 将会有一个点被随机到。如果没有一个被随机到,这个概率是:1-1500/5e4 = 0.97。有97%的机率选不到。但是500次随机之后 (0.97)500 = 2.4e- 7。
也就是1e7次的期望值才2。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+;
bool vis[maxn];
int maxval, pos;
int main() {
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // IOS
srand(time());
int n, start, x, val, next;
scanf("%d%d%d", &n, &start, &x);
if(n<=){
int hh = start;
while(){
printf("? %d\n", hh);fflush(stdout);
scanf("%d%d", &val, &hh);
if(val>=x){
printf("! %d\n", val);
return ;
}
if(hh==-){
printf("! -1\n");
return ;
}
}
}
printf("? %d\n", start);fflush(stdout);
scanf("%d%d", &maxval, &next);
pos = start;
for(int i = ;i<;i++){
int hh = random(, n);
while(vis[hh]) hh = random(, n);
vis[hh] = ;
printf("? %d\n", hh);fflush(stdout);
scanf("%d%d", &val, &next);
if(val==x){
printf("! %d\n", x);
return ;
}
if(val<x){
if(val>maxval){
maxval = val;
pos = hh;
}
}
}
int hh = pos;
for(int i = ;i<;i++){
printf("? %d\n", hh);fflush(stdout);
scanf("%d%d", &val, &hh);
if(val>=x){
printf("! %d\n", val);
return ;
}
if(hh==-){
printf("! -1\n");
return ;
}
}
return ;
}
Codeforces Aim Tech Round4 (Div2) D的更多相关文章
- Codeforces AIM Tech Round (Div. 2)
这是我第一次完整地参加codeforces的比赛! 成绩 news standings中第50. 我觉这个成绩不太好.我前半小时就过了前三题,但后面的两题不难,却乱搞了1.5h都没有什么结果,然后在等 ...
- Codeforces AIM Tech Round3
打得最烂一场Codeforces,多次都错题,无限WA... A题: 题意:给定n个橘子的大小,大小超过b的丢掉,不足d的补充进来,同时超过d的部分去掉,问要去掉几次 分析:直接模拟即可 #inclu ...
- 【Codeforces AIM Tech Round 4 (Div. 2) C】
·将排序限制于子序列中,又可以说明什么呢? C. Sorting by Subsequences ·英文题,述大意: 输入一个长度为n的无重复元素的序列{a1,a2……an}(1<= ...
- AIM Tech R3 div2 E Centroid
思路很明显了,假设是点x,则看它的子树中是否有大于n/2的,如果有,则在该子树中剪去它可以剪的且小于n/2的,接到点x上. 则统计出在以x点为根的子树中,它的各子树可以剪去的且小于n/2的最大子子树. ...
- codeforces AIM Tech Round 4 div 2
A:开个桶统计一下,但是不要忘记k和0比较大小 #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int k; ...
- Codeforces AIM Tech Round 5 (rated, Div. 1 + Div. 2)
A. Find Square time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...
- AIM Tech Round 5 1028cf(A-E)
AIM Tech Round 5 (codeforces上题目编号是1028)(A-E) ---完全被这次比赛打击,自己真的很渣--- 战况 依旧3题选手 被构造题坑得好惨 稍稍涨了rating,希望 ...
- codeforce AIM tech Round 4 div 2 B rectangles
2017-08-25 15:32:14 writer:pprp 题目: B. Rectangles time limit per test 1 second memory limit per test ...
- AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)
rating又掉下去了.好不容易蓝了.... A..没读懂题,wa了好几次,明天问队友补上... B. Checkpoints 题意:一条直线上n个点x1,x2...xn,现在在位置a,求要经过任意n ...
随机推荐
- day 02 while 循环 格式化输出 运算符 and or not - 编码的初识
while 循环 while 条件: 循环体 循环如何终止? 改变条件. flag = Truewhile flag: print('狼的诱惑') print('我们不一样') ...
- 如何利用`keep-alive`按需缓存页面数据
随着项目不断变大,页面变多,搜索条件也随之也越来越多,而每次跳转页面再返回时,之前的筛选的条件都会别清空.之前在elment-ui table组件 -- 远程筛选排序提到过缓存,但是有所取巧,这次重新 ...
- 安装linux mint后要做20件事
Linux Mint 17 Qiana Cinnamon Linux Mint 17已经发布,定名为Qiana.Mint是Linux最佳发行版之一,它定位于桌面用户,关注可用性和简洁.它携带了风格迥异 ...
- java中的集合类详情解析以及集合和数组的区别
数组和链表 数组:所谓数组就是相同数据类型的元素按照一定顺序排列的集合. 它的存储区间是连续的,占用内存严重,所以空间复杂度很大,为o(n),但是数组的二分查找时间复杂度很小为o(1). 特点是大小固 ...
- 在eclipse中创建第一个java应用程序,并在控制台输出“hello world”。
package com.fs.test; public class HelloWorld { public void aMethod() { } public static void main(Str ...
- 用ajax写机器人聊天的案例
HTML 中的文档 <body> <h3>简单的Ajax实例</h3> <div class="chatbox"> <!-- ...
- lua基础(2)
错误处理: local function add(a,b) assert(type(a) == "number", "a 不是一个数字") assert(typ ...
- linux把用户添加到组
使用 usermod 命令 将现有的用户添加到多个次要组或附加组 # usermod -a -G GroupName UserName id 命令查看输出 # id UserName 用户添加到多个次 ...
- Python深入:编码问题总结
转自:http://blog.csdn.net/gqtcgq/article/details/47068817 一:字符编码简介 1:ASCII 最初的计算机的使用 ...
- C++线程同步 -- windows
简介: 在一般情况下,创建一个线程是不能提高程序的执行效率的,所以要创建多个线程.但是多个线程同时运行的时候可能调用线程函数,在多个线程同时对同一个内存地址进行写入, 由于CPU时间调度上的问题,写入 ...