#include "prize.h"
#include <stdio.h>
#include <vector>
#include <algorithm>
int L[200005], R[200005];
std::vector<int> V;
std::vector<int> W;
int min(int a, int b)
{
return (a < b)?a:b;
}
int max(int a, int b)
{
return (a > b)?a:b;
}
int find_best(int n) {
int i;
for (i = 0; i < n; i++)
L[i] = -1;
int sqrtn;
for (sqrtn = 1; sqrtn*sqrtn < n; sqrtn++);
// printf("sqrtn %d\n", sqrtn);
std::vector<int> r;
for (i = 0; i*sqrtn < n; i++)
{
r = ask(i*sqrtn);
L[i*sqrtn] = r[0];
R[i*sqrtn] = r[1];
V.push_back(-r[1]);
W.insert(W.begin(), -r[0]);
}
// for (i = 0; i < W.size(); i++)
// printf("%d\n", W[i]);
int sig = 0;
if (L[sig] == 0 && R[sig] == 0)
return sig;
int mn, md, mx;
//find first sig
mn = 0;
mx = n - 1;
while (mn != mx)
{
// printf("mn %d mx %d\n", mn, mx);
if (mx - mn == 1)
{
sig = mx;
break;
}
md = (mn + mx)/2;
if (L[md] == -1)
{
r = ask(md);
L[md] = r[0];
R[md] = r[1];
}
if (R[md] >= R[sig])
mn = md;
else
mx = md;
}
if (mn == mx)
sig = mn;
if (L[sig] == -1)
{
r = ask(sig);
L[sig] = r[0];
R[sig] = r[1];
}
int ssig = sig;
//go right
while (L[sig] != 0 || R[sig] != 0)
{
// printf("sig %d\n", sig);
mx = std::lower_bound(V.begin() + sig/sqrtn + 1, V.end(), -R[sig]) - V.begin();
mx *= sqrtn;
mx = min(mx, n - 1);
mn = mx - sqrtn;
mn = max(mn, sig);
// printf("sig %d mn %d mx %d\n", sig, mn, mx);
while (mn != mx)
{
if (mx - mn == 1)
{
sig = mx;
break;
}
md = (mn + mx)/2;
if (L[md] == -1)
{
r = ask(md);
L[md] = r[0];
R[md] = r[1];
}
if (R[md] > R[sig])
mn = md;
else
mx = md;
}
if (mn == mx)
sig = mn;
if (L[sig] == -1)
{
r = ask(sig);
L[sig] = r[0];
R[sig] = r[1];
}
if (sig == n - 1 && (L[sig] != 0 || R[sig] != 0))
break;
}
if (sig == n - 1 && (L[sig] != 0 || R[sig] != 0))
{
sig = ssig;
while (L[sig] != 0 || R[sig] != 0)
{
// printf("sig %d %d\n", sig, -L[sig]);
mn = W.end() - std::lower_bound(W.end() - 1 - (sig - 1)/sqrtn, W.end(), -L[sig]) - 1;
mn *= sqrtn;
mn = max(mn, 0);
mx = mn + sqrtn;
mx = min(mx, sig);
// printf("sig %d mn %d mx %d\n", sig, mn, mx);
while (mn != mx)
{
if (mx - mn == 1)
{
sig = mn;
break;
}
md = (mn + mx)/2;
if (L[md] == -1)
{
r = ask(md);
L[md] = r[0];
R[md] = r[1];
}
if (L[md] > L[sig])
mx = md;
else
mn = md;
}
if (mn == mx)
sig = mn;
if (L[sig] == -1)
{
r = ask(sig);
L[sig] = r[0];
R[sig] = r[1];
}
}
return sig;
}
else
return sig;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3584 KB |
Output is correct |
2 |
Correct |
8 ms |
3584 KB |
Output is correct |
3 |
Correct |
3 ms |
3584 KB |
Output is correct |
4 |
Correct |
0 ms |
3584 KB |
Output is correct |
5 |
Correct |
4 ms |
3584 KB |
Output is correct |
6 |
Correct |
4 ms |
3584 KB |
Output is correct |
7 |
Correct |
5 ms |
3584 KB |
Output is correct |
8 |
Correct |
1 ms |
3584 KB |
Output is correct |
9 |
Correct |
3 ms |
3584 KB |
Output is correct |
10 |
Correct |
4 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3584 KB |
Output is correct |
2 |
Correct |
3 ms |
3584 KB |
Output is correct |
3 |
Correct |
6 ms |
3584 KB |
Output is correct |
4 |
Correct |
2 ms |
3584 KB |
Output is correct |
5 |
Correct |
1 ms |
3584 KB |
Output is correct |
6 |
Correct |
1 ms |
3584 KB |
Output is correct |
7 |
Correct |
3 ms |
3584 KB |
Output is correct |
8 |
Correct |
6 ms |
3584 KB |
Output is correct |
9 |
Correct |
0 ms |
3584 KB |
Output is correct |
10 |
Correct |
1 ms |
3584 KB |
Output is correct |
11 |
Incorrect |
21 ms |
3584 KB |
Incorrect |
12 |
Halted |
0 ms |
0 KB |
- |