Submission #62751

#TimeUsernameProblemLanguageResultExecution timeMemory
62751MatheusLealVWiring (IOI17_wiring)C++17
100 / 100
146 ms110508 KiB
#include <bits/stdc++.h>
#include "wiring.h"
#define inf 2000000000000000000LL
#define N 200005
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
 
typedef long long ll;
 
int n, m, group, grupo[N], head[N], last[N];
 
ll dp[N], sum[N];
 
pii v[N];
 
vector<int> split[N];
 
vector< pair<int, int> > all;
 
inline ll soma(int a, int b)
{
	return sum[max(a, b)] - sum[min(a, b) - 1];
}
 
inline ll custo(int i, int j)
{
	ll Bn = v[last[grupo[i]]].f, R1 = v[head[grupo[j]]].f;
 
	ll tam_1 = last[ grupo[i] ] - i + 1, tam_2 = j - head[ grupo[j] ] + 1;
 
	ll sum_1 = soma( i, last[grupo[i]] ), sum_2 = soma( head[grupo[j]], j);
 
	return tam_1*Bn - tam_2*R1 - sum_1 + sum_2 + max(tam_1, tam_2)*abs(Bn - R1);
}
 
inline ll get(int i, int j) {
	return min(dp[j], dp[j - 1]) + custo(min(i, j), max(i, j));
}
 
ll min_total_length(vector<int> e, vector<int> d)
{
	n = e.size(), m = d.size();
 
	for(int i = 0, x; i < e.size(); i++) all.push_back(make_pair(e[i], 0));
 
	for(int i = 0, x; i < d.size(); i++) all.push_back(make_pair(d[i], 1));
 
	sort(all.begin(), all.end());
 
	int ini = 0;
 
	for(int i = 0; i < all.size(); i++)
	{
		if( (i < n + m - 1 && all[i].s != all[i + 1].s) || i == n + m - 1)
		{
			group ++;
 
			for(int j = ini; j <= i; j++) grupo[j + 1] = group, split[group].push_back(all[j].f);
 
			last[group] = i + 1;
 
			head[group] = ini + 1;
 
			ini = i + 1;
		}
	}
 
	for(int i = 0; i < all.size(); i++) v[i + 1] = all[i], dp[i + 1] = inf;
 
	for(int i = 1; i <= n + m; i++) sum[i] = sum[i - 1] + v[i].f;
 
	int ptr = 1;
 
	for(int i = 1; i <= n + m; i++)
	{
		if(grupo[i] == 1) continue;
 
		if(grupo[i] == 2)
		{
			dp[i] = custo(1, i);
 
			continue;
		}
 
		if(v[i].s == v[i - 1].s)
		{
			dp[i] = get(i, ptr);
 
			while(ptr > 1 && get(i, ptr - 1) <= dp[i] && ptr - 1>= max(1, head[ grupo[i] - 1 ])) dp[i] = get(i, ptr - 1), ptr --;
		}
 
		else
		{
			ptr = last[grupo[i] - 1];
 
			dp[i] = get(i, ptr);
 
			while(ptr > 1 && get(i, ptr - 1) <= dp[i] && ptr - 1>= max(1, head[ grupo[i] - 1 ])) dp[i] = get(i, ptr - 1), ptr --;
		}
	}
 
	return dp[n + m];
}

Compilation message (stderr)

wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:46:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0, x; i < e.size(); i++) all.push_back(make_pair(e[i], 0));
                    ~~^~~~~~~~~~
wiring.cpp:46:17: warning: unused variable 'x' [-Wunused-variable]
  for(int i = 0, x; i < e.size(); i++) all.push_back(make_pair(e[i], 0));
                 ^
wiring.cpp:48:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0, x; i < d.size(); i++) all.push_back(make_pair(d[i], 1));
                    ~~^~~~~~~~~~
wiring.cpp:48:17: warning: unused variable 'x' [-Wunused-variable]
  for(int i = 0, x; i < d.size(); i++) all.push_back(make_pair(d[i], 1));
                 ^
wiring.cpp:54:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < all.size(); i++)
                 ~~^~~~~~~~~~~~
wiring.cpp:70:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < all.size(); i++) v[i + 1] = all[i], dp[i + 1] = inf;
                 ~~^~~~~~~~~~~~
#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...