답안 #970275

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
970275 2024-04-26T10:06:02 Z CSQ31 전선 연결 (IOI17_wiring) C++17
0 / 100
51 ms 13016 KB
#include "wiring.h"
#include <bits/stdc++.h>
#define sz(a) a.size()
using namespace std;
typedef long long int ll;
const ll INF = 1e16;
const int MAXN = 2e5+5;
ll pre[MAXN],suff[MAXN]; //pre and suff min
ll min_total_length(vector<int> R,vector<int> B) {
	vector<array<int,2>>a;
	for(int x:R)a.push_back({x,0});
	for(int x:B)a.push_back({x,1});
	sort(a.begin(),a.end());
	vector<vector<int>>b;

	int n = sz(a);
	for(int i=0;i<n;i++){
		int j = i;
		b.push_back({a[i][0]});
		while(j+1<n && a[j][1] == a[j+1][1]){
			j++;
			b.back().push_back(a[j][0]);
		}
		i = j;
	}

	vector<int>s;
	for(auto x:b)s.push_back(sz(x));
	for(int i=0;i<=n;i++)suff[i] = pre[i] = INF;
	pre[s[0]] = suff[s[0]] = 0;

	for(int i=0;i<s[0];i++){
		pre[s[0]] += b[0].back() - b[0][i];
		suff[s[0]] += b[0].back() - b[0][i];
	}
	//compute prefix and suffix min
	suff[s[0]] += 1LL * s[0] * (b[1][0] - b[0].back()); //distance in between
	for(int i=1;i<=s[1];i++)pre[i] = min(pre[i],pre[i-1]);
	for(int i=s[1]-1;i>=0;i--)suff[i] = min(suff[i],suff[i+1]);

    /*
	for(auto x:b){
		for(int y:x)cout<<y<<" ";
		cout<<'\n';
	}*/
	for(int i=1;i<sz(s);i++){
		vector<ll>tmp(s[i]+1,0);
		//tmp[i] = min cost if i guys from this batch is assigned to the left
		ll cur = 0;
		for(int j=0;j<=s[i];j++){
			if(j)cur += b[i][j-1] - b[i][0];
			ll x = pre[j] + 1LL * j * (b[i][0] - b[i-1].back()); //distance in between
			ll y = suff[j];

			tmp[j] = cur + min(x,y);
		}
		cur = 0;
		reverse(tmp.begin(),tmp.end());
		for(int j=0;j<=s[i];j++){
			if(j)cur += b[i].back() - b[i][s[i]-j];
			tmp[j] += cur;
		}
		/*
		cout<<"at "<<i<<'\n';
		for(ll x:tmp)cout<<x<<" ";
		cout<<'\n';*/
		//tmp[i] = min cost if i guys assigned to the right
		if(i == sz(s)-1)return tmp[0];

		for(int j=0;j<=s[i+1];j++)pre[j] = suff[j] = INF;
		for(int j=0;j<=s[i];j++){
			pre[j] = suff[j] = tmp[j];
			suff[j] += 1LL * j * (b[i+1][0] - b[i].back());
		}
		for(int j=1;j<=s[i+1];j++)pre[j] = min(pre[j],pre[j-1]);
		for(int j=s[i+1]-1;j>=0;j--)suff[j] = min(suff[j],suff[j+1]);
	}
	return 0;
	
}

Compilation message

wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i=1;i<sz(s);i++){
      |               ^
wiring.cpp:68:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |   if(i == sz(s)-1)return tmp[0];
      |      ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2552 KB Output is correct
2 Incorrect 1 ms 2904 KB 3rd lines differ - on the 1st token, expected: '14340', found: '14522'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB 3rd lines differ - on the 1st token, expected: '904', found: '10000000000000281'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Incorrect 51 ms 13016 KB 3rd lines differ - on the 1st token, expected: '1068938599', found: '10000000000016523'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Incorrect 36 ms 8820 KB 3rd lines differ - on the 1st token, expected: '373710605', found: '409801698'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2552 KB Output is correct
2 Incorrect 1 ms 2904 KB 3rd lines differ - on the 1st token, expected: '14340', found: '14522'
3 Halted 0 ms 0 KB -