본문 바로가기
코딩테스트_백준풀이

센서 #2212 c++ 풀이

by wanna_dev 2023. 11. 24.

https://www.acmicpc.net/problem/2212

 

2212번: 센서

첫째 줄에 센서의 개수 N(1 ≤ N ≤ 10,000), 둘째 줄에 집중국의 개수 K(1 ≤ K ≤ 1000)가 주어진다. 셋째 줄에는 N개의 센서의 좌표가 한 개의 정수로 N개 주어진다. 각 좌표 사이에는 빈 칸이 하나 있

www.acmicpc.net

문제이해가 쉽지 않았다. 문제이해력이 골드

구현해봤으나 처참히 실패 누군가 나의 실패를 분석해주면 좋겠다.

//실패코드
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector<int> v;
int main() {
	int n, k;
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n; 
	cin >> k;

	int k1 = k;
	int k2 = k;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		v.push_back(temp);

	}
	sort(v.begin(), v.end());
	int cur_distance;
	int next_distance;
	int total_distance=0;


	for (int i = 0; i < v.size()-2; i++) {//10^4 * 10^3 유효
		//for (int j = 0; j < k; j++) {
		//	
		//}
		//if (v[i] == v[i + 1]) {
		//	//k--;
		//	continue;
		//}
		cur_distance =  v[i + 1]-v[i];
		next_distance = v[i + 2]-v[i+1];
		//cout <<i<<"번째 : " << cur_distance << " " << next_distance << endl;
		if (cur_distance > next_distance) { // 다음 센서로 건너뛰기
			
			
			if (k1 > 1) {
				k1--;
				continue;

			}
			else if (cur_distance == 0) {
				continue;
			}
			

		}
		else if (next_distance >= cur_distance) { //붙이기
			total_distance += cur_distance;
			if (k1 == 1) {
				for (int j = i+1; j < v.size()-1; j++) {
					cur_distance = v[j + 1] - v[j];
					total_distance += cur_distance;
				}
				break;
			}
		}


	

		
		
		//cout <<"total D: " << total_distance << endl;
		//cout <<"K: " << k << endl;
	}
	int total_distance2 = 0;
	
	for (int i = v.size() - 1; i > 1; i--) {//10^4 * 10^3 유효
	//for (int j = 0; j < k; j++) {
	//	
	//}
	//if (v[i] == v[i + 1]) {
	//	//k--;
	//	continue;
	//}
		cur_distance = v[i] - v[i - 1];
		next_distance = v[i - 1] - v[i - 2];
		cout <<i<<"번째 : " << cur_distance << " " << next_distance << endl;
		if (cur_distance > next_distance) { // 다음 센서로 건너뛰기


			if (k2 > 1) {
				k2--;
				if (k2 == 1) {
					cout << "K는 1입니다" << endl;
					total_distance2 += cur_distance;
					for (int j = i - 1; j >= 1; j--) {
						cur_distance = v[j] - v[j - 1];
						total_distance2 += cur_distance;
						cout << "total2 : " << total_distance2 << endl;
					}
					break;
				}
				continue;

			}
			else if (cur_distance == 0) {
				continue;
			}


		}
		else if (next_distance >= cur_distance) { //붙이기
			total_distance2 += cur_distance;
			cout << "total2 : " << total_distance2 << endl;
			if (k2 == 1) {
				cout << "K는 1입니다" << endl;

				for (int j = i -1; j > 1; j--) {
					cur_distance = v[j] - v[j - 1];
					
					total_distance2 += cur_distance;
				}
				break;
			}
		}
		
	}
	//for (int i = v[0]; i < v[v.size() - 1]; i++) {
	//
	//}
	cout << "total2 : " << total_distance2 << endl;
	cout << "total1 : " << total_distance << endl;
	cout << min(total_distance,total_distance2) << endl;
	return 0;
}

 

정답은 다음 블로그를 참고하세요

https://dong-gas.tistory.com/32

 

[백준] 2212 센서 (C++)

2212번: 센서 첫째 줄에 센서의 개수 N(1 ≤ N ≤ 10,000), 둘째 줄에 집중국의 개수 K(1 ≤ K ≤ 1000)가 주어진다. 셋째 줄에는 N개의 센서의 좌표가 한 개의 정수로 N개 주어진다. 각 좌표 사이에는 빈

dong-gas.tistory.com

 

'코딩테스트_백준풀이' 카테고리의 다른 글

BOJ 1244 스위치 켜고 끄기 JAVA  (0) 2024.01.29
배 #1092 c++ 풀이  (2) 2023.11.24
강의실 배정 #11000  (1) 2023.11.23
로프 #2217 c++ 풀이  (0) 2023.11.22
행렬 #1080 c++ 풀이  (0) 2023.11.22