Submission #494581

# Submission time Handle Problem Language Result Execution time Memory
494581 2021-12-15T18:52:48 Z aris12345678 Stove (JOI18_stove) C++14
0 / 100
0 ms 204 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int mxN = 100005;
int a[mxN];

bool check(ll md, int n, int k) {
    int cnt = 0, start = 0;
    for(int i = 0; i < n; i++) {
        if(a[i]-a[start] > md) {
            start = i;
            cnt++;
        }
    }
    if(start != n)
        cnt++;
    return cnt <= k;
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    ll st = 0, en = LLONG_MAX, md, diff;
    while(st <= en) {
        md = (st+en)/2;
        if(check(md, n, k))
            diff = md, en = md-1;
        else
            st = md+1;
    }
    // cout << diff << "\n";
    int start = 0;
    ll ans = 0;
    for(int i = 0; i < n; i++) {
        if(a[i]-a[start] > diff) {
            ans += 1LL*(a[i-1]-a[start]+1);
            start = i;
        }
    }
    if(start != n)
        ans += 1LL*(a[n-1]-a[start]+1);
    printf("%lld\n", ans);
    return 0;
}

Compilation message

stove.cpp: In function 'int main()':
stove.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
stove.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
stove.cpp:39:9: warning: 'diff' may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |         if(a[i]-a[start] > diff) {
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -