Submission #336330

# Submission time Handle Problem Language Result Execution time Memory
336330 2020-12-15T04:51:39 Z hackxsaras Rice Hub (IOI11_ricehub) C++14
100 / 100
35 ms 3300 KB
#include "bits/stdc++.h"
// #include "chrono"

using namespace std;
// using namespace std::chrono; 
 
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define pb push_back
#define galen_colin {ios_base::sync_with_stdio(false);}
#define orz {cin.tie(NULL); cout.tie(NULL);}
#define fix(prec) {cout << setprecision(prec) << fixed;}
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
    cout << ""; for(int i = 0; i < v.size(); i++) {if (i) cout << " "; cout << v[i];} return cout << "\n";
}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
    cin >> p.first;
    return cin >> p.second;
}
template<typename A> istream& operator>>(istream& cin, vector<A> &v) {
    for(auto &x:v)cin>>x;
    return cin;
}

ll min(ll a, int b){return min(a, (ll) b);}
ll min(int a, ll b){return min(b, (ll) a);}
ll max(ll a, int b){return max(a, (ll) b);}
ll max(int a, ll b){return max(b, (ll) a);}

void usaco(string filename) {
  // #pragma message("be careful, freopen may be wrong")
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}
 
const ld pi = 3.14159265358979323846;
const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;
const ll INF = 1e9+7;
 


ll n, m, k, q, l, r, x, y, z;

const ll sz = 1e6 + 1;
string s, t;
ll ans = 0;

//BINARY SEARCH OVER THE NUMBER OF ALLOTED THINGS, 

//we can calcuate  a prefix array, and then search for the optimal point and break into -ve and +ve

//now if any of the window gets true, return true
vl pre, rice;
bool check2(deque<ll> &v, ll alloted, ll s){

    //the median
    ll med = v[v.size()/2 - (v.size()%2==0)];
    // cout<<"\n";
    // cout<<med<<" ";
    ll l=s, r=s + v.size() - 1;
    ll optimal = s;
    while(l<=r){
        ll m = (l+r)/2;
        if(med >= rice[m]){
            optimal = m;
            l = m+1;
        } else {
            r = m-1;
        }
    }
    ll left = optimal-s + 1;
    ll right = v.size() - left;
    ll cost = med*left - (pre[optimal+1] - pre[s]) + pre[min(n, s + v.size())] - pre[min(optimal+1, n)]  - med*right;//excluding optimal in Right
    // for(int i=0;i<v.size();i++)cout<<v[i]<<" ";cout<<"=";
    // cout<<cost<<" "<< left<<" "<<right<<" "<<optimal<<"\n";
    return cost <= alloted;
}
bool check1(ll window){
    deque<ll> v;
    for(int i=0;i<window;i++){
        v.pb(rice[i]);
    }
    bool ans = check2(v, k, 0);
    for(ll i=window;i<n;i++){
        v.pop_front();
        v.pb(rice[i]);
        ans |= check2(v, k, i - window + 1);
    }
    return ans;
}
ll besthub(int R, int L, int ricef[], ll B) {
    n = R, k = B;
    pre.resize(R+1);
    rice.resize(R);
    for(ll i=0;i<R;i++){
        rice[i] = ricef[i];
        pre[i+1] = pre[i] + rice[i];
    }
    ll ans = 1;
    ll l=2, r = R;
    while(l <= r){
        m = (l+r)/2;
        //cout<<m<<" "<<check1(m)<<"\n";
        if(check1(m)){
            ans = m;
            l = m+1;
        } else {
            r = m-1;
        }
    }
    return ans;
}
 
// int main() {
//     int R = 5, L = 20, B = 6, X[5] = {1,2,10,12,14};
//     cout<<besthub(R, L, X, B);
// } 

Compilation message

ricehub.cpp: In function 'void usaco(std::string)':
ricehub.cpp:56:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   56 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ricehub.cpp:57:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   57 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 3 ms 492 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 2 ms 492 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
28 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 748 KB Output is correct
2 Correct 9 ms 748 KB Output is correct
3 Correct 31 ms 3212 KB Output is correct
4 Correct 31 ms 3212 KB Output is correct
5 Correct 27 ms 1516 KB Output is correct
6 Correct 29 ms 1516 KB Output is correct
7 Correct 30 ms 3212 KB Output is correct
8 Correct 35 ms 3300 KB Output is correct
9 Correct 33 ms 1516 KB Output is correct
10 Correct 31 ms 1568 KB Output is correct
11 Correct 31 ms 3212 KB Output is correct
12 Correct 31 ms 3212 KB Output is correct
13 Correct 25 ms 1516 KB Output is correct
14 Correct 26 ms 1516 KB Output is correct
15 Correct 25 ms 2540 KB Output is correct
16 Correct 23 ms 2504 KB Output is correct
17 Correct 27 ms 2900 KB Output is correct
18 Correct 27 ms 2900 KB Output is correct
19 Correct 30 ms 3056 KB Output is correct
20 Correct 30 ms 3012 KB Output is correct