이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define ull unsigned int
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
//int find_best(int n);
//std::vector<int> ask(int i);
#include "prize.h"
ll maxi = 0;
ll posa = 50;
ll solve(ll l,ll r,ll L,ll R){
if(l > r){
return -1;
}
ll mid = (l+r)/2;
vector<int>res = ask(mid);
posa++;
if(res[0] == 0 && res[1] == 0){
return mid;
}
if(l == r){
return -1;
}
ll ans = -1;
ll pos = mid;
while(pos < r && (res[0] + res[1]) < maxi){
pos++;
res = ask(pos);
posa++;
if(res[0] == 0 && res[1] == 0){
return pos;
}
}
if(res[0] + res[1] < maxi){
pos = mid - 1;
while(pos > l && (res[0] + res[1]) < maxi){
pos--;
res = ask(pos);
posa++;
if(res[0] == 0 && res[1] == 0){
return pos;
}
}
}
while(posa >= 7000){
}
if((res[0] + res[1]) == maxi){
if(res[0] - L > res[1] - R){
ans = solve(l,mid,L,R + res[1]);
if(ans == -1){
ans = solve(mid+1,r,L + res[0],R);
}
}
else{
ans = solve(mid+1,r,L + res[0],R);
if(ans == -1){
ans = solve(l,mid,L,R + res[1]);
}
}
}
return ans;
}
int find_best(int n){
srand(time(0));
ll k = 50;
while(k--){
vector<int> res = ask(rand() % n);
maxi = max(maxi,res[0] + res[1]);
}
return solve(0,n-1,0,0);
}
/*
static const int max_q = 10000;
static int n;
static int query_count = 0;
static vector<int> g;
static vector<vector<int> > rank_count;
vector<int> ask(int i) {
query_count++;
if(query_count > max_q) {
cerr << "Query limit exceeded" << endl;
exit(0);
}
if(i < 0 || i >= n) {
cerr << "Bad index: " << i << endl;
exit(0);
}
vector<int> res(2);
res[0] = rank_count[g[i] - 1][i + 1];
res[1] = rank_count[g[i] - 1][n] - res[0];
return res;
}
int main() {
cin >> n;
g.resize(n);
for(int i = 0; i < n; i++) {
cin >> g[i];
if(g[i] < 1) {
cerr << "Invalid rank " << g[i] << " at index " << i << endl;
exit(0);
}
}
int max_rank = *max_element(g.begin(), g.end());
rank_count.resize(max_rank + 1, vector<int>(n + 1, 0));
for(int r = 0; r <= max_rank; r++) {
for(int i = 1; i <= n; i++) {
rank_count[r][i] = rank_count[r][i - 1];
if(g[i - 1] == r)
rank_count[r][i]++;
}
}
for(int i = 0; i <= n; i++)
for(int r = 1; r <= max_rank; r++)
rank_count[r][i] += rank_count[r - 1][i];
int res = find_best(n);
cout << res << endl << "Query count: " << query_count << endl;
return 0;
}
8
3 2 3 3 3 3 2 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |