제출 #1339993

#제출 시각아이디문제언어결과실행 시간메모리
1339993notbrainingPotatoes and fertilizers (LMIO19_bulves)C++20
24 / 100
1095 ms4228 KiB
//https://oj.uz/problem/view/LMIO19_bulves
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#include <cassert>
#include<algorithm>
#include<queue>
#include <array>
#include <bitset>
#include<cstring>
using namespace std;
#define int long long
#define pii pair<int,int>
int INF = 1e18;
int a[500001];
int n;
void ez(){
    int cur = 0;
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        cur += a[i];
        ans += abs(cur);
    }
    cout << ans << "\n";
}
int dp[30001]; int dp2[30001];
const int lol = 1;
void onedp(){
    vector<int>psums(n + 1, 0);
    for(int i = 1; i <= n; ++i){
        psums[i] = psums[i - 1] + a[i];
    }
    //vector<vector<int>>dp(n + 2, vector<int>(2, INF)); //min cost to reach this state
    //state: from 1...i-1 is all 0, if j==1 then it has the total prefix sums here, otherwise, it has one less than the total prefix sums, (didn't transfer one)
    memset(dp, 0x3f, sizeof(dp));
    memset(dp2, 0x3f, sizeof(dp2));
    dp[0] = 0;
    for(int i = 1; i <= n; ++i){
        for(int j = 0; j <= lol; ++j){
            //if the psums is negative, you must transfer it all out (def not optimal to just drag it here)
            dp2[j] = min(dp2[j], dp[j] + abs(psums[i] - j));
            for(int keep = 0; keep <= lol - j; ++keep){
                dp2[keep + j] = min(dp2[j + keep], dp[j] + abs(psums[i] - j - keep));
            }
        }
        /*
                for(int j = 0; j <= 10; ++j){
            if(dp[j] >= INF){
                cout << "inf "; continue;
            }
            cout << dp[j] << " ";
        }
        cout << endl;
        */

        swap(dp, dp2);
        memset(dp2, 0x3f, sizeof(dp2));
    }
    int ans = INF;
    for(int i = 0; i < lol; ++i){
        ans = min(ans, dp[i]);
    }
    cout << ans << "\n";
}
int32_t main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    int sumA = 0;
    for(int i = 1; i <= n; ++i){
        int x, y; cin >> x >> y;
        a[i] = x - y;
        sumA += x - y;
    }
    if(sumA == 0){
        ez();
    } else{
        onedp();
    }
}
#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...