Submission #223546

#TimeUsernameProblemLanguageResultExecution timeMemory
223546Ruxandra985Stove (JOI18_stove)C++14
100 / 100
46 ms2816 KiB
#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;

long long dp[DIMN];
int cnt[DIMN] , v[DIMN];
priority_queue <pair <long long , int> > h;
void solve (long long cost , int n){
    int i , mini = -v[1] , cmin = 0;

    for (i = 1 ; i <= n ; i++){
        dp[i] = cost + mini + v[i] + 1;
        cnt[i] = cmin + 1;

        if (dp[i] > cost + dp[i - 1] + 1){
            dp[i] = cost + dp[i - 1] + 1;
            cnt[i] = 1 + cnt[i - 1];
        }

        if (dp[i] - v[i + 1] < mini){
            mini = dp[i] - v[i + 1];
            cmin = cnt[i];
        }

    }


}
int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int n , k , i;
    long long st , dr , mid;
    fscanf (fin,"%d%d",&n,&k);
    for (i = 1 ; i <= n ; i++)
        fscanf (fin,"%d",&v[i]);

    st = 0;
    dr = 1000000000000000000LL;
    while (st <= dr){
        mid = (st + dr) / 2;
        solve(mid , n);
        if (cnt[n] > k)
            st = mid + 1;
        else
            dr = mid - 1;
    }
    //st = max(st - 1 , 0LL);
    solve(st , n);

    fprintf (fout,"%lld" , dp[n] - 1LL * k * st);

    return 0;
}

Compilation message (stderr)

stove.cpp: In function 'int main()':
stove.cpp:35:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d",&n,&k);
     ~~~~~~~^~~~~~~~~~~~~~~~~~
stove.cpp:37:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d",&v[i]);
         ~~~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...