#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N ((int)301*1000)
#define INF ((ll)1e16)
ll n,dp[N];
vector <pair<int,bool> > a;
ll seg[4*N],lazy[4*N];
void shift(int id)
{
seg[id*2]+=lazy[id];
seg[id*2+1]+=lazy[id];
lazy[id*2]+=lazy[id];
lazy[id*2+1]+=lazy[id];
lazy[id]=0;
}
void update(int ql,int qr,ll val,int xl=0,int xr=n,int id=1)
{
if(qr<=xl || xr<=ql)return ;
if(ql<=xl && xr<=qr)
{
seg[id]+=val;
lazy[id]+=val;
return ;
}
int mid=(xl+xr)/2;
shift(id);
update(ql,qr,val,xl,mid,id*2);
update(ql,qr,val,mid,xr,id*2+1);
seg[id]=min(seg[id*2],seg[id*2+1]);
}
ll query(int ql,int qr,int xl=0,int xr=n,int id=1)
{
if(qr<=xl || xr<=ql)return INF;
if(ql<=xl && xr<=qr)return seg[id];
int mid=(xl+xr)/2;
shift(id);
return min(query(ql,qr,xl,mid,id*2),query(ql,qr,mid,xr,id*2+1));
}
ll min_total_length(vector<int> r,vector<int> b)
{
for(auto u:r)a.push_back({u,0});
for(auto u:b)a.push_back({u,1});
sort(a.begin(),a.end());
n=a.size();
int i=0;
while(i<n && a[i].second==a[0].second)i++;
int l=0,l2=i;
for(ll j=i-1,sum=0;j>=0;j--)
{
sum+=a[i-1].first-a[j].first+(a[i].first-a[i-1].first);
update(j,j+1,sum+((j>0)?INF:0));
dp[j]=INF;
}
dp[i]=query(l,l2);i++;
//cout<<i-1<<" "<<dp[i-1]<<"\n";
for(;i<n;i++)
{
if(a[i].second==a[l2].second)
{
update(max(l,l2-(i-l2)),l2,a[l2].first-a[l2-1].first);
// cout<<query(l,l2)<<"\n";
update(l,l2,a[i].first-a[l2].first);
dp[i]=query(l,l2);
// cout<<i<<" "<<dp[i]<<"\n";
continue;
}
l=l2;l2=i;
for(ll j=i-1,sum=0;j>=l;j--)
{
sum+=a[i-1].first-a[j].first+(a[i].first-a[i-1].first);
update(j,j+1,sum+dp[j-1]);
}
dp[i]=query(l,l2);
// cout<<i<<" "<<dp[i]<<"\n";
}
return dp[n-1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
484 KB |
3rd lines differ - on the 1st token, expected: '14340', found: '14694' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
484 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
113 ms |
9872 KB |
Output is correct |
4 |
Correct |
107 ms |
9872 KB |
Output is correct |
5 |
Correct |
113 ms |
9872 KB |
Output is correct |
6 |
Correct |
173 ms |
9872 KB |
Output is correct |
7 |
Correct |
187 ms |
9872 KB |
Output is correct |
8 |
Correct |
107 ms |
9872 KB |
Output is correct |
9 |
Correct |
128 ms |
9872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9872 KB |
Output is correct |
2 |
Correct |
2 ms |
9872 KB |
Output is correct |
3 |
Incorrect |
253 ms |
13656 KB |
3rd lines differ - on the 1st token, expected: '1068938599', found: '1152497305' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13656 KB |
Output is correct |
2 |
Correct |
335 ms |
13656 KB |
Output is correct |
3 |
Correct |
249 ms |
14948 KB |
Output is correct |
4 |
Correct |
352 ms |
16064 KB |
Output is correct |
5 |
Correct |
260 ms |
17432 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17432 KB |
3rd lines differ - on the 1st token, expected: '42', found: '43' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
484 KB |
3rd lines differ - on the 1st token, expected: '14340', found: '14694' |
3 |
Halted |
0 ms |
0 KB |
- |