Submission #895925

#TimeUsernameProblemLanguageResultExecution timeMemory
895925joonwu04Wiring (IOI17_wiring)C++17
20 / 100
20 ms5196 KiB
#include "wiring.h" 
#include <iostream>
#define MAX 1000000000000000000

using namespace std;

typedef long long ll;

ll abs(ll x) {
	return x>=0 ? x:-x;
}

bool isSub1(int rSize, int bSize) {
	return rSize<=200 && bSize<=200;
}

ll sub1Dp[210][210]; // sub1Dp[i][j]: minCost when connecting r[0~i], b[0~j]

ll sub1(vector<int> &r, vector<int> &b) {
	int n = r.size(), m = b.size();
	
	sub1Dp[0][0] = abs(r[0] - b[0]);
	for(int j=1; j<m; j++) {
		sub1Dp[0][j] = sub1Dp[0][j-1] + abs(r[0] - b[j]);
	}
	for(int i=1; i<n; i++) {
		sub1Dp[i][0] = sub1Dp[i-1][0] + abs(r[i] - b[0]);
		for(int j=1; j<m; j++) {
			// r[i], b[j] should be connected
			ll connectingCost = abs(r[i] - b[j]);
			
			// case 1: r[i], b[j] are seperated to the rest
			ll t = sub1Dp[i-1][j-1]; // t is the restCost	
			
			// case 2: r[i] is connected to the rest 
			// in this case, b[j] is seperated to the rest
			t = min(t, sub1Dp[i][j-1]);
			
			// case 3: b[j] is connected to the rest
			// in this case, r[i] is seperated to the rest
			t = min(t, sub1Dp[i-1][j]);
			
			sub1Dp[i][j] = t + connectingCost;
		}
	}
	
	return sub1Dp[n-1][m-1];
}

bool isSub2(vector<int> &r, vector<int> &b) {
	return r.back() < b[0];
}

ll sub2(vector<int> &left, vector<int> &right, int lSt, int lEd, int rSt, int rEd) {
	
	ll lSum = 0;
	for(int i=lSt; i<=lEd; i++) {
		lSum += left[lEd] - left[i];
	}
	
	ll rSum = 0;
	for(int j=rSt; j<=rEd; j++) {
		rSum += right[j] - right[rSt];
	}
	
	ll connectingCost = (ll)(right[rSt] - left[lEd]) * max(lEd-lSt+1, rEd-rSt+1);
	
	return lSum + rSum + connectingCost;
}

bool isSubtask3(std::vector<int> r, std::vector<int> b) {
  int n=r.size(), m=b.size();
  vector<int> a(n+m);
  for(int i=0, j=0; i<m || j<n;) 
    i==n? a[m+i++]= 0: j==m? a[n+j++] = 1: a[i+j] = r[i]<b[j]? r[i++]:b[j++];
  int cnt = 1;
  for(int k=1; k<n+m; k++) {
    cnt = a[k]==a[k-1]? cnt+1:0;
     if (cnt >= 7) return false;
  }
  return true;
}

bool isSub3(vector<int> &r, vector<int> &b) {
	int n = r.size(), m = b.size();
	vector<int> v;
	
	int i=0, j=0;
	while(i < n || j < m) {
		if(i == n) { v.push_back(0); j++;}
		else if(j == m) { v.push_back(1); i++;}
		else if(r[i] < b[j]) { v.push_back(1); i++;}
		else { v.push_back(0); j++;}
	}
	
	int cnt = 1;
	for(int i=1; i<v.size(); i++) {
		if(v[i] == v[i-1]) cnt++;
		else cnt = 1;
		
		if(cnt == 7) return false;
	}
	
	return true;
}

void makeVectors(vector<int> &color, vector<int> &idx, vector<int> &r, vector<int> &b) {
	int n = r.size(), m = b.size();
	
	int i=0, j=0;
	while(i < n || j < m) {
		if(i == n) {color.push_back(0); idx.push_back(j); j++;}
		else if(j == m) {color.push_back(1); idx.push_back(i); i++;}
		else if(r[i] < b[j]) {color.push_back(1); idx.push_back(i); i++;}
		else {color.push_back(0); idx.push_back(j); j++;}
	}
}

void printVector(vector<int> &v) {
	printf("{");
	for(int i=0; i<v.size(); i++) {
		printf("%d,", v[i]);
	}
	printf("}\n");
}

ll sub3Dp[200010]; // minCost of v[0~i] 

ll sub3(vector<int> &r, vector<int> &b) {
	vector<int> color, idx;
	makeVectors(color, idx, r, b);
	int vSize = color.size();
	//printVector(color);	// OK
	
	int rightSt=-1, rightEd=-1;
	int vSt = -1;
	for(int vIdx=0; vIdx<vSize; vIdx++) {
		sub3Dp[vIdx] = MAX;
		
		if(color[vIdx] != color[vIdx+1]) {
			vSt = vIdx + 1;
			rightSt = 0;
			rightEd = vIdx;
			break;
		}
	}
	
	int prevL = -1, prevR = -1;
	/*
	printf("(prevL, prevR) = (%d, %d)\n", prevL, prevR);
	printf("leftRed: "); printVector(leftRed);
	printf("rightBlue: "); printVector(rightBlue);*/
	 // OK
	int leftEd=-1;
	for(int vIdx=vSt; vIdx<vSize; vIdx++) {
		sub3Dp[vIdx] = MAX;
		
		if(color[vIdx] != color[vIdx-1]) {
			prevL = rightSt;
			prevR = rightEd;
			rightSt = vIdx;
			leftEd = prevR;
			
			//printf("vIdx = %d\n", vIdx);
			//printf("(prevL, prevR) = (%d, %d)\n", prevL, prevR);
		}
		
		rightEd = vIdx;
		
		for(int leftSt=prevR; leftSt>=prevL; leftSt--) {
			bool reversed = color[vIdx] == 1;
			
			//printf("(vIdx, leftSt, leftEd, rightSt, rightEd) = (%d, %d, %d, %d, %d)\n", vIdx, leftSt, leftEd, rightSt, rightEd);
			ll cost;
			if(reversed) {
				cost = sub2(b, r, idx[leftSt], idx[leftEd], idx[rightSt], idx[rightEd]);
				//printf("cost=%lld\n", cost);
			}
			else cost = sub2(r, b, idx[leftSt], idx[leftEd], idx[rightSt], idx[rightEd]);
			
			if(sub3Dp[leftSt] == MAX) {
				if(leftSt == prevL) {
					sub3Dp[vIdx] = cost;
					break;
				}
				else continue;
			}
			
			ll rest = sub3Dp[leftSt];
			if(sub3Dp[leftSt-1] != MAX)
				rest = min(rest, sub3Dp[leftSt-1]);
			
			//printf("rSt=%d: cost = %lld     rest = %lld\n", rSt, cost, rest);
			
				
			sub3Dp[vIdx] = min(sub3Dp[vIdx], cost + rest);
		}
		
		//printf("sub3Dp[%d] = %lld\n", vIdx, sub3Dp[vIdx]);
	}
	
	return sub3Dp[vSize-1];
}

ll min_total_length(vector<int> r, vector<int> b) {
	int n = r.size(), m = b.size();
	if(isSub1(n, m)) {
		return sub1(r, b);
	}
	else if(isSub2(r, b)) {
		return sub2(r, b, 0, n-1, 0, m-1);
	}
	else if(isSubtask3(r, b)) {
		return sub3(r, b);
	}
	else return -1;
}

Compilation message (stderr)

wiring.cpp: In function 'bool isSubtask3(std::vector<int>, std::vector<int>)':
wiring.cpp:75:67: warning: operation on 'i' may be undefined [-Wsequence-point]
   75 |     i==n? a[m+i++]= 0: j==m? a[n+j++] = 1: a[i+j] = r[i]<b[j]? r[i++]:b[j++];
      |                                                                  ~^~
wiring.cpp:75:74: warning: operation on 'j' may be undefined [-Wsequence-point]
   75 |     i==n? a[m+i++]= 0: j==m? a[n+j++] = 1: a[i+j] = r[i]<b[j]? r[i++]:b[j++];
      |                                                                         ~^~
wiring.cpp: In function 'bool isSub3(std::vector<int>&, std::vector<int>&)':
wiring.cpp:97:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |  for(int i=1; i<v.size(); i++) {
      |               ~^~~~~~~~~
wiring.cpp: In function 'void printVector(std::vector<int>&)':
wiring.cpp:121:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |  for(int i=0; i<v.size(); i++) {
      |               ~^~~~~~~~~
#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...