Submission #114787

# Submission time Handle Problem Language Result Execution time Memory
114787 2019-06-02T16:58:40 Z faustaadp Wiring (IOI17_wiring) C++17
38 / 100
231 ms 11128 KB
#include "wiring.h"
#include<bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll n,i,te,d[202020];
pair<ll,ll> a[202020];
ll com(ll aa,ll bb)
{
	ll ii;
	vector<ll> x,y;
	for(ii=aa;ii<=bb;ii++)
		if(a[ii].se==a[aa].se)
			x.pb(a[ii].fi);
		else
			y.pb(a[ii].fi);
	ll H=0;
	reverse(x.begin(),x.end());
	if(x.size()<y.size())
	{
		for(ii=0;ii<x.size();ii++)
			H+=abs(x[ii]-y[ii]);
		for(ii=x.size();ii<y.size();ii++)
			H+=abs(x[0]-y[ii]);
	}
	else
	{
		for(ii=0;ii<y.size();ii++)
			H+=abs(x[ii]-y[ii]);
		for(ii=y.size();ii<x.size();ii++)
			H+=abs(y[0]-x[ii]);
	}
	//cout<<aa<<" "<<bb<<" "<<H<<"\n";
	return H;
}
ll depe(ll aa)
{
	if(aa==n+1)
		return 0;
	if(d[aa]==-1)
	{
		d[aa]=1e18;
		ll ii,udh=0;
		vector<ll> z;
		for(ii=aa;ii<=n;ii++)
		{
			if(ii>aa&&a[ii].se!=a[ii-1].se)
				udh=ii;
			if(udh&&a[ii].se==a[aa].se)
				break;
			if(udh)
			{
				z.pb(ii);
				//if((udh-ii)==(ii+1-udh))
				//	d[aa]=min(d[aa],com(aa,ii)+min(depe(ii+1),depe(ii)));
			}
		}
		ll zs=z.size();
		ll ki=0;
		if(zs)
			ki=a[z[0]-1].fi;
		ll ka=0;
		if(zs)
			ka=a[z[zs-1]+1].fi;
		//return d[aa];
		//cout<<ki<<" "<<ka<<"\n";
		if(ki<ka&&zs)
		{			
			ll L=0,R=zs-1,C,ten=0;
			while(L<=R)
			{
				C=(L+R)/2;
				if(abs(a[z[C]].fi-ki)<abs(a[z[C]].fi-ka))
				{
					ten=C;
					L=C+1;
				}
				else
					R=C-1;
			}
		//	cout<<ten<<"\n";
			//return d[aa];
			if(zs==1)
			{
				for(ii=0;ii<zs;ii++)
					d[aa]=min(d[aa],com(aa,z[ii])+min(depe(z[ii]+1),depe(z[ii])));
			}
			else if(zs)
			{
			//	cout<<zs<<" "<<zs/2-1<<" "<<zs/2-1+(zs%2==1)<<"\n";
				for(ii=max(0LL,ten-1);ii<=min(zs-1,ten+1);ii++)
					d[aa]=min(d[aa],com(aa,z[ii])+min(depe(z[ii]+1),depe(z[ii])));
			}
		}
		if(zs)
			for(ii=zs-1;ii<=zs-1;ii++)
				d[aa]=min(d[aa],com(aa,z[ii])+min(depe(z[ii]+1),depe(z[ii])));
	}
	return d[aa];
}
long long min_total_length(std::vector<int> r, std::vector<int> b) 
{
	n=r.size()+b.size();
	for(i=0;i<r.size();i++)
		a[++te]=mp(r[i],0);
	for(i=0;i<b.size();i++)
		a[++te]=mp(b[i],1);
	if(r[r.size()-1]<b[0])
		return com(1,n);
	sort(a+1,a+1+te);
	memset(d,-1,sizeof(d));
	return depe(1);
}

Compilation message

wiring.cpp: In function 'll com(ll, ll)':
wiring.cpp:24:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ii=0;ii<x.size();ii++)
            ~~^~~~~~~~~
wiring.cpp:26:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ii=x.size();ii<y.size();ii++)
                   ~~^~~~~~~~~
wiring.cpp:31:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ii=0;ii<y.size();ii++)
            ~~^~~~~~~~~
wiring.cpp:33:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ii=y.size();ii<x.size();ii++)
                   ~~^~~~~~~~~
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:107:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0;i<r.size();i++)
          ~^~~~~~~~~
wiring.cpp:109:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0;i<b.size();i++)
          ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1920 KB Output is correct
2 Incorrect 3 ms 1920 KB 3rd lines differ - on the 1st token, expected: '14340', found: '14554'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 29 ms 5620 KB Output is correct
4 Correct 29 ms 5612 KB Output is correct
5 Correct 28 ms 5368 KB Output is correct
6 Correct 35 ms 7328 KB Output is correct
7 Correct 36 ms 7384 KB Output is correct
8 Correct 36 ms 7404 KB Output is correct
9 Correct 35 ms 7412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Incorrect 4 ms 1920 KB 3rd lines differ - on the 1st token, expected: '17703', found: '17773'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1920 KB Output is correct
2 Correct 79 ms 8408 KB Output is correct
3 Correct 227 ms 11128 KB Output is correct
4 Correct 87 ms 8968 KB Output is correct
5 Correct 231 ms 11128 KB Output is correct
6 Correct 3 ms 1920 KB Output is correct
7 Correct 3 ms 1920 KB Output is correct
8 Correct 3 ms 1920 KB Output is correct
9 Correct 3 ms 1920 KB Output is correct
10 Correct 4 ms 1920 KB Output is correct
11 Correct 3 ms 1920 KB Output is correct
12 Correct 3 ms 1920 KB Output is correct
13 Correct 4 ms 1920 KB Output is correct
14 Correct 4 ms 1920 KB Output is correct
15 Correct 4 ms 1920 KB Output is correct
16 Correct 12 ms 1920 KB Output is correct
17 Correct 4 ms 1920 KB Output is correct
18 Correct 65 ms 8736 KB Output is correct
19 Correct 81 ms 8440 KB Output is correct
20 Correct 72 ms 8828 KB Output is correct
21 Correct 70 ms 8732 KB Output is correct
22 Correct 56 ms 9432 KB Output is correct
23 Correct 45 ms 8884 KB Output is correct
24 Correct 53 ms 9280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1920 KB Output is correct
2 Incorrect 3 ms 1920 KB 3rd lines differ - on the 1st token, expected: '14340', found: '14554'
3 Halted 0 ms 0 KB -