Submission #1078384

#TimeUsernameProblemLanguageResultExecution timeMemory
1078384steveonalexHoliday (IOI14_holiday)C++17
0 / 100
59 ms5464 KiB
#include "holiday.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }


struct FenwickTree{
    int n;
    vector<ll> a;

    FenwickTree(int _n){
        n = _n;
        a.resize(n+1);
    }

    void update(int i, ll v){
        while(i <= n){
            a[i] += v;
            i += LASTBIT(i);
        }
    }

    ll get(int i){
        ll ans = 0;
        while(i > 0){
            ans += a[i];
            i -= LASTBIT(i);
        }
        return ans;
    }

    int lower_bound(ll v){
        if (v == 0) return 0;
        int p = MASK(logOf(n)), idx = 0;
        while(p > 0){
            if (idx + p < n && v > a[idx + p]){
                idx += p;
                v -= a[idx];
            }
            p >>= 1;
        }
        return idx + 1;
    }
};

ll findMaxAttraction_Chiral(int n, int s, int d, vector<int> a){
    // find max attraction, where distance to the left get doubled, while distance to the right doesn't
    ll ans = 0;

    vector<pair<int, int>> b(n);
    for(int i = 0; i<n; ++i) b[i] = {a[i], i};
    sort(ALL(b)); reverse(ALL(b));

    vector<int> pos(n);
    for(int i = 0; i<n; ++i) pos[b[i].second] = i + 1;

    // first, find the distance to the right
    vector<int> dp1((n - s) * 2);
    FenwickTree bit(n), sum(n);
    for(int i = s; i<n; ++i){
        bit.update(pos[i], 1); sum.update(pos[i], a[i]);
    }

    // cout << "Jujutsu ki\n";
    pair<int, int> cur = {(n - s) - 1, (n - s)};
    while(cur.first + cur.second > 0){
        dp1[cur.first + cur.second] = sum.get(bit.lower_bound(cur.second));

        ll step1 = -1;
        if (cur.second > 0) {
            step1 = sum.get(bit.lower_bound(cur.second - 1));
        }

        ll step2 = -1;
        if (cur.first > 0){
            int idx = s + cur.first;
            bit.update(pos[idx], -1); sum.update(pos[idx], -a[idx]); 
            step2 = sum.get(bit.lower_bound(cur.second));
        }
        if (step1 >= step2){
            if (cur.first > 0){
                int idx = s + cur.first;
                bit.update(pos[idx], 1); sum.update(pos[idx], a[idx]); 
            }
            cur.second--;
        }
        else cur.first--;
    }

    for(int i = 0; i <= min(d, dp1.size() - 1); ++i) 
        maximize(ans, dp1[i]);

    return ans;
}

ll findMaxAttraction(int n, int s, int d, int attraction[]){
    ll ans = 0;
    if (s != 0) exit(1);
    for(int x = 0; x <= 0; ++x){
        vector<int> a(n);
        for(int i = 0; i<n; ++i) a[i] = attraction[i];

        if (x) { 
            reverse(ALL(a));
            s = n - s - 1;
        }

        maximize(ans, findMaxAttraction_Chiral(n, s, d, a));
    }

    return ans;
}

// int main(void){
//     ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
//     clock_t start = clock();

//     int n, s, d;
//     cin >> n >> s >> d;
//     int attraction[n];
//     for(int i = 0; i<n; ++i) cin >> attraction[i];

//     cout << findMaxAttraction(n, s, d, attraction) << "\n";

//     cerr << "Time elapsed: " << clock() - start << " ms\n";
//     return 0;
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...