tvvtdddlbvdlbdns's blog

By tvvtdddlbvdlbdns, history, 12 months ago, In English

Problem – Pagination

You are given a text consisting of n words, numbered from 1 to n. The i-th word has length a[i].

We want to arrange these words into lines and then into pages.

Each line has maximum length L. A line consists of one or more consecutive words. The total length of all words on a line must not exceed L. Each page can contain at most k lines. The words must appear in order; you cannot reorder them.

First, you split the sequence of words into lines. Then, you split the sequence of lines into pages, with up to k lines per page.

There is one strict rule: Among all possible ways to paginate the text, you must choose one that uses the minimum possible number of pages.

Now we define the “penalty”:

For a line whose words have total length s, its line penalty is L — s. For a page, its page penalty is the maximum line penalty among all lines on that page. The total penalty of a pagination is the sum of the penalties of all pages.

Your task: Among all paginations that use the minimum possible number of pages, find the minimal possible total penalty.

Input

The first line contains three positive integers n, L, k: - n – number of words - L – maximum length of a line - k – maximum number of lines per page

Each of the next n lines contains a positive integer a[i] – the length of the i-th word.

Output

Print a single integer: the minimal total penalty of a pagination that uses the minimum possible number of pages.

Example

Input

 6 10 3 4 5 9 7 8 6 

Output

 5 
That's the problem. My teacher solved with dp[i][j][k]: ith word, jth line, k penalty point. I don't understand my teacher's solution, but he said: "The case that one word place in line i worst than if it place in line i + 1 will make my solution wrong", Can someone solve this problem and explain it for me, because i dont understand him and he also say his solution has some counter. Thanks Sorry for my bad English!
  • Vote: I like it
  • +3
  • Vote: I do not like it

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

AC code for whoever need it:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define NAME ""
#define plii pair<ll, pair<int, int>>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define lb lower_bound
#define ub upper_bound
const ll MOD = 1e9 + 7;
const ll base = 311;
const int MAX6 = 1e6 + 7;
const int MAX5 = 2e5 + 7;
const int MAX3 = 1e3 + 7;
const int intINF = 1e9;
const ll llINF = 1e18;

const int MAXN = 5000 + 7;
const int MAXP = 30 + 7;
const int MAXL = 70 + 7;

int n, lineLen, maxLines;
int wLen[MAXN];
ll pref[MAXN];

const ll BIG = (ll)1e13;
ll dp[MAXN][MAXP][MAXL];

void Input() {
    cin >> n >> lineLen >> maxLines;
    for (int i = 1; i <= n; i++) {
        cin >> wLen[i];
        pref[i] = pref[i - 1] + wLen[i];
    }
}

void Preprocess() {

}

void Solve() {
    for (int i = 0; i <= n; i++) {
        for (int t = 0; t <= maxLines; t++) {
            for (int mx = 0; mx <= lineLen; mx++) {
                dp[i][t][mx] = llINF;
            }
        }
    }
    dp[0][0][0] = 0;

    for (int i = 0; i < n; i++) {
        for (int t = 0; t <= maxLines; t++) {
            for (int mx = 0; mx <= lineLen; mx++) {
                ll cur = dp[i][t][mx];
                if (cur == llINF) continue;

                for (int j = i + 1; j <= n; j++) {
                    ll sumLen = pref[j] - pref[i];
                    if (sumLen > lineLen) break;

                    int slack = (int)(lineLen - sumLen);

                    if (t == 0) {
                        ll &refA = dp[j][1][slack];
                        if (cur < refA) refA = cur;
                    } else {
                        if (t + 1 <= maxLines) {
                            int newMx = mx;
                            if (slack > newMx) newMx = slack;
                            ll &refA = dp[j][t + 1][newMx];
                            if (cur < refA) refA = cur;
                        }
                    }

                    if (t > 0) {
                        ll newCost = cur + BIG + mx;
                        ll &refB = dp[j][1][slack];
                        if (newCost < refB) refB = newCost;
                    }
                }
            }
        }
    }

    ll best = llINF;
    for (int t = 0; t <= maxLines; t++) {
        for (int mx = 0; mx <= lineLen; mx++) {
            ll cur = dp[n][t][mx];
            if (cur == llINF) continue;
            ll val = cur;
            if (t > 0) {
                val += BIG + mx;
            }
            if (val < best) best = val;
        }
    }

    ll answerPenalty = best % BIG;
    cout << answerPenalty;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(NAME".INP", "r")) {
        freopen(NAME".INP", "r", stdin);
        freopen(NAME".OUT", "w", stdout);
    }

    Input();
    Preprocess();
    Solve();
    return 0;
}