This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 4e5;
const long long INF = 1e15;
struct wire {
int c, x;
};
struct segment {
int l, r;
};
long long sp[MAX_N + 1];
wire wires[MAX_N + 1];
vector<segment> seg;
vector<long long> dp[MAX_N + 1];
long long sum( int l, int r ) {
return sp[r] - sp[l - 1];
}
long long min_total_length( vector<int> a, vector<int> b ) {
int n = a.size() + b.size();
for ( int i = 0; i < a.size(); i++ )
wires[i + 1] = { 0, a[i] };
for ( int i = 0; i < b.size(); i++ )
wires[i + 1 + a.size()] = { 1, b[i] };
sort( wires + 1, wires + 1 + n, []( wire x, wire y ) {
return x.x < y.x;
} );
for ( int i = 1; i <= n; i++ )
sp[i] = sp[i - 1] + wires[i].x;
int i = 1;
while ( i <= n ) {
int j = i;
while ( j + 1 <= n && wires[i].c == wires[j + 1].c )
j++;
seg.push_back( { i, j } );
i = j + 1;
}
for ( int i = 0; i < seg.size(); i++ )
dp[i].resize( seg[i].r - seg[i].l + 2 );
for ( int x = 1; x <= seg[0].r - seg[0].l + 1; x++ )
dp[0][x] = INF;
for ( int i = 1; i < seg.size(); i++ ) {
for ( int x = 0; x < dp[i].size(); x++ ) {
dp[i][x] = INF;
for ( int y = 0; y < dp[i - 1].size(); y++ ) {
int z = seg[i - 1].r - seg[i - 1].l + 1 - y;
long long cost = -sum( seg[i - 1].l + y, seg[i - 1].r ) + sum( seg[i].l, seg[i].l + x - 1 );
if ( z < x )
cost -= (long long)wires[seg[i - 1].r].x * (x - z);
else
cost += (long long)wires[seg[i].l].x * (z - x);
dp[i][x] = min( dp[i][x], dp[i - 1][y] + cost );
}
}
}
/*for ( int i = 0; i < seg.size(); i++ ) {
for ( int x = 0; x < dp[i].size(); x++ )
printf( "%lld ", dp[i][x] );
printf( "\n" );
}*/
return dp[seg.size() - 1].back();
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:29:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for ( int i = 0; i < a.size(); i++ )
| ~~^~~~~~~~~~
wiring.cpp:31:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for ( int i = 0; i < b.size(); i++ )
| ~~^~~~~~~~~~
wiring.cpp:48:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<segment>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for ( int i = 0; i < seg.size(); i++ )
| ~~^~~~~~~~~~~~
wiring.cpp:53:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<segment>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for ( int i = 1; i < seg.size(); i++ ) {
| ~~^~~~~~~~~~~~
wiring.cpp:54:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for ( int x = 0; x < dp[i].size(); x++ ) {
| ~~^~~~~~~~~~~~~~
wiring.cpp:56:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for ( int y = 0; y < dp[i - 1].size(); y++ ) {
| ~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |