답안 #425461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
425461 2021-06-13T04:04:26 Z errorgorn Feast (NOI19_feast) C++17
100 / 100
171 ms 14576 KB
#include <cstdio>
#include <utility>
#include <queue>
#include <cstdlib>
using namespace std;
typedef pair<int,int> ii;
typedef pair<long long,ii> iii;
int n,m,pos;
long long t,total;
int p[300005];
int r[300005];
iii node[300005];
priority_queue<iii, vector<iii>, greater<iii> > pq;
int parent(int i){return (p[i]==i)?i: p[i]=parent(p[i]);}
void unions(int i,int j){ //update node
    i=parent(i),j=parent(j);
    if (i>j) swap(i,j);
    if (r[i]>r[j]){
        p[j]=i;
        node[i]=iii (node[i].first+node[j].first, ii(node[i].second.first,node[j].second.second));
    }
    else{
        p[i]=j;
        if (r[i]==r[j]) r[j]++;
        node[j]=iii (node[i].first+node[j].first, ii(node[i].second.first,node[j].second.second));
    }
}
void init(){
    for (int x=0;x<300005;x++){
        p[x]=x;
        r[x]=0;
    }
}
int main(){
    //freopen("input.txt","r",stdin);
    scanf("%d%d",&n,&m);
    init();
    for (int x=0;x<n;x++){
        scanf("%lld",&t);
        node[x]=iii(t, ii(x,x));
    }
    for (int x=1;x<n;x++){
        if ( node[parent(x)].first==0 || (node[parent(x-1)].first^node[parent(x)].first)>=0 ){
            unions(x-1,x);
        }
    }
    for (int x=0;x<n;){
        x=parent(x);
        if ( !( (node[x].second.first==0 || node[x].second.second==n-1)&& node[x].first<0) )pq.push(iii(llabs(node[x].first),node[x].second));
        if (node[x].first>0) pos++,total+=node[x].first;
        x=node[x].second.second+1;
    }
    //printf("%d %lld\n",pos,total);
    int s,e;
    long long v;
    while (pos>m){
        v=pq.top().first,s=pq.top().second.first,e=pq.top().second.second,pq.pop();
        if (node[parent(s)].second!=ii(s,e) ) continue;
        //printf("%d %d\n",s,e);
        pos--;
        total-=v;
        if (s!=0){
            unions(s,s-1);
        }
        if (e!=n-1){
            unions(e,e+1);
        }
         if ( !( (node[parent(s)].second.first==0 || node[parent(s)].second.second==n-1)&& node[parent(s)].first<0) ) pq.push(iii(abs(node[parent(s)].first),node[parent(s)].second));
    }
    /*for (int x=0;x<n;){
        x=parent(x);
        printf("%lld %d %d\n",node[x].first,node[x].second.first,node[x].second.second);
        x=node[x].second.second+1;
    }*/
    printf("%lld\n",total);
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     scanf("%d%d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
feast.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         scanf("%lld",&t);
      |         ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 9960 KB Output is correct
2 Correct 70 ms 10024 KB Output is correct
3 Correct 59 ms 10208 KB Output is correct
4 Correct 52 ms 10024 KB Output is correct
5 Correct 53 ms 10056 KB Output is correct
6 Correct 51 ms 9968 KB Output is correct
7 Correct 53 ms 9852 KB Output is correct
8 Correct 53 ms 10052 KB Output is correct
9 Correct 53 ms 9928 KB Output is correct
10 Correct 55 ms 9972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 8228 KB Output is correct
2 Correct 42 ms 8468 KB Output is correct
3 Correct 41 ms 8244 KB Output is correct
4 Correct 43 ms 8240 KB Output is correct
5 Correct 64 ms 9920 KB Output is correct
6 Correct 49 ms 8192 KB Output is correct
7 Correct 43 ms 8360 KB Output is correct
8 Correct 53 ms 10056 KB Output is correct
9 Correct 52 ms 9928 KB Output is correct
10 Correct 45 ms 8380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 131 ms 14488 KB Output is correct
2 Correct 126 ms 14388 KB Output is correct
3 Correct 130 ms 14504 KB Output is correct
4 Correct 171 ms 14444 KB Output is correct
5 Correct 125 ms 14460 KB Output is correct
6 Correct 128 ms 14516 KB Output is correct
7 Correct 132 ms 14516 KB Output is correct
8 Correct 131 ms 14448 KB Output is correct
9 Correct 132 ms 14576 KB Output is correct
10 Correct 129 ms 14536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2592 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2592 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2596 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2592 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2592 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2596 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2592 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2592 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2592 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2592 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2596 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2592 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2592 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 3 ms 2672 KB Output is correct
27 Correct 3 ms 2604 KB Output is correct
28 Correct 3 ms 2636 KB Output is correct
29 Correct 3 ms 2604 KB Output is correct
30 Correct 3 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 9960 KB Output is correct
2 Correct 70 ms 10024 KB Output is correct
3 Correct 59 ms 10208 KB Output is correct
4 Correct 52 ms 10024 KB Output is correct
5 Correct 53 ms 10056 KB Output is correct
6 Correct 51 ms 9968 KB Output is correct
7 Correct 53 ms 9852 KB Output is correct
8 Correct 53 ms 10052 KB Output is correct
9 Correct 53 ms 9928 KB Output is correct
10 Correct 55 ms 9972 KB Output is correct
11 Correct 40 ms 8228 KB Output is correct
12 Correct 42 ms 8468 KB Output is correct
13 Correct 41 ms 8244 KB Output is correct
14 Correct 43 ms 8240 KB Output is correct
15 Correct 64 ms 9920 KB Output is correct
16 Correct 49 ms 8192 KB Output is correct
17 Correct 43 ms 8360 KB Output is correct
18 Correct 53 ms 10056 KB Output is correct
19 Correct 52 ms 9928 KB Output is correct
20 Correct 45 ms 8380 KB Output is correct
21 Correct 131 ms 14488 KB Output is correct
22 Correct 126 ms 14388 KB Output is correct
23 Correct 130 ms 14504 KB Output is correct
24 Correct 171 ms 14444 KB Output is correct
25 Correct 125 ms 14460 KB Output is correct
26 Correct 128 ms 14516 KB Output is correct
27 Correct 132 ms 14516 KB Output is correct
28 Correct 131 ms 14448 KB Output is correct
29 Correct 132 ms 14576 KB Output is correct
30 Correct 129 ms 14536 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2592 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2592 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2596 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2636 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2592 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2592 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 2 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 3 ms 2672 KB Output is correct
57 Correct 3 ms 2604 KB Output is correct
58 Correct 3 ms 2636 KB Output is correct
59 Correct 3 ms 2604 KB Output is correct
60 Correct 3 ms 2636 KB Output is correct
61 Correct 68 ms 14424 KB Output is correct
62 Correct 68 ms 14516 KB Output is correct
63 Correct 122 ms 14480 KB Output is correct
64 Correct 74 ms 14444 KB Output is correct
65 Correct 90 ms 14516 KB Output is correct
66 Correct 96 ms 14504 KB Output is correct
67 Correct 90 ms 14404 KB Output is correct
68 Correct 142 ms 14508 KB Output is correct
69 Correct 128 ms 14312 KB Output is correct
70 Correct 132 ms 14368 KB Output is correct