#ifdef atom
#include "grader.cpp"
#endif
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
vector<int> x;
vector<int> pong;
vector<int> prv;
vector< vector< ll > > pref;
vector< vector< ll > > dp;
vector< vector<int> > buck;
long long min_total_length(std::vector<int> r, std::vector<int> b)
{
int ir, ib; ir = ib = 0;
while(ir< r.size() && ib< b.size())
{
if(r[ir]< b[ib])
{
x.pb(r[ir]); pong.pb(0);
ir++;
}
else
{
x.pb(b[ib]); pong.pb(1);
ib++;
}
}
while(ir< r.size()) x.pb(r[ir++]), pong.pb(0);
while(ib< b.size()) x.pb(b[ib++]), pong.pb(1);
ll last = -1e18;
int n = pong.size();
for(int i = 0; i< n; i++)
{
if(i && pong[i] != pong[i-1]) last = x[i-1];
prv.pb(last);
}
pref.pb(vector<ll>());
buck.pb(vector<int>());
for(int i = 0; i< n; i++)
{
if(i && pong[i] != pong[i-1])
{
pref.pb(vector<ll>());
buck.pb(vector<int>());
}
pref.back().pb(pref.back().empty()?x[i]:(pref.back().back()+x[i]) );
buck.back().pb(x[i]);
}
dp.resize(pref.size());
int comps = pref.size();
for(int i = 0; i< (int) dp.size(); i++)
{
dp[i].resize(pref[i].size()+1);
}
for(int i = comps-1; i>= 0; i--)
{
if(i == comps-1) dp[i][pref[i].size()] = 0;
else dp[i][pref[i].size()] = dp[i+1][0];
//printf("%d\n", i);
for(int j = pref[i].size()-1; j>= 0; j--)
{
//printf("dp[%d][%d]\n", i, j);
int numleft = pref[i].size()-j;
//printf("numleft = %d ((%d))->(%d)\n", numleft, pref[i].size()-numleft-1, pref[i][pref[i].size()-1]);
ll sumhere = pref[i][pref[i].size()-1]-(((int)pref[i].size()-numleft-1>= 0)?pref[i][pref[i].size()-numleft-1]:0ll);
ll sumnext;
if(i+1< comps)
{
if(numleft-1< pref[i+1].size()) sumnext = pref[i+1][numleft-1];
else sumnext = pref[i+1].back() + pref[i+1][0]*(numleft-pref[i+1].size());
}
else sumnext = 1e18;
//printf("sumnext %lld sumhere %lld\n", sumnext, sumhere);
ll prev;
if(i) prev = buck[i-1].back();
else prev = -1e18;
dp[i][j] = min(i+1< comps?dp[i+1][min(numleft, (int)pref[i+1].size())]+sumnext-sumhere:(ll)1e18, dp[i][j+1]+buck[i][j]-prev);
//printf("dp[%d][%d] = %lld\n", i, j, dp[i][j]);
}
}
return dp[0][0];
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:22:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ir< r.size() && ib< b.size())
~~^~~~~~~~~~
wiring.cpp:22:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ir< r.size() && ib< b.size())
~~^~~~~~~~~~
wiring.cpp:35:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ir< r.size()) x.pb(r[ir++]), pong.pb(0);
~~^~~~~~~~~~
wiring.cpp:36:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ib< b.size()) x.pb(b[ib++]), pong.pb(1);
~~^~~~~~~~~~
wiring.cpp:76:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(numleft-1< pref[i+1].size()) sumnext = pref[i+1][numleft-1];
~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
436 KB |
3rd lines differ - on the 1st token, expected: '15280', found: '15398' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
436 KB |
Output is correct |
2 |
Correct |
2 ms |
444 KB |
Output is correct |
3 |
Correct |
33 ms |
6668 KB |
Output is correct |
4 |
Correct |
35 ms |
6688 KB |
Output is correct |
5 |
Correct |
34 ms |
7152 KB |
Output is correct |
6 |
Correct |
63 ms |
8604 KB |
Output is correct |
7 |
Correct |
53 ms |
8640 KB |
Output is correct |
8 |
Correct |
46 ms |
8700 KB |
Output is correct |
9 |
Correct |
48 ms |
8716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8716 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8716 KB |
3rd lines differ - on the 1st token, expected: '17703', found: '18722' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
8716 KB |
3rd lines differ - on the 1st token, expected: '27', found: '30' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
436 KB |
3rd lines differ - on the 1st token, expected: '15280', found: '15398' |
4 |
Halted |
0 ms |
0 KB |
- |