답안 #730698

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
730698 2023-04-26T09:29:00 Z bgnbvnbv Feast (NOI19_feast) C++14
100 / 100
368 ms 25584 KB
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=3e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,k,pre[maxN],R[maxN],val[maxN],a[maxN];
set<pli>ms1,ms2;
set<ll> ms3;
set<ll>::iterator it;
void solve()
{
    cin >> n >> k;
    //k=1;
    for(int i=1;i<=n;i++) cin >> a[i],pre[i]=pre[i-1]+a[i];
    ll last=-1;
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]<=0) continue;
        ll j=i;
        ll sum=0;
        while(j<=n&&a[j]>0)
        {
            sum+=a[j];
            j++;
        }
        R[i]=j-1;
        val[i]=sum;
        ms1.insert({sum,i});
        ms3.insert(i);
        if(last!=-1)
        {
            ms2.insert({-(pre[i-1]-pre[R[last]]),i});
        }
        last=i;
        ans+=sum;
        i=j-1;
    }
    //cout << ans << '\n';
    while(ms3.size()>k)
    {
        if(ms2.begin()->fi<=ms1.begin()->fi)
        {
            ll x=ms2.begin()->fi;
            ans-=ms2.begin()->fi;
            ll id=ms2.begin()->se;
            ms2.erase(ms2.begin());
            it=ms3.lower_bound(id);
            ms1.erase({val[id],id});
            it--;
            ms1.erase({val[*it],*it});
            val[*it]+=val[id]-x;
            R[*it]=R[id];
            ms1.insert({val[*it],*it});
            it++;
            ms3.erase(it);
        }
        else
        {
            ans-=ms1.begin()->fi;
            ll id=ms1.begin()->se;
            ll l=-1,r=-1;
            it=ms3.upper_bound(id);
            if(it!=ms3.end()) r=*it;
            it--;
            if(it!=ms3.begin())
            {
                it--;
                l=*it;
            }
            ms1.erase(ms1.begin());
            ms3.erase(id);
            if(l!=-1)
            {
                ms2.erase({-(pre[id-1]-pre[R[l]]),id});
            }
            if(r!=-1)
            {
                ms2.erase({-(pre[r-1]-pre[R[id]]),r});
            }
            if(l!=-1&&r!=-1)
            {
                ms2.insert({-(pre[r-1]-pre[R[l]]),r});
            }
        }
    }
    cout << ans;
}
int main()
{
    fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}

Compilation message

feast.cpp: In function 'void solve()':
feast.cpp:47:21: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   47 |     while(ms3.size()>k)
      |           ~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 7588 KB Output is correct
2 Correct 36 ms 7804 KB Output is correct
3 Correct 36 ms 7884 KB Output is correct
4 Correct 33 ms 7768 KB Output is correct
5 Correct 34 ms 7756 KB Output is correct
6 Correct 40 ms 7580 KB Output is correct
7 Correct 32 ms 7628 KB Output is correct
8 Correct 34 ms 7772 KB Output is correct
9 Correct 31 ms 7628 KB Output is correct
10 Correct 38 ms 7768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7856 KB Output is correct
2 Correct 23 ms 7980 KB Output is correct
3 Correct 22 ms 7716 KB Output is correct
4 Correct 22 ms 7868 KB Output is correct
5 Correct 33 ms 7648 KB Output is correct
6 Correct 23 ms 7656 KB Output is correct
7 Correct 24 ms 7988 KB Output is correct
8 Correct 47 ms 7756 KB Output is correct
9 Correct 33 ms 7596 KB Output is correct
10 Correct 22 ms 8028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 25332 KB Output is correct
2 Correct 301 ms 25020 KB Output is correct
3 Correct 355 ms 25272 KB Output is correct
4 Correct 368 ms 24928 KB Output is correct
5 Correct 293 ms 25220 KB Output is correct
6 Correct 344 ms 25424 KB Output is correct
7 Correct 317 ms 25472 KB Output is correct
8 Correct 327 ms 25244 KB Output is correct
9 Correct 293 ms 25584 KB Output is correct
10 Correct 324 ms 25560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 472 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 472 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 7588 KB Output is correct
2 Correct 36 ms 7804 KB Output is correct
3 Correct 36 ms 7884 KB Output is correct
4 Correct 33 ms 7768 KB Output is correct
5 Correct 34 ms 7756 KB Output is correct
6 Correct 40 ms 7580 KB Output is correct
7 Correct 32 ms 7628 KB Output is correct
8 Correct 34 ms 7772 KB Output is correct
9 Correct 31 ms 7628 KB Output is correct
10 Correct 38 ms 7768 KB Output is correct
11 Correct 23 ms 7856 KB Output is correct
12 Correct 23 ms 7980 KB Output is correct
13 Correct 22 ms 7716 KB Output is correct
14 Correct 22 ms 7868 KB Output is correct
15 Correct 33 ms 7648 KB Output is correct
16 Correct 23 ms 7656 KB Output is correct
17 Correct 24 ms 7988 KB Output is correct
18 Correct 47 ms 7756 KB Output is correct
19 Correct 33 ms 7596 KB Output is correct
20 Correct 22 ms 8028 KB Output is correct
21 Correct 320 ms 25332 KB Output is correct
22 Correct 301 ms 25020 KB Output is correct
23 Correct 355 ms 25272 KB Output is correct
24 Correct 368 ms 24928 KB Output is correct
25 Correct 293 ms 25220 KB Output is correct
26 Correct 344 ms 25424 KB Output is correct
27 Correct 317 ms 25472 KB Output is correct
28 Correct 327 ms 25244 KB Output is correct
29 Correct 293 ms 25584 KB Output is correct
30 Correct 324 ms 25560 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 1 ms 320 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 328 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 472 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 2 ms 468 KB Output is correct
56 Correct 2 ms 472 KB Output is correct
57 Correct 1 ms 468 KB Output is correct
58 Correct 1 ms 468 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 182 ms 24984 KB Output is correct
62 Correct 158 ms 25552 KB Output is correct
63 Correct 263 ms 24812 KB Output is correct
64 Correct 215 ms 25412 KB Output is correct
65 Correct 234 ms 25428 KB Output is correct
66 Correct 319 ms 25336 KB Output is correct
67 Correct 242 ms 24916 KB Output is correct
68 Correct 311 ms 25440 KB Output is correct
69 Correct 309 ms 24956 KB Output is correct
70 Correct 292 ms 24724 KB Output is correct