Submission #232658

#TimeUsernameProblemLanguageResultExecution timeMemory
232658dualityConstellation 3 (JOI20_constellation3)C++11
35 / 100
1587 ms19828 KiB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int A[200000];
vpii stars[200000];
int l[200000],r[200000];
vi S;
bool comp(int a,int b) {
    return r[a]-l[a] < r[b]-l[b];
}
int order[200000];
LLI dp[200000];
int main() {
    int i;
    int N,M,X,Y,C;
    LLI sum = 0;
    scanf("%d",&N);
    for (i = 0; i < N; i++) scanf("%d",&A[i]);
    scanf("%d",&M);
    for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;

    int j;
    for (i = 0; i < N; i++) {
        while (!S.empty() && (A[S.back()] <= A[i])) S.pop_back();
        if (!S.empty()) l[i] = S.back();
        else l[i] = -1;
        S.pb(i);
    }
    S.clear();
    for (i = N-1; i >= 0; i--) {
        while (!S.empty() && (A[S.back()] < A[i])) S.pop_back();
        if (!S.empty()) r[i] = S.back();
        else r[i] = N;
        S.pb(i);
    }
    for (i = 0; i < N; i++) order[i] = i,sort(stars[i].begin(),stars[i].end(),greater<pii>());
    sort(order,order+N,comp);
    LLI ans = 0;
    for (i = 0; i < N; i++) {
        int u = order[i];
        LLI maxl = 0,maxr = 0;
        for (j = l[u]+1; j < u; j++) {
            while (!stars[j].empty() && (stars[j].back().first <= A[u])) {
                maxl = max(maxl,dp[j]+stars[j].back().second);
                stars[j].pop_back();
            }
            maxl = max(maxl,dp[j]);
        }
        for (j = u+1; j < r[u]; j++) {
            while (!stars[j].empty() && (stars[j].back().first <= A[u])) {
                maxr = max(maxr,dp[j]+stars[j].back().second);
                stars[j].pop_back();
            }
            maxr = max(maxr,dp[j]);
        }
        for (j = l[u]+1; j <= u; j++) dp[j] += maxr;
        for (j = u; j < r[u]; j++) dp[j] += maxl;
        ans = max(ans,dp[u]);
    }
    for (i = 0; i < N; i++) {
        while (!stars[i].empty()) ans = max(ans,stars[i].back().second+dp[i]),stars[i].pop_back();
    }
    printf("%lld\n",sum-ans);

    return 0;
}

Compilation message (stderr)

constellation3.cpp: In function 'int main()':
constellation3.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&N);
     ~~~~~^~~~~~~~~
constellation3.cpp:72:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&A[i]);
                             ~~~~~^~~~~~~~~~~~
constellation3.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&M);
     ~~~~~^~~~~~~~~
constellation3.cpp:74:78: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...