Submission #689703

#TimeUsernameProblemLanguageResultExecution timeMemory
689703true22Discharging (NOI20_discharging)C++14
0 / 100
3 ms596 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<ll, ll> pl;
typedef vector<ll> vl;   
typedef vector<pl> vp;

#define nl "\n"
#define fr first
#define sc second
#define pb push_back

#define all(x) x.begin(), x.end()
#define fur(i, a, b) for(ll i = a; i <= b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= b; --i)
#define pv(x) for(auto k : x){cout << k << " ";} cout << nl

const ll inf = 1e17;

struct dt{
    ll cost, li, wt;

    dt(){
        cost = li = wt = inf;
    }

    void print(){
        cout << cost << ' ' << li << ' ' << wt << nl;
    }

};

ll n;
vl t;

/*
    Idea:
    - The state:
        -  dp[i] = {cost, last index, waiting time};
    
    Terms:
    - cost = best minimum ans till i
    - last index = the beginning of the segment
    - waiting time = waiting time of the person i

    Base cases:
    - dp[1] = {t[1], 1, t[1]};

    Reccurence:
    dp[i].fr = min(dp[j].fr + cost(j + 1 to i), dp[i].fr)
    cost(j + 1 to i):
        (mx[j + 1, i] + dp[j].waiting) * (i - j)

*/

void solve(){
    vector<pl> dp;
    dp.resize(n + 1, {inf, inf});
    //dp[0] = {0, 0, 0};
    dp[0] = {0, 0};
    dp[1] = {t[1], t[1]};
   
    fur(i, 2, n){
       // cout << "i = " << i << nl;
        fur(j, 0, i - 1){       // account for this current seg begin the whole seg
         //   cout << "j = " << j << nl;
            ll mx = -1;
            fur(p, j + 1, i)
                mx = max(mx, t[p]);

            ll cost = (mx + dp[j].sc) * (i - j);
     //       cout << "cost = " << cost << nl;
            if (dp[j].fr + cost < dp[i].fr){
                dp[i].fr = dp[j].fr + cost;
                dp[i].sc = dp[j].sc + mx;
            }
        }
    }
    cout << dp[n].fr << nl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifndef ONLINE_JUDGE
        freopen("/home/compslab9/Desktop/Abhineet/cpp/input.txt", "r", stdin);
        freopen("/home/compslab9/Desktop/Abhineet/cpp/output.txt", "w", stdout);
    #endif

    cin >> n;
    t.resize(n + 1);
    fur(i, 1, n)
        cin >> t[i];

    solve();

    return 0;
}

Compilation message (stderr)

Discharging.cpp: In function 'int main()':
Discharging.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         freopen("/home/compslab9/Desktop/Abhineet/cpp/input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen("/home/compslab9/Desktop/Abhineet/cpp/output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...