1. 程式人生 > >Codeforces Aim Tech Round4 (Div2) D

Codeforces Aim Tech Round4 (Div2) D

push_back 0.00 queue pen algo 升序 map class air

題目鏈接:

題意:

  給你一個嚴格升序的單鏈表,但是是用數組來存放的。對於每一個位置來說,你可以知道這個位置的值和下一個的位置。你每一個可以詢問一個位置,機器會告訴你這個位置的值,和下一個位置的指針。要你確認大於等於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。

  

技術分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11
#include <map> 12 #include <stack> 13 #include <set> 14 using namespace std; 15 typedef long long LL; 16 typedef unsigned long long uLL; 17 #define ms(a, b) memset(a, b, sizeof(a)) 18 #define pb push_back 19 #define mp make_pair 20 #define eps 0.0000000001 21 #define IOS ios::sync_with_stdio(0);cin.tie(0); 22
#define random(a, b) rand()*rand()%(b-a+1)+a 23 const LL INF = 0x3f3f3f3f3f3f3f3f; 24 const int inf = 0x3f3f3f3f; 25 const int maxn = 50000+10; 26 const int mod = 1e9+7; 27 bool vis[maxn]; 28 int maxval, pos; 29 int main() { 30 #ifdef LOCAL 31 // freopen("input.txt", "r", stdin); 32 // freopen("output.txt", "w", stdout); 33 #endif 34 35 // IOS 36 srand(time(0)); 37 int n, start, x, val, next; 38 scanf("%d%d%d", &n, &start, &x); 39 if(n<=2000){ 40 int hh = start; 41 while(1){ 42 printf("? %d\n", hh);fflush(stdout); 43 scanf("%d%d", &val, &hh); 44 if(val>=x){ 45 printf("! %d\n", val); 46 return 0; 47 } 48 if(hh==-1){ 49 printf("! -1\n"); 50 return 0; 51 } 52 } 53 } 54 printf("? %d\n", start);fflush(stdout); 55 scanf("%d%d", &maxval, &next); 56 pos = start; 57 for(int i = 1;i<500;i++){ 58 int hh = random(1, n); 59 while(vis[hh]) hh = random(1, n); 60 vis[hh] = 1; 61 printf("? %d\n", hh);fflush(stdout); 62 scanf("%d%d", &val, &next); 63 if(val==x){ 64 printf("! %d\n", x); 65 return 0; 66 } 67 if(val<x){ 68 if(val>maxval){ 69 maxval = val; 70 pos = hh; 71 } 72 } 73 } 74 int hh = pos; 75 for(int i = 0;i<1500;i++){ 76 printf("? %d\n", hh);fflush(stdout); 77 scanf("%d%d", &val, &hh); 78 if(val>=x){ 79 printf("! %d\n", val); 80 return 0; 81 } 82 if(hh==-1){ 83 printf("! -1\n"); 84 return 0; 85 } 86 } 87 return 0; 88 }
View Code

Codeforces Aim Tech Round4 (Div2) D