24922023-01-13 19:23:45kohumarkTV szolgáltatókcpp11Wrong answer 9/4035ms5676 KiB
#include <iostream>
#include <climits>
using namespace std;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int n, k; cin >> n >> k;
	int bj[n][2];
	int veg=0;
	for(int i=0; i<n; i++){
		cin >> bj[i][0] >> bj[i][1];
		veg=max(veg,bj[i][1]);
		bj[i][0]--; bj[i][1]--;
	}
	int petakmi=0; int petakma=0;
	
	
	int mi=k-1; int ma=veg;
	while(ma-mi>1){
		petakmi=0; petakma=0;
		for(int i=0; i<n; i++){
			if(mi-k+1<bj[i][0]) petakmi+=bj[i][0]-(mi-k+1);
			if(mi>bj[i][1]) petakmi+=mi-bj[i][1];
			if(ma-k+1<bj[i][0]) petakma+=bj[i][0]-(ma-k+1);
			if(ma>bj[i][1]) petakma+=ma-bj[i][1];
		}
		if(petakmi<petakma) ma=(mi+ma)/2;
		else mi=(mi+ma)/2;
	}
	
	petakmi=0; petakma=0;
	for(int i=0; i<n; i++){
		if(mi-k+1<bj[i][0]) petakmi+=bj[i][0]-(mi-k+1);
		if(mi>bj[i][1]) petakmi+=mi-bj[i][1];
		if(ma-k+1<bj[i][0]) petakma+=bj[i][0]-(ma-k+1);
		if(ma>bj[i][1]) petakma+=ma-bj[i][1];
	}
		
	cout << min(petakmi,petakma);
}
SubtaskSumTestVerdictTimeMemory
base9/40
1Accepted0/03ms1764 KiB
2Accepted0/02ms1928 KiB
3Accepted1/12ms2152 KiB
4Accepted1/12ms2356 KiB
5Wrong answer0/12ms2568 KiB
6Accepted1/12ms2792 KiB
7Accepted2/22ms2876 KiB
8Wrong answer0/22ms2844 KiB
9Accepted2/22ms2848 KiB
10Accepted2/22ms2916 KiB
11Wrong answer0/214ms3960 KiB
12Wrong answer0/214ms3804 KiB
13Wrong answer0/216ms3808 KiB
14Wrong answer0/214ms3932 KiB
15Wrong answer0/228ms4912 KiB
16Wrong answer0/228ms5124 KiB
17Wrong answer0/218ms4428 KiB
18Wrong answer0/218ms4472 KiB
19Wrong answer0/218ms4552 KiB
20Wrong answer0/218ms4760 KiB
21Wrong answer0/118ms4724 KiB
22Wrong answer0/118ms4680 KiB
23Wrong answer0/135ms5544 KiB
24Wrong answer0/135ms5548 KiB
25Wrong answer0/135ms5624 KiB
26Wrong answer0/135ms5448 KiB
27Wrong answer0/135ms5456 KiB
28Wrong answer0/135ms5676 KiB