제출 #1082256

#제출 시각아이디문제언어결과실행 시간메모리
1082256ByeWorldBigger segments (IZhO19_segments)C++14
100 / 100
139 ms22100 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
const int MAXN = 5e5+15;
const int INF = 1e9+10;
const int MOD = 1e9+7;

/*
dp[i] : max segment [1..i]
pref[i] : prefix sum [1..i]
opt[i] : min last segment untuk mendapatkan dp[i]

for (int i=1;i<=N;i++) {
    for (int j=i-1;j>=0;j--) {
        if (opt[j] <= pref[i]-pref[j]) {
            dp[i] = dp[j]+1;
            opt[i] = pref[i]-pref[j];
            break;
        }
    }
}

opt[j]+pref[j] <= pref[i]
*/
int n;
int a[MAXN], dp[MAXN], pr[MAXN], opt[MAXN];
deque <int> dq; // idx nya

signed main(){
    cin >> n;
    for(int i=1; i<=n; i++){ cin >> a[i]; pr[i] = pr[i-1]+a[i]; }
    int las = 0;
    dq.pb(0);
    for(int i=1; i<=n; i++){
        int las = -1;
        while(!dq.empty() && opt[dq.front()]+pr[dq.front()] <= pr[i]){
            las = dq.front(); dq.pop_front();
        }
        if(las != -1) dq.push_front(las);
        dp[i] = dp[las]+1;
        opt[i] = pr[i]-pr[las];
        while(!dq.empty() && opt[dq.back()]+pr[dq.back()] >= opt[i]+pr[i])
            dq.pop_back();
        dq.pb(i);
    }
    cout << dp[n] << '\n';
}

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

segments.cpp: In function 'int main()':
segments.cpp:42:9: warning: unused variable 'las' [-Wunused-variable]
   42 |     int las = 0;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...