← Real problems

ccc02s4

easydppartitionreconstructStatement on DMOJ

CP Lang

19 lines0.3 spec
// ccc02s4 Bridge Crossing: people cross in order, at most m at a time, and a group takes as
// long as its slowest member. Split the line into groups to minimize the total.
let (m, n) = read<(1..=20, 1..=100)>()
domain P = 0..n
let people = read<[P](name: Str, t: 1..=1000)>()

fn cost(c: P.cuts, e: P.cuts) -> Int = people[c..e].map(p => p.t).max()! + total(e)
@memo(array)
fn total(c: P.cuts) -> Int:
    if c == P.cuts.last!:
        return 0
    (c.next..).take(m).map(e => cost(c, e)).min()!

print("Total Time:", total(P.cuts.first!))
var c = P.cuts.first!
while c != P.cuts.last!:
    let e = (c.next..).take(m).min(by: e => cost(c, e))!
    print(people[c..e].map(p => p.name))
    c = e

Accepted C++

70 lines · 0.01 s on the judge
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...) 1412
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

void solve() {
	int m, n;
	cin >> m >> n;
	string names[n];
	int arr[n];
	for (int i = 0; i < n; i++) {
		cin >> names[i] >> arr[i];
	}
	pair<int, int> dp[n + 1];
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = {1e9, 1e9};
		int cmx = 0;
		for (int j = i + 1; j <= min(n, i + m); j++) {
			cmx = max(cmx, arr[j - 1]);
			dp[i] = min(dp[i], {cmx + dp[j].first, j});
		}
	}
	cout << "Total Time: " << dp[0].first << endl;
	int i = 0;
	while (i < n) {
		int e = dp[i].second;
		for (; i < e; i++) {
			cout << names[i] << " \n"[i == e - 1];
		}
	}
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	int t = 1;
	// cin >> t;
	for (int _ = 1; _ <= t; _++) {
		dbg(_);
		// cout << "Case #" << _ << ": ";
		solve();
	}
}