/*
Compete against Yourself.
Author - Aryan (@aryanc403)
*/
/*
Credits -
Atcoder library - https://atcoder.github.io/ac-library/production/document_en/ (namespace atcoder)
Github source code of library - https://github.com/atcoder/ac-library/tree/master/atcoder
https://codeforces.com/contest/4/submission/150120627
*/
#ifdef ARYANC403
#include <header.h>
#else
#pragma GCC optimize ("Ofast")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC target ("sse,sse2,mmx")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define dbg(args...) 42;
#define endl "\n"
#endif
// y_combinator from @neal template https://codeforces.com/contest/1553/submission/123849801
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun> class y_combinator_result {
Fun fun_;
public:
template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
using namespace std;
#define fo(i,n) for(i=0;i<(n);++i)
#define repA(i,j,n) for(i=(j);i<=(n);++i)
#define repD(i,j,n) for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define eb emplace_back
#define X first
#define Y second
using lli = long long int;
using mytype = long double;
using ii = pair<lli,lli>;
using vii = vector<ii>;
using vi = vector<lli>;
template <class T>
using ordered_set = __gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
// X.find_by_order(k) return kth element. 0 indexed.
// X.order_of_key(k) returns count of elements strictly less than k.
// namespace Re = std::ranges;
// namespace Ve = std::ranges::views;
const lli SEED=chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 rng(SEED);
inline lli rnd(lli l,lli r)
{return uniform_int_distribution<lli>(l,r)(rng);}
int find_best(int n);
std::vector<int> ask(int i);
const lli LIM = 4000;
int find_best(int n){
const vector<int> bst = {0,0};
if(n<=LIM){
for(lli i=0;i<n;i++){
if(ask(i)==bst)
return i;
}
}
map<lli,ii> dp;
lli ans = -1;
auto getVal = [&](const lli i)->ii{
if(dp.count(i))
return dp[i];
const auto res = ask(i);
dp[i]={res[0],res[1]};
if(res==bst)
ans=i;
return dp[i];
};
lli mxVal=0;
for(lli it=0;it<100;it++){
const lli v=rnd(0,n-1);
const auto res=getVal(v);
mxVal=max(mxVal,res.X+res.Y);
}
y_combinator([&](const auto &self,lli l,lli r)->void{
if(ans!=-1)
return;
while(l<=r){
const auto res=getVal(l);
if(res.X+res.Y>=mxVal)
break;
l++;
}
while(l<=r){
const auto res=getVal(r);
if(res.X+res.Y>=mxVal)
break;
r--;
}
if(l>r)
return;
if(getVal(l).X==getVal(r).X)
return;
if(r-l<=1)
return;
const lli mid = (l+r)/2;
self(l,mid);
self(mid,r);
})(0,n-1);
dbg(dp);
assert(ans!=-1);
return ans;
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:22:26: warning: statement has no effect [-Wunused-value]
22 | #define dbg(args...) 42;
| ^~
prize.cpp:125:5: note: in expansion of macro 'dbg'
125 | dbg(dp);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
444 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
448 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
2 ms |
708 KB |
Output is correct |
12 |
Correct |
1 ms |
440 KB |
Output is correct |
13 |
Correct |
4 ms |
700 KB |
Output is correct |
14 |
Correct |
2 ms |
440 KB |
Output is correct |
15 |
Correct |
4 ms |
968 KB |
Output is correct |
16 |
Correct |
23 ms |
1268 KB |
Output is correct |
17 |
Correct |
1 ms |
440 KB |
Output is correct |
18 |
Correct |
24 ms |
1036 KB |
Output is correct |
19 |
Correct |
1 ms |
436 KB |
Output is correct |
20 |
Correct |
8 ms |
1224 KB |
Output is correct |
21 |
Correct |
8 ms |
1288 KB |
Output is correct |
22 |
Correct |
2 ms |
712 KB |
Output is correct |
23 |
Correct |
1 ms |
440 KB |
Output is correct |
24 |
Correct |
1 ms |
448 KB |
Output is correct |
25 |
Correct |
12 ms |
1568 KB |
Output is correct |
26 |
Correct |
12 ms |
968 KB |
Output is correct |
27 |
Correct |
2 ms |
444 KB |
Output is correct |
28 |
Correct |
19 ms |
1100 KB |
Output is correct |
29 |
Correct |
16 ms |
1112 KB |
Output is correct |
30 |
Correct |
34 ms |
1820 KB |
Output is correct |
31 |
Correct |
1 ms |
444 KB |
Output is correct |
32 |
Correct |
3 ms |
700 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
700 KB |
Output is correct |
35 |
Correct |
1 ms |
696 KB |
Output is correct |
36 |
Correct |
5 ms |
988 KB |
Output is correct |
37 |
Correct |
2 ms |
700 KB |
Output is correct |
38 |
Correct |
1 ms |
444 KB |
Output is correct |
39 |
Correct |
14 ms |
560 KB |
Output is correct |
40 |
Correct |
21 ms |
1060 KB |
Output is correct |
41 |
Correct |
13 ms |
1272 KB |
Output is correct |
42 |
Correct |
14 ms |
1056 KB |
Output is correct |
43 |
Correct |
13 ms |
1332 KB |
Output is correct |
44 |
Correct |
12 ms |
1216 KB |
Output is correct |
45 |
Correct |
8 ms |
1488 KB |
Output is correct |
46 |
Correct |
1 ms |
436 KB |
Output is correct |
47 |
Correct |
12 ms |
1244 KB |
Output is correct |
48 |
Correct |
18 ms |
1260 KB |
Output is correct |
49 |
Correct |
2 ms |
700 KB |
Output is correct |
50 |
Correct |
26 ms |
1400 KB |
Output is correct |
51 |
Correct |
9 ms |
1228 KB |
Output is correct |
52 |
Correct |
1 ms |
444 KB |
Output is correct |
53 |
Correct |
1 ms |
696 KB |
Output is correct |
54 |
Correct |
13 ms |
1252 KB |
Output is correct |
55 |
Correct |
1 ms |
436 KB |
Output is correct |
56 |
Correct |
34 ms |
1428 KB |
Output is correct |
57 |
Correct |
15 ms |
1368 KB |
Output is correct |
58 |
Correct |
24 ms |
984 KB |
Output is correct |
59 |
Correct |
15 ms |
2080 KB |
Output is correct |
60 |
Correct |
14 ms |
1280 KB |
Output is correct |
61 |
Correct |
1 ms |
448 KB |
Output is correct |
62 |
Correct |
1 ms |
444 KB |
Output is correct |
63 |
Correct |
1 ms |
440 KB |
Output is correct |
64 |
Correct |
1 ms |
444 KB |
Output is correct |
65 |
Correct |
1 ms |
444 KB |
Output is correct |
66 |
Correct |
3 ms |
712 KB |
Output is correct |
67 |
Correct |
3 ms |
472 KB |
Output is correct |
68 |
Correct |
2 ms |
464 KB |
Output is correct |
69 |
Correct |
3 ms |
884 KB |
Output is correct |
70 |
Correct |
2 ms |
452 KB |
Output is correct |
71 |
Correct |
25 ms |
1924 KB |
Output is correct |
72 |
Correct |
3 ms |
724 KB |
Output is correct |
73 |
Correct |
33 ms |
932 KB |
Output is correct |
74 |
Correct |
30 ms |
952 KB |
Output is correct |
75 |
Correct |
1 ms |
452 KB |
Output is correct |
76 |
Correct |
28 ms |
1288 KB |
Output is correct |
77 |
Correct |
31 ms |
1528 KB |
Output is correct |
78 |
Correct |
3 ms |
440 KB |
Output is correct |
79 |
Correct |
13 ms |
776 KB |
Output is correct |
80 |
Correct |
28 ms |
712 KB |
Output is correct |
81 |
Correct |
29 ms |
1488 KB |
Output is correct |
82 |
Correct |
31 ms |
1756 KB |
Output is correct |
83 |
Correct |
1 ms |
432 KB |
Output is correct |
84 |
Correct |
19 ms |
1100 KB |
Output is correct |
85 |
Correct |
25 ms |
956 KB |
Output is correct |
86 |
Correct |
4 ms |
696 KB |
Output is correct |
87 |
Correct |
2 ms |
696 KB |
Output is correct |
88 |
Correct |
6 ms |
960 KB |
Output is correct |
89 |
Correct |
6 ms |
696 KB |
Output is correct |
90 |
Correct |
1 ms |
440 KB |
Output is correct |
91 |
Correct |
6 ms |
952 KB |
Output is correct |
92 |
Correct |
1 ms |
436 KB |
Output is correct |
93 |
Correct |
3 ms |
952 KB |
Output is correct |
94 |
Correct |
3 ms |
956 KB |
Output is correct |
95 |
Correct |
2 ms |
460 KB |
Output is correct |
96 |
Correct |
2 ms |
444 KB |
Output is correct |
97 |
Correct |
1 ms |
444 KB |
Output is correct |