#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair < int, int > PII;
int N;
int Candy_Sum = 0;
unordered_map < int, bool > Sel;
unordered_map < int, pair < int, int > > mp;
///
inline PII Get_Ans (int pos)
{
if(Sel[pos])
return mp[pos];
vector < int > res = ask(pos);
Sel[pos] = 1, mp[pos] = {res[0], res[1]};
return {res[0], res[1]};
}
inline bool IsDiamond (PII X)
{
return ((X.first + X.second) == 0);
}
inline bool IsCandy (PII X)
{
return ((X.first + X.second) == Candy_Sum);
}
///
inline int Go (int Left, int Right)
{
if(Left > Right)
return 0;
if(Left == Right)
return Left;
int Mid = ((Left + Right) >> 1);
PII Now = Get_Ans(Mid);
if(IsDiamond(Now))
return Mid;
if(Now.first)
return Go(Left, Mid - 1);
return Go(Mid + 1, Right);
}
inline int F (int Left, int Right, int less_to_left, int less_to_right)
{
if(Left > Right)
return 0;
if(Left == Right)
{
if(IsDiamond(Get_Ans(Left)))
return Left;
return 0;
}
int Mid = ((Left + Right) >> 1);
PII Now = Get_Ans(Mid);
if(IsDiamond(Now))
return Mid;
int i = Mid - 1, j = Mid + 1, pos = 0;
bool found = 0;
if(IsCandy(Now))
found = 1, pos = Mid;
///
while(!found && (i >= Left || j <= Right))
{
if(i >= Left)
{
PII curr = Get_Ans(i);
if(IsDiamond(curr))
return i;
if(!IsCandy(curr))
--i;
else
{
found = 1, pos = i, Now = curr;
break;
}
}
if(j <= Right)
{
PII curr = Get_Ans(j);
if(IsDiamond(curr))
return j;
if(!IsCandy(curr))
++j;
else
{
found = 1, pos = j, Now = curr;
break;
}
}
}
///
if(!found)
return 0;
Mid = pos;
int ans = 0;
int ext_left = Now.first - less_to_left;
int ext_right = Now.second - less_to_right;
if(!ext_left && !ext_right)
return 0;
if(ext_left && ext_right)
{
if(ext_left > ext_right)
{
ans = F(Left, Mid - 1, less_to_left, Now.second);
if(!ans)
ans = F(Mid + 1, Right, Now.first, less_to_right);
}
else
{
ans = F(Mid + 1, Right, Now.first, less_to_right);
if(!ans)
ans = F(Left, Mid - 1, less_to_left, Now.second);
}
}
else
{
if(ext_left)
ans = F(Left, Mid - 1, less_to_left, Now.second);
else
ans = F(Mid + 1, Right, Now.first, less_to_right);
}
return ans;
}
int find_best(int n)
{
N = n;
for(int i = 0; i < min(N, 476); ++i)
{
PII Now = Get_Ans(i);
if(IsDiamond(Now))
return i;
Candy_Sum = max(Candy_Sum, Now.first + Now.second);
}
if(Candy_Sum == 1)
return Go(0, N - 1);
return F(0, N - 1, 0, 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
400 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
416 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
396 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
416 KB |
Output is correct |
12 |
Correct |
6 ms |
428 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
11 ms |
384 KB |
Output is correct |
15 |
Correct |
19 ms |
504 KB |
Output is correct |
16 |
Correct |
22 ms |
504 KB |
Output is correct |
17 |
Correct |
4 ms |
376 KB |
Output is correct |
18 |
Correct |
22 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
19 ms |
508 KB |
Output is correct |
21 |
Correct |
8 ms |
500 KB |
Output is correct |
22 |
Correct |
11 ms |
492 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
8 ms |
384 KB |
Output is correct |
25 |
Correct |
22 ms |
504 KB |
Output is correct |
26 |
Correct |
20 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
17 ms |
376 KB |
Output is correct |
29 |
Correct |
17 ms |
504 KB |
Output is correct |
30 |
Correct |
17 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
5 ms |
428 KB |
Output is correct |
35 |
Correct |
7 ms |
384 KB |
Output is correct |
36 |
Correct |
20 ms |
632 KB |
Output is correct |
37 |
Correct |
5 ms |
428 KB |
Output is correct |
38 |
Correct |
3 ms |
408 KB |
Output is correct |
39 |
Correct |
39 ms |
632 KB |
Output is correct |
40 |
Correct |
12 ms |
376 KB |
Output is correct |
41 |
Correct |
26 ms |
504 KB |
Output is correct |
42 |
Correct |
23 ms |
504 KB |
Output is correct |
43 |
Correct |
9 ms |
416 KB |
Output is correct |
44 |
Correct |
12 ms |
464 KB |
Output is correct |
45 |
Correct |
11 ms |
556 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
17 ms |
500 KB |
Output is correct |
48 |
Correct |
8 ms |
504 KB |
Output is correct |
49 |
Correct |
13 ms |
568 KB |
Output is correct |
50 |
Correct |
26 ms |
504 KB |
Output is correct |
51 |
Correct |
14 ms |
376 KB |
Output is correct |
52 |
Partially correct |
29 ms |
944 KB |
Partially correct - number of queries: 5212 |
53 |
Correct |
7 ms |
376 KB |
Output is correct |
54 |
Correct |
8 ms |
384 KB |
Output is correct |
55 |
Correct |
6 ms |
384 KB |
Output is correct |
56 |
Correct |
20 ms |
504 KB |
Output is correct |
57 |
Correct |
31 ms |
564 KB |
Output is correct |
58 |
Correct |
37 ms |
504 KB |
Output is correct |
59 |
Correct |
20 ms |
504 KB |
Output is correct |
60 |
Correct |
28 ms |
504 KB |
Output is correct |
61 |
Correct |
5 ms |
384 KB |
Output is correct |
62 |
Correct |
8 ms |
384 KB |
Output is correct |
63 |
Correct |
5 ms |
384 KB |
Output is correct |
64 |
Correct |
7 ms |
376 KB |
Output is correct |
65 |
Correct |
4 ms |
376 KB |
Output is correct |
66 |
Correct |
12 ms |
384 KB |
Output is correct |
67 |
Correct |
6 ms |
384 KB |
Output is correct |
68 |
Correct |
4 ms |
384 KB |
Output is correct |
69 |
Correct |
9 ms |
376 KB |
Output is correct |
70 |
Correct |
9 ms |
384 KB |
Output is correct |
71 |
Partially correct |
52 ms |
888 KB |
Partially correct - number of queries: 5234 |
72 |
Correct |
8 ms |
384 KB |
Output is correct |
73 |
Partially correct |
55 ms |
760 KB |
Partially correct - number of queries: 5171 |
74 |
Partially correct |
44 ms |
860 KB |
Partially correct - number of queries: 5198 |
75 |
Correct |
5 ms |
532 KB |
Output is correct |
76 |
Correct |
58 ms |
764 KB |
Output is correct |
77 |
Partially correct |
48 ms |
760 KB |
Partially correct - number of queries: 5342 |
78 |
Correct |
10 ms |
384 KB |
Output is correct |
79 |
Correct |
29 ms |
512 KB |
Output is correct |
80 |
Partially correct |
30 ms |
768 KB |
Partially correct - number of queries: 5338 |
81 |
Partially correct |
49 ms |
888 KB |
Partially correct - number of queries: 5337 |
82 |
Partially correct |
36 ms |
1016 KB |
Partially correct - number of queries: 5297 |
83 |
Correct |
9 ms |
512 KB |
Output is correct |
84 |
Correct |
53 ms |
772 KB |
Output is correct |
85 |
Partially correct |
69 ms |
868 KB |
Partially correct - number of queries: 5320 |
86 |
Correct |
7 ms |
384 KB |
Output is correct |
87 |
Correct |
3 ms |
368 KB |
Output is correct |
88 |
Correct |
3 ms |
392 KB |
Output is correct |
89 |
Correct |
5 ms |
384 KB |
Output is correct |
90 |
Correct |
5 ms |
384 KB |
Output is correct |
91 |
Correct |
5 ms |
384 KB |
Output is correct |
92 |
Correct |
3 ms |
384 KB |
Output is correct |
93 |
Correct |
8 ms |
384 KB |
Output is correct |
94 |
Correct |
9 ms |
384 KB |
Output is correct |
95 |
Correct |
10 ms |
384 KB |
Output is correct |
96 |
Correct |
8 ms |
384 KB |
Output is correct |
97 |
Correct |
5 ms |
384 KB |
Output is correct |