답안 #699068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699068 2023-02-15T14:03:40 Z Ran Stove (JOI18_stove) C++17
0 / 100
2 ms 340 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 100;

long long n, k, a[N], sum[N], c[N];

long long dp[5020][5020];

void sub2()
{
    sort(a + 1, a + 1 + n);
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++)
        dp[i][0] = 1e18;
    for (int j = 1; j <= k; j++)
    {
        long long ok = a[1] - dp[0][j - 1];
        for (int i = 1; i <= n; i++)
        {
            dp[i][j] = (a[i] + 1) - ok;
            ok = max(ok, a[i + 1] - dp[i][j - 1]);
        }
    }
    cout << dp[n][k];
}

void sub()
{
    sort(a + 1, a + 1 + n);
    int m = 0;
    for (int i = 1; i <= n; i++)
        if (m == 0 || c[m] != a[i])
        {
            m++;
            c[m] = a[i];
        }
    vector <long long> b;
    for (int i = 2; i <= m; i++)
        b.push_back(c[i] - c[i - 1] - 1);
    sort(b.begin(), b.end());
    long long ans = a[n] + 1 - a[1];
    for (int i = b.size() - 1; i >= 0; i--)
    {
        if (k == 1)
            break;
        ans = ans - b[i];
        k--;
    }
    cout << ans;
}

int main()
{
    freopen("Stove.inp", "r", stdin);
    freopen("Stove.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    if (n <= 5000)
        sub2();
    else
        sub();
    return 0;
}

Compilation message

stove.cpp: In function 'int main()':
stove.cpp:56:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     freopen("Stove.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:57:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     freopen("Stove.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -