Submission #93698

# Submission time Handle Problem Language Result Execution time Memory
93698 2019-01-10T17:54:24 Z someone_aa Wiring (IOI17_wiring) C++17
0 / 100
1000 ms 5996 KB
#include <bits/stdc++.h>
#include "wiring.h"
#define ll long long
#define mp make_pair
#define pb push_back

using namespace std;
const int maxn = 200100;
vector<pair<int, char> > v;
ll dp[maxn];
int n;

bool valid(int l, int r) {
    int max_r = INT_MIN, min_r = INT_MAX;
	int max_b = INT_MIN, min_b = INT_MAX;

	for(int i=l;i<=r;i++) {
        if(v[i].second == 'R') {
            max_r = max(max_r, v[i].first);
            min_r = min(min_r, v[i].first);
        }
        else {
            max_b = max(max_b, v[i].first);
            min_b = min(min_b, v[i].first);
        }
	}
	return (max_r < min_b) || (max_b < min_r);
}

ll cost(int l, int r) {
    int max_r = INT_MIN, min_r = INT_MAX;
	int max_b = INT_MIN, min_b = INT_MAX;

	ll rcnt = 0LL, bcnt = 0LL;

    for(int i=l;i<=r;i++) {
        if(v[i].second == 'R') {
            max_r = max(max_r, v[i].first);
            min_r = min(min_r, v[i].first);
            rcnt++;
        }
        else {
            max_b = max(max_b, v[i].first);
            min_b = min(min_b, v[i].first);
            bcnt++;
        }
	}

    ll sum = 0LL;
	if(min_r > max_b) {
        // B B B R R
        sum = (1LL * min_r - 1LL * max_b) * 1LL * max(bcnt, rcnt);
        for(int i=l;i<=r;i++) {
            if(v[i].second == 'R') {
                sum += v[i].first - min_r;
            }
            else {
                sum += max_b - v[i].first;
            }
        }
	}
	else {
        // R R R B B
        sum = (1LL * min_b - 1LL * max_r) * 1LL * max(bcnt, rcnt);
        for(int i=l;i<=r;i++) {
            if(v[i].second == 'R') {
                sum += max_r - v[i].first;
            }
            else {
                sum += v[i].first - min_b;
            }
        }
	}

	return sum;

}

long long min_total_length(std::vector<int> r, std::vector<int> b) {
	n = r.size() + b.size();
	for(int i:r) {
        v.pb(mp(i, 'R'));
	}
	for(int i:b) {
        v.pb(mp(i, 'B'));
	}
    v.pb(mp(-1, 'A'));
	sort(v.begin(), v.end());

	dp[0] = 0LL;
	for(int i=1;i<=n;i++) {
        dp[i] = (1LL << 45);
        for(int j=i-1;j>=1;j--) {
            if(valid(j, i)) {
                dp[i] = min(dp[i], dp[j-1] + cost(j, i));
            }
        }
    }
    return dp[n];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Incorrect 2 ms 504 KB 3rd lines differ - on the 1st token, expected: '14340', found: '14694'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 15 ms 376 KB Output is correct
3 Execution timed out 1079 ms 3948 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Execution timed out 1070 ms 5996 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Execution timed out 1082 ms 5356 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Incorrect 2 ms 504 KB 3rd lines differ - on the 1st token, expected: '14340', found: '14694'
3 Halted 0 ms 0 KB -