Submission #622555

#TimeUsernameProblemLanguageResultExecution timeMemory
622555Dremix10Wiring (IOI17_wiring)C++17
0 / 100
122 ms262144 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
	#define pp(x) cerr<<#x<<" = "<<x.F<<"-"<<x.S<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto v : x)cerr<<v<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto v : x)cerr<<v.F<<"-"<<v.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x) {}
    #define p2(x,y) {}
	#define pp(x) {}
    #define pv(x) {}
    #define ppv(x) {}
#endif
const int N = 3e5+5;
const int MOD = 1e9+7;
const ll INF = 1e18+5;

void solve(int i){


}

long long min_total_length(std::vector<int> r, std::vector<int> b) {
	int i,j,k;
	int n = r.size();
	int m = b.size();

	ll ans = 0;
	int cr[n] = {};
	int cb[m] = {};
	bool v[n][m] = {};
	vector<pi> arr;

	for(i=0;i<n;i++){
		int idx = -1;
		for(j=0;j<m;j++){
			if(idx == -1 || abs(r[i]-b[j]) < abs(r[i]-b[idx]))
				idx = j;
		}
		cr[i]++;
		cb[idx]++;
		v[i][idx] = true;
		arr.push_back({i,idx});
		ans += abs(r[i] - b[idx]);
	}
	
	for(i=0;i<m;i++){
		int idx = -1;
		for(j=0;j<n;j++){
			if(idx == -1 || abs(r[j]-b[i]) < abs(b[i]-r[idx]))
				idx = j;
		}
		if(v[idx][i])continue;
		cb[i]++;
		cr[idx]++;
		v[idx][i] = true;
		arr.push_back({idx,i});
		ans += abs(b[i] - r[idx]);
	}

	bool flag = true;
	//vector<pi> neo;
	while(flag){
		flag = false;
		vector<pi> neo;
		ppv(arr)
		p(ans)
		pv(cr)
		pv(cb)
		for(auto &x : arr){
			//neo.push_back(x);
			if(v[x.F][x.S] == false)continue;
			if(cr[x.F] > 1 && cb[x.S] > 1){
				cr[x.F] --;
				cb[x.S] --;
				ans -= abs(r[x.F] - b[x.S]);
				v[x.F][x.S] = false;
				//neo.pop_back();
				continue;
			}
			if(cr[x.F] == 1 && cb[x.S] == 1){
				//neo.push_back(x);
				continue;
			}
			for(auto &y : arr){
				
				if(v[y.F][y.S] == false)continue;
				if(cr[y.F] == 1 && cb[y.S] == 1 || x==y)continue;
				if(cr[y.F] > 1 && cb[y.S] > 1)continue;
				if(cr[x.F] == 1 && cb[y.S] == 1){
					
					if(abs(r[x.F]-b[x.S]) + abs(r[y.F]-b[y.S]) > abs(r[x.F] - b[y.S])){
						pp(x)
						pp(y)
						cr[y.F]--;
						cb[x.S]--;
						ans -= abs(r[x.F]-b[x.S]) + abs(r[y.F]-b[y.S]) - abs(r[x.F] - b[y.S]);
						v[x.F][x.S] = false;
						v[y.F][y.S] = false;
						v[x.F][y.S] = true;
						neo.push_back({x.F,y.S});
						break;
					}
				}
				else if(cb[x.S] == 1 && cr[y.F] == 1){
					
					if(abs(r[x.F]-b[x.S]) + abs(r[y.F]-b[y.S]) > abs(r[y.F] - b[x.S])){
						pp(y)
						pp(x)
						cr[x.F]--;
						cb[y.S]--;
						ans -= abs(r[x.F]-b[x.S]) + abs(r[y.F]-b[y.S]) - abs(r[y.F] - b[x.S]);
						v[x.F][x.S] = false;
						v[y.F][y.S] = false;
						v[y.F][x.S] = true;
						neo.push_back({y.F,x.S});
						break;
					}
				}
			}
		}
		vector<pi> temp;
		for(auto &x : arr){
			if(v[x.F][x.S] == false){
				flag = true;
				continue;
			}
			temp.push_back(x);
		}
		for(auto &x : neo){
			flag = true;
			temp.push_back(x);
		}
		arr = temp;
	}

	return ans;
}

Compilation message (stderr)

wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:97:21: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   97 |     if(cr[y.F] == 1 && cb[y.S] == 1 || x==y)continue;
      |        ~~~~~~~~~~~~~^~~~~~~~~~~~~~~
wiring.cpp:33:10: warning: unused variable 'k' [-Wunused-variable]
   33 |  int i,j,k;
      |          ^
#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...