Submission #227496

# Submission time Handle Problem Language Result Execution time Memory
227496 2020-04-27T14:56:54 Z MKopchev Candies (JOI18_candies) C++14
100 / 100
175 ms 12936 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=4e5+42;

int n;

int inp[nmax];

bool blocked[nmax];

int left_active[nmax],right_active[nmax];

priority_queue< pair<long long/*gain*/,int/*id*/> > pq;

long long would_add[nmax];

int main()
{
    scanf("%i",&n);

    for(int i=1;i<=n;i++)
    {
        scanf("%i",&inp[i]);

        would_add[i]=inp[i];

        left_active[i]=i-1;
        right_active[i]=i+1;
        pq.push({inp[i],i});
    }

    would_add[0]=-1e18;
    would_add[n+1]=-1e18;

    left_active[0]=0;
    right_active[0]=1;

    left_active[n+1]=n;
    right_active[n+1]=n+1;

    long long outp=0;

    for(int j=1;j<=(n+1)/2;j++)
    {
        while(blocked[pq.top().second])pq.pop();

        /*
        cout<<pq.size()<<" "<<pq.top().first<<" "<<pq.top().second<<endl;

        for(int p=1;p<=n+1+j;p++)cout<<p<<" "<<blocked[p]<<" "<<left_active[p]<<" "<<right_active[p]<<endl;

        for(int p=1;p<=n+1+j;p++)
            if(blocked[p]==0)cout<<p<<" -> "<<would_add[p]<<endl;

        cout<<"---"<<endl;
        */

        outp+=pq.top().first;

        int id=pq.top().second;

        pq.pop();

        int new_id=n+1+j;

        would_add[new_id]=would_add[left_active[id]]+would_add[right_active[id]]-would_add[id];

        //cout<<j<<" -> "<<new_id<<" "<<would_add[new_id]<<" id= "<<id<<endl;

        pq.push({would_add[new_id],new_id});

        blocked[left_active[id]]=1;
        blocked[right_active[id]]=1;
        blocked[id]=1;

        left_active[new_id]=left_active[left_active[id]];

        right_active[left_active[left_active[id]]]=new_id;

        right_active[new_id]=right_active[right_active[id]];

        left_active[right_active[right_active[id]]]=new_id;

        printf("%lld\n",outp);
    }
    return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
candies.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i",&inp[i]);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 137 ms 12764 KB Output is correct
22 Correct 167 ms 12892 KB Output is correct
23 Correct 156 ms 12892 KB Output is correct
24 Correct 107 ms 12644 KB Output is correct
25 Correct 107 ms 12636 KB Output is correct
26 Correct 111 ms 12724 KB Output is correct
27 Correct 126 ms 12764 KB Output is correct
28 Correct 120 ms 12896 KB Output is correct
29 Correct 115 ms 12764 KB Output is correct
30 Correct 130 ms 12868 KB Output is correct
31 Correct 139 ms 12892 KB Output is correct
32 Correct 120 ms 12844 KB Output is correct
33 Correct 142 ms 12692 KB Output is correct
34 Correct 139 ms 12612 KB Output is correct
35 Correct 138 ms 12744 KB Output is correct
36 Correct 160 ms 12936 KB Output is correct
37 Correct 171 ms 12816 KB Output is correct
38 Correct 146 ms 12764 KB Output is correct
39 Correct 120 ms 12720 KB Output is correct
40 Correct 124 ms 12636 KB Output is correct
41 Correct 109 ms 12764 KB Output is correct
42 Correct 134 ms 12880 KB Output is correct
43 Correct 133 ms 12896 KB Output is correct
44 Correct 118 ms 12764 KB Output is correct
45 Correct 124 ms 12764 KB Output is correct
46 Correct 150 ms 12764 KB Output is correct
47 Correct 175 ms 12892 KB Output is correct
48 Correct 146 ms 12644 KB Output is correct
49 Correct 149 ms 12656 KB Output is correct
50 Correct 125 ms 12636 KB Output is correct