#include "wiring.h"
#include "bits/stdc++.h"
#define ff first
#define ss second
#define ll long long
using namespace std;
const ll B = 1e16;
void make(vector<ll>&v, int sz){
v.resize(sz);
for (int i = 0; i < sz; i++)
v[i] = B;
}
ll min_total_length(vector<int> r, vector<int> b) {
vector<pair<int,int> > v;
vector<vector<int> > d;
vector<int> sz;
for (auto x: r) v.push_back({x, 0});
for (auto x: b) v.push_back({x, 1});
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++){
int j = i;
vector<int> g{-1};
while (j < v.size() and v[i].ss == v[j].ss)
g.push_back(v[j++].ff);
d.push_back(g);
sz.push_back(j - i);
i = j - 1;
}
int n = d.size();
vector<ll> dp, odp;
vector<ll> pre, suf;
make(dp, sz[0]+1);
dp[0] = 0;
for (int i = 1; i < n; i++){
swap(odp, dp);
make(dp, sz[i]+1);
dp[0] = odp[sz[i-1]];
pre.resize(sz[i]+1);
suf.resize(sz[i-1]+2);
for (int j = 1; j <= sz[i]; j++)
pre[j] = pre[j-1] + d[i][j];
suf[sz[i-1]+1] = 0;
for (int j = sz[i-1]; j >= 1; j--)
suf[j] = suf[j+1] + d[i-1][j];
for (int j = 1; j <= sz[i]; j++){
int p = sz[i-1]+1-j;
if (p >= 1){
for (int k = 1; k < p; k++)
dp[j] = min(dp[j], pre[j]-j*1LL*d[i][1] +
min(odp[k-1], odp[k])+(d[i][1]-d[i-1][sz[i-1]])*1LL*(sz[i-1]-k+1)+d[i-1][sz[i-1]]*1LL*(sz[i-1]-k+1)-suf[k]);
for (int k = p; k <= sz[i-1]; k++)
dp[j] = min(dp[j], (d[i][1]-d[i-1][sz[i-1]])*1LL*j+pre[j]-j*1LL*d[i][1] +
min(odp[k-1], odp[k])+d[i-1][sz[i-1]]*1LL*(sz[i-1]-k+1) - suf[k]);
}
}
}
return dp[sz[n-1]];
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
wiring.cpp:23:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | while (j < v.size() and v[i].ss == v[j].ss)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '14340', found: '10000000000000000' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Execution timed out |
1079 ms |
5572 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Incorrect |
45 ms |
6960 KB |
3rd lines differ - on the 1st token, expected: '1068938599', found: '10000000000000000' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
4812 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '14340', found: '10000000000000000' |
3 |
Halted |
0 ms |
0 KB |
- |