답안 #37924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
37924 2017-12-29T04:03:40 Z Just_Solve_The_Problem K개의 묶음 (IZhO14_blocks) C++11
0 / 100
1000 ms 60224 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll N = (ll)1e5 + 7;
const ll linf = (ll)1e15 + 7;

ll dp[2][N];
ll ar[N];
ll numlog[N];
ll table[20][N];
ll n;
int back[101][N];

void buildtable () {
    for (ll i = 2; i <= n; i++) {
        numlog[i] = numlog[i / 2] + 1;
    }
    for (ll i = 0; i <= numlog[n]; i++) {
        ll curlen = 1 << i;
        for (ll j = 1; j <= n; j++) {
            if (i == 0) {
                table[i][j] = ar[j];
                continue;
            }
            table[i][j] = max(table[i - 1][j], table[i - 1][j + curlen / 2]);
        }
    }
}

ll get (ll l, ll r) {
    if (l > r) return linf;
    ll curlog = numlog[r - l + 1];
    return max(table[curlog][l], table[curlog][r - (1 << curlog) + 1]);
}

void div (ll k, ll l, ll r, ll optl, ll optr) {
    if (l > r) return ;
    ll mid = (l + r) >> 1;
    ll opt = optl;
    dp[k & 1][mid] = linf;
    for (ll i = optl; i <= mid; i++) {
        ll cur = dp[k & 1 ^ 1][i] + get(i + 1, mid);
        if (cur < dp[k & 1][mid]) {
            dp[k & 1][mid] = cur;
            opt = i;
        }
    }
    back[k][mid] = opt;
    div(k, l, mid - 1, optl, opt);
    div(k, mid + 1, r, opt, optr);
}

main() {
    ll k;
    scanf ("%lld %lld", &n, &k);
    for (ll i = 1; i <= n; i++) {
        scanf ("%lld", ar + i);
    }
    buildtable();
    for (ll i = 0; i <= n; i++) {
        dp[0][i] = linf;
    }
    for (ll i = 1; i <= n; i++) {
        dp[1][i] = get(1, i);
    }
//    cout << get(1, n) << endl;
//    for (ll i = 1; i <= n; i++) {
//        cout << dp[1][i] << ' ';
//    }
//    puts("");
    for (ll i = 2; i <= k; i++) {
        div(i, 1, n, 1, n - 1);
//        for (ll j = 1; j <= n; j++) {
//            cout << dp[i & 1][j] << ' ';
//        }
//        puts("");
    }
    cout << dp[k & 1][n] << endl;
//    vector < int > vec;
//    while (n > 0 && k > 0 && back[k][n] > 0) {
//        vec.push_back(back[k][n]);
//        n = back[k][n];
//        k--;
//    }
//    reverse(vec.begin(), vec.end());
//    for (int to : vec) {
//        cout << to << ' ';
//    }
}

Compilation message

blocks.cpp: In function 'void div(long long int, long long int, long long int, long long int, long long int)':
blocks.cpp:45:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
         ll cur = dp[k & 1 ^ 1][i] + get(i + 1, mid);
                       ^
blocks.cpp: At global scope:
blocks.cpp:56:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
blocks.cpp: In function 'int main()':
blocks.cpp:58:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%lld %lld", &n, &k);
                                ^
blocks.cpp:60:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%lld", ar + i);
                               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 60224 KB Output is correct
2 Correct 0 ms 60224 KB Output is correct
3 Correct 0 ms 60224 KB Output is correct
4 Correct 0 ms 60224 KB Output is correct
5 Correct 0 ms 60224 KB Output is correct
6 Correct 0 ms 60224 KB Output is correct
7 Correct 0 ms 60224 KB Output is correct
8 Correct 0 ms 60224 KB Output is correct
9 Correct 0 ms 60224 KB Output is correct
10 Correct 0 ms 60224 KB Output is correct
11 Incorrect 0 ms 60224 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 60224 KB Output is correct
2 Correct 0 ms 60224 KB Output is correct
3 Correct 0 ms 60224 KB Output is correct
4 Correct 0 ms 60224 KB Output is correct
5 Correct 0 ms 60224 KB Output is correct
6 Correct 0 ms 60224 KB Output is correct
7 Correct 0 ms 60224 KB Output is correct
8 Correct 0 ms 60224 KB Output is correct
9 Correct 0 ms 60224 KB Output is correct
10 Correct 0 ms 60224 KB Output is correct
11 Incorrect 0 ms 60224 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 60224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1000 ms 60224 KB Execution timed out
2 Halted 0 ms 0 KB -