제출 #889161

#제출 시각아이디문제언어결과실행 시간메모리
889161dimashhhFeast (NOI19_feast)C++17
100 / 100
291 ms26572 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1, MOD = 998244353;
typedef long long ll;
#define int long long
int n, k, a[N],p[N];
bool ok[N];
vector<ll> b, sum(N);
set<int> pos;
set<pair<ll,ll>> st;
ll res = 0,col = 0;
void del(int i){
    if(i >= 0 && i < n){
        st.erase({abs(sum[i]),i});
        pos.erase(i);
        ok[i] = 1;
        if(sum[i] >= 0){
            res -= sum[i];
            col--;
        }
    }
}
void test()
{
    cin >> n >> k;
    ll lst = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        ok[i] = (a[i] >= 0);
    }
    ll cur = 0;
    ok[0] = 1;
    for (int i = 1; i <= n; i++)
    {
        if (ok[i] == ok[i - 1])
        {
            cur += a[i];
        }
        else
        {
            if(b.empty() && cur < 0){
            }
            else b.push_back(cur);
            cur = a[i];
        }
    }
    if(cur >= 0) b.push_back(cur);
    n = (int)b.size();
    for(int i = 0;i < n;i++){
        p[i] = i;
        sum[i] = b[i];
        if(b[i] >= 0){
            col++;
            res += b[i];
        }
        pos.insert(i);
        st.insert({abs(b[i]),i});
    }
    int it = 0;
    while(col > k){
        auto [x,y] = (*st.begin());
        st.erase({x,y});
        // cout << x << ' ' << y << ' ' << res << ' ' << col << '\n';
        auto it = pos.upper_bound(y);
        ll nv = sum[y];
        del(y);
        if(it != pos.end()){
            nv += sum[*it];
            del(*it);
        }
        auto it1 = pos.lower_bound(y);
        if(it1 != pos.begin()){
            it1--;
            nv += sum[*it1];
            del(*it1);
        }
        sum[y] = nv;
        if(sum[y] >= 0){
            pos.insert(y);
            st.insert({abs(sum[y]),y});
            col++;
            res += sum[y];
        }else{
            if(!pos.empty() && *pos.begin() < y && *pos.rbegin() > y){
                pos.insert(y);
                st.insert({abs(sum[y]),y});               
            }
        }
    }
    cout << res;
}
main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1; // cin >> T;
    while (T--)
        test();
}

컴파일 시 표준 에러 (stderr) 메시지

feast.cpp: In function 'void test()':
feast.cpp:26:8: warning: unused variable 'lst' [-Wunused-variable]
   26 |     ll lst = 0;
      |        ^~~
feast.cpp:60:9: warning: unused variable 'it' [-Wunused-variable]
   60 |     int it = 0;
      |         ^~
feast.cpp: At global scope:
feast.cpp:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   93 | main()
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...