제출 #1213195

#제출 시각아이디문제언어결과실행 시간메모리
1213195hainam2k9Candies (JOI18_candies)C++20
0 / 100
1 ms320 KiB
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 2e5+5;
const string NAME = "";
int n,a[MAXN];
ll rs=0;
bool del[MAXN];
priority_queue<pair<int,int>> pq;
multiset<pair<ll,int>> ms;
int p[MAXN],l[MAXN];
ll sum[MAXN];
bool type[MAXN];
int Find(int u){
    if(u==p[u]) return u;
    return p[u]=Find(p[u]);
}
void Union(int x, int y){
    x=Find(x), y=Find(y);
    if(x==y) return;
    if(x<y) swap(x,y);
    p[y]=x, sum[x]+=sum[y], type[x]|=type[y], l[x]=min(l[x],l[y]);
}
int main()
{
    tt;
    if(fopen((NAME + ".INP").c_str(), "r")) fo;
    cin >> n;
    for(int i = 1; i<=n; ++i)
        cin >> a[i], pq.ins(a[i],i), sum[i]=-a[i];
    iota(p+1,p+n+1,1);
    iota(l+1,l+n+1,1);
    for(int i = 1; i<=(n+1)/2; ++i){
        pair<int,int> top=pq.top();
        while(!pq.empty()&&(del[top.se]||del[top.se-1]||del[top.se+1])) pq.pop(), top=pq.top();
        if(del[top.se]||del[top.se-1]||del[top.se+1]||(!ms.empty()&&ms.rbegin()->fi>top.fi)){
            int pos=ms.rbegin()->se;
            rs+=ms.rbegin()->fi, ms.erase(prev(ms.end()));
            type[pos]=del[pos+1]=del[l[pos]-1]=1, Union(pos,pos+1), Union(pos,l[pos]-1);
        }else{
            del[top.se]=1, rs+=top.fi, pq.pop();
            int add=a[top.se-1]+a[top.se+1];
            if(top.se>2&&del[top.se-2]){
                if(!type[top.se-2]) ms.erase(mp(sum[top.se-2],top.se-2));
                add-=a[top.se-1], Union(top.se,top.se-2);
            }
            if(del[top.se+2]){
                if(!type[Find(top.se+2)]) ms.erase(mp(sum[Find(top.se+2)],Find(top.se+2)));
                add-=a[top.se+1], Union(top.se,top.se+2);
            }
            top.se=Find(top.se), sum[top.se]+=add;
            if(top.se!=n&&l[top.se]!=1&&!type[top.se]) ms.ins(sum[top.se],top.se);
        }
        cout << rs << "\n";
    }
}

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

candies.cpp: In function 'int main()':
candies.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:43:45: note: in expansion of macro 'fo'
   43 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
candies.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:43:45: note: in expansion of macro 'fo'
   43 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...