Submission #825984

# Submission time Handle Problem Language Result Execution time Memory
825984 2023-08-15T09:42:15 Z vnm06 Wiring (IOI17_wiring) C++14
13 / 100
107 ms 15720 KB
#include<bits/stdc++.h>
#include "wiring.h"
using namespace std;

struct point
{
    long long ty;
    long long pos;
    point() {}
    point(long long a, long long b)
    {
        ty=a;
        pos=b;
    }
};

bool cmp(point p, point q)
{
    return p.pos<q.pos;
}

long long 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];
long long id[800005];

void update(long long v, long long le, long long ri, long long pos, long long st)
{
    if(ri<pos || le>pos) return;
    if(le==ri)
    {
        val[le]=st;
        id[v]=le;
    }
    else
    {
        long long 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];
    }
}

long long query(long long v, long long le, long long ri, long long be, long long en)
{
    if(ri<be || le>en) return -1;
    if(be<=le && ri<=en) return id[v];
    else
    {
        long long mid=(le+ri)/2;
        long long id1=query(2*v, le, mid, be, en);
        long long 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(long long i=0; i<r.size(); i++) t[i+1]=point(1, r[i]);
    for(long long i=0; i<b.size(); i++) t[i+r.size()+1]=point(0, b[i]);
    sort(t+1, t+n+1, cmp);
    long long idb=n+1, idr=n+1;
    for(long long i=1; i<=n; i++) pref[i]=pref[i-1]+t[i].pos;
    for(long long i=n; i>=1; i--)
    {
        long long 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;
        }
    }
    for(long long i=1; i<=n; i++) sp[i]+=sp[i-1];
    for(long long i=1; i<=n; i++) update(1, 0, n+2, i, 1e18);
    update(1, 0, n+2, 0, 0);
    update(1, 0, n+2, 1, 1e18);
    ans[1]=1e18;
    long long 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);
    }
    //cout<<n<<endl;
    for(long long j=ri1+1; j<=n; j++)
    {
        /////cout<<j<<" ";
        //cout<<j<<" c "<<le1<<" "<<ri1<<endl;
        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;
          ///  //cout<<j<<" "<<ans[j]<<" "<<ans[j]-sp[j]<<endl;
            update(1, 0, n+2, j, ans[j]-sp[j]);
        }
        else if(j-ri1==ri1-le1+1)
        {
            //cout<<j<<" tuka: ";
            ans[j]=min(ans[le1-1]+pref[j]-2*pref[ri1]+pref[le1-1], ans[le1]+pref[j]-2*pref[ri1]+pref[le1-1]);
            //cout<<j<<" "<<ans[j]<<" "<<ans[j]-sp[j]<<endl;
            //cout<<ans[le1-1]+pref[j]-2*pref[ri1]+pref[le1-1]<<" "<<ans[le1]+pref[j]-2*pref[ri1]+pref[le1-1]<<endl;
            update(1, 0, n+2, j, ans[j]-sp[j]);
        }
        else
        {
            ans[j]=ans[j-1]+t[j].pos-t[ri1].pos;
            long long tid=query(1, 0, n+2, le1-1, ri1-j+ri1);
            //cout<<ans[j]<<" "<<tid<<endl;
            long long calc2=ans[tid];
            //cout<<calc2<<" ";
            tid++;
            calc2+=t[ri1+1].pos*(ri1-tid-(j-(ri1+1)));
            //cout<<calc2<<" ";
            calc2+=pref[j]-2*pref[ri1]+pref[tid-1];
            //cout<<calc2<<endl;
            if(calc2<ans[j]) ans[j]=calc2;
            //cout<<j<<" "<<ans[j]<<" "<<ans[j]-sp[j]<<endl;
            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:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for(long long i=0; i<r.size(); i++) t[i+1]=point(1, r[i]);
      |                        ~^~~~~~~~~
wiring.cpp:70:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(long long i=0; i<b.size(); i++) t[i+r.size()+1]=point(0, b[i]);
      |                        ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '15280', found: '15342'
4 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 79 ms 12928 KB Output is correct
4 Correct 74 ms 12804 KB Output is correct
5 Correct 76 ms 12876 KB Output is correct
6 Correct 103 ms 15584 KB Output is correct
7 Correct 103 ms 15560 KB Output is correct
8 Correct 107 ms 15720 KB Output is correct
9 Correct 103 ms 15544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 312 KB 3rd lines differ - on the 1st token, expected: '17703', found: '18114'
3 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: '27', found: '28'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '15280', found: '15342'
4 Halted 0 ms 0 KB -