Codeforces 746F Music in Car

Music in Car

用两个Set维护一下尺取的过程。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1);

int n, w, k, a[N], t[N];
bool flag[N];

set<PII> Set1;
set<PII> Set2;

int main() {
    scanf("%d%d%d", &n, &w, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &t[i]);
    int now = 0, tmp = 0, ans = 0;
    for(int i = 1, j = 1; i <= n; ++i){
        now += (1 + t[i]) >> 1;
        tmp += a[i];
        if(Set1.size() < w){
            Set1.insert(mk(t[i] >> 1, i));
            flag[i] = true;
        }else{
            if((*Set1.begin()).fi < (t[i] >> 1)){
                PII tt = *Set1.begin();
                Set1.erase(Set1.begin());
                now += tt.fi;
                flag[tt.se] = false;
                Set1.insert(mk(t[i] >> 1, i));
                flag[i] = true;
                Set2.insert(tt);
            }
            else{
                now += t[i] >> 1;
                Set2.insert(mk(t[i] >> 1, i));
            }
        }
        while(now > k){
            if(flag[j]){
                now -= (t[j] + 1) >> 1;
                Set1.erase(mk(t[j] >> 1 , j));
                while(Set2.size() && (*Set2.rbegin()).se < j) Set2.erase(*Set2.rbegin());
                if(Set2.size()){
                    PII tt = *Set2.rbegin();Set2.erase(*Set2.rbegin());
                    now -= tt.fi;
                    Set1.insert(tt);
                    flag[tt.se] = true;
                }
            }
            else{
                now -= t[j];
            }
            tmp -= a[j];
            ++j;
        }
        ans = max(ans, tmp);
    }
    printf("%d
", ans);
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/10618745.html