# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
825906 |
2023-08-15T09:12:41 Z |
vnm06 |
Wiring (IOI17_wiring) |
C++14 |
|
70 ms |
13284 KB |
#include<bits/stdc++.h>
#include "wiring.h"
using namespace std;
struct point
{
int ty;
long long pos;
point() {}
point(int a, long long b)
{
ty=a;
pos=b;
}
};
bool cmp(point p, point q)
{
return p.pos<q.pos;
}
int n;
point t[200005];
long long ans[200005];
long long sp[200005];
long long pref[200005];
long long ri_r[200005], ri_b[200005];
long long val[200005];
int id[800005];
void update(int v, int le, int ri, int pos, long long st)
{
if(ri<pos || le>pos) return;
if(le==ri)
{
val[le]=st;
id[v]=le;
}
else
{
int mid=(le+ri)/2;
update(2*v, le, mid, pos, st);
update(2*v+1, mid+1, ri, pos, st);
if(val[id[2*v]]<val[id[2*v+1]]) id[v]=id[2*v];
else id[v]=id[2*v+1];
}
}
int query(int v, int le, int ri, int be, int en)
{
if(ri<be || le>en) return -1;
if(be<=le && ri<=en) return id[v];
else
{
int mid=(le+ri)/2;
int id1=query(2*v, le, mid, be, en);
int id2=query(2*v+1, mid+1, ri, be, en);
if(id1==-1) return id2;
if(id2==-1) return id1;
if(val[id1]<val[id2]) return id1;
return id2;
}
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n=r.size()+b.size();
for(int i=0; i<r.size(); i++) t[i+1]=point(1, r[i]);
for(int i=0; i<b.size(); i++) t[i+r.size()+1]=point(0, b[i]);
sort(t+1, t+n+1, cmp);
int idb=n+1, idr=n+1;
for(int i=1; i<=n; i++) pref[i]=pref[i-1]+t[i].pos;
for(int i=n; i>=1; i--)
{
int ty=t[i].ty;
if(ty==1)
{
if(idb==n+1) sp[i]=0;
else sp[i]=t[idb].pos-t[i].pos;
idr=i;
}
else
{
if(idr==n+1) sp[i]=0;
else sp[i]=t[idr].pos-t[i].pos;
idb=i;
}
}
update(1, 0, n+2, 0, 0);
update(1, 0, n+2, 1, 1e18);
ans[1]=1e18;
int le1=1, ri1=1;
while(ri1+1<=n && t[ri1+1].ty==t[ri1].ty)
{
ri1++;
ans[ri1]=1e18;
update(1, 0, n+2, ri1, 1e18);
}
for(int j=ri1+1; j<=n; j++)
{
///cout<<j<<" ";
if(t[j].ty!=t[j-1].ty && j!=ri1+1)
{
le1=ri1+1;
ri1=j-1;
}
if(j-ri1>ri1-le1+1)
{
ans[j]=ans[j-1]+t[j].pos-t[ri1].pos;
update(1, 0, n+2, j, ans[j]-sp[j]);
}
else if(j-ri1==ri1-le1+1)
{
ans[j]=min(ans[le1-1]+pref[j]-2*pref[ri1]+pref[le1-1], ans[le1]+pref[j]-2*pref[ri1]+pref[le1-1]);
update(1, 0, n+2, j, ans[j]-sp[j]);
}
else
{
ans[j]=ans[j-1]+t[j].pos-t[ri1].pos;
int tid=query(1, 0, n+2, le1-1, ri1-1);
/// cout<<tid<<endl;
long long calc2=ans[tid];
///cout<<calc2<<endl;
tid++;
calc2+=t[ri1+1].pos*(ri1-tid-(j-(ri1+1)));
calc2+=pref[j]-2*pref[ri1]+pref[tid-1];
/// cout<<calc2<<endl;
if(calc2<ans[j]) ans[j]=calc2;
update(1, 0, n+2, j, ans[j]-sp[j]);
}
/// cout<<ans[j]<<endl;
}
return ans[n];
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0; i<r.size(); i++) t[i+1]=point(1, r[i]);
| ~^~~~~~~~~
wiring.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0; i<b.size(); i++) t[i+r.size()+1]=point(0, b[i]);
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '30413' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
46 ms |
10504 KB |
Output is correct |
4 |
Correct |
46 ms |
10516 KB |
Output is correct |
5 |
Correct |
46 ms |
10500 KB |
Output is correct |
6 |
Correct |
70 ms |
13232 KB |
Output is correct |
7 |
Correct |
63 ms |
13260 KB |
Output is correct |
8 |
Correct |
67 ms |
13284 KB |
Output is correct |
9 |
Correct |
67 ms |
13232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '17703', found: '18366' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '27', found: '33' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '30413' |
2 |
Halted |
0 ms |
0 KB |
- |