Submission #468119

#TimeUsernameProblemLanguageResultExecution timeMemory
468119PedroBigManWiring (IOI17_wiring)C++14
100 / 100
55 ms23612 KiB
#include "wiring.h"
/*
Author of all code: Pedro BIGMAN Dias
Last edit: 15/02/2021
*/
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 5000000000000000LL
#define EPS 0.00000001
#define pi 3.14159
ll mod=1000000007LL;

template<class A=ll> 
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} 

vector<ll> dp;
vector<ll> ps;

ll f(ll s)
{
	if(s==-1LL) {return 0LL;}
	else {return dp[s];}
}

ll PS(ll a, ll b)
{
	if(a==0) {return ps[b];} else {return (ps[b]-ps[a-1]);}
}

ll min_total_length(vector<int> r, vector<int> b) 
{
	vector<pl> a;
	ll cntA=0LL, cntB=0LL;
	while(cntA<r.size() || cntB<b.size())
	{
		if(cntA==r.size()) {a.pb({b[cntB],1}); cntB++;}
		else if(cntB==b.size()) {a.pb({r[cntA],0}); cntA++;}
		else if(b[cntB]>r[cntA]) {a.pb({r[cntA],0}); cntA++;}
		else {a.pb({b[cntB],1}); cntB++;}
	}
	ll N = a.size(); REP(i,0,N) {dp.pb(INF);}
	ll cursum=0LL; REP(i,0,N) {cursum+=a[i].ff; ps.pb(cursum);}
	vector<pl> lastblock; pl cur={-1,-1};
	REP(i,0,N)
	{
		if(i>0 && a[i].ss!=a[i-1].ss) 
		{
			cur={lastblock.back().ss+1,i-1};
		}
		lastblock.pb(cur);
	}
	ll len, len1, len2;
	ll A,B,C;
	vector<ll> val1,val2; REP(i,0,N) {val1.pb(INF); val2.pb(INF);}
	vector<ll> minright1, minleft2; REP(i,0,N) {minright1.pb(INF); minleft2.pb(INF);}
	ll curmin1,curmin2;
	REP(i,0,N)
	{
		A = lastblock[i].ff; B = lastblock[i].ss+1; C = i;
		if(A==B-1LL && A>=0LL)
		{
			dp[i]=min(dp[i],min(f(A),f(A-1))+PS(B,C)-(C-A)*a[A].ff);
		}
		else if(A>=0LL)
		{
			len1=INF; len2=INF;
			ll pos1 = max(A,2LL*B-C-1LL); ll pos2=min(B-1,pos1-1LL);
			if(pos1<B) {len1=minright1[pos1];}
			if(pos2>=A) {len2=minleft2[pos2];}
			dp[i]=min(dp[i],len1 + PS(B,C) - (C+1LL-2LL*B)*a[B-1].ff);
			dp[i]=min(dp[i],len2 + PS(B,C) - (C+1LL-2LL*B)*a[B].ff);
		}
		if(i!=N-1 && a[i+1].ss!=a[i].ss)
		{
			A = lastblock[i+1].ff; B = lastblock[i+1].ss +1;
			curmin1=INF; curmin2=INF;
			REP(x,A,B)
			{
				val1[x]= f(x-1) - PS(x,B-1) - x*a[B-1].ff;
				val2[x]= f(x-1) - PS(x,B-1) - x*a[B].ff;
			}
			REP(x,A,B)
			{
				curmin2=min(curmin2,val2[x]); minleft2[x]=curmin2;
			}
			for(ll x = B-1; x>=A; x--) 
			{
				curmin1=min(curmin1,val1[x]); minright1[x]=curmin1;
			}
		}
	}
	return dp[N-1];
}

Compilation message (stderr)

wiring.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O3")
      | 
wiring.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("unroll-loops")
      | 
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:66:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |  while(cntA<r.size() || cntB<b.size())
      |        ~~~~^~~~~~~~~
wiring.cpp:66:29: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |  while(cntA<r.size() || cntB<b.size())
      |                         ~~~~^~~~~~~~~
wiring.cpp:68:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |   if(cntA==r.size()) {a.pb({b[cntB],1}); cntB++;}
      |      ~~~~^~~~~~~~~~
wiring.cpp:69:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |   else if(cntB==b.size()) {a.pb({r[cntA],0}); cntA++;}
      |           ~~~~^~~~~~~~~~
wiring.cpp:84:5: warning: unused variable 'len' [-Wunused-variable]
   84 |  ll len, len1, len2;
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...