좌표는 중복된 값이 들어올 수 있으므로 set 자료구조를 이용하면 편하게 구할 수 있습니다.
6
2
1 6 9 3 6 7
백준 2212 설명
위 예제에서는 센서간의 간격의 길이가 3이 제일 길기 때문에
전체 간격의 길이인 9-1=8에서 3을 빼주면
나머지 센서를 다 포괄할 수 있게 됩니다.
2.문제풀이코드 C++
#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
set<int> num;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
num.insert(tmp);
}
n = num.size();
if (n <= k) {
cout << 0;
return 0;
}
vector<int> v(num.begin(), num.end());
vector<int> interval;
int ans = 0;
for (int i = 0; i < n - 1; i++) {
ans += v[i + 1] - v[i];
interval.push_back(v[i + 1] - v[i]);
}
sort(interval.begin(), interval.end());
for (int i = 0; i < k-1; i++) {
ans -= interval[interval.size() - 1 - i];
}
cout << ans << '\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[1000];
void compare(int x, int y, int z) {
for (int i = 123; i <= 987; i++) {
if (ch[i]) {
int s = 0, b = 0;
int arr1[3] = { i / 100, (i % 100) / 10 ,i % 10 };
int arr2[3] = { x / 100, (x % 100) / 10 ,x % 10 };
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if(j==k && (arr1[j]==arr2[k])){
s++;
}
else if (j != k && arr1[j] == arr2[k]) {
b++;
}
}
}
//후보에서 제외
if (!(y == s && z == b)) {
ch[i] = 0;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
//ch배열 초기화
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (i != j && k != j && i != k) {
ch[i * 100 + 10 * j + k] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
int x, y, z;
cin >> x >> y >> z;
compare(x, y, z);
}
int ans = 0;
for (int i = 0; i < 1000; i++) {
if (ch[i]) ans++;
}
cout << ans << '\n';
return 0;
}
두번째 방법
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[101][1000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
int a, s, b;
cin >> a >> s >> b;
int x = a / 100;
int y = (a % 100) / 10;
int z = a % 10;
if (s == 3 && b == 0) {
cout << 1 << '\n';
return 0;
}
else if (s == 0 && b == 3) {
ch[i][z * 100 + x * 10 + y] = 1;
ch[i][y * 100 + z * 10 + x] = 1;
}
else if (s == 1 && b == 2) {
ch[i][100 * x + 10 * z + y] = 1;
ch[i][100 * z + 10 * y + x] = 1;
ch[i][100 * y + 10 * x + z] = 1;
}
else if (s == 2 && b == 0) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * x + 10 * y + j] = 1;
ch[i][100 * x + 10 * j + z] = 1;
ch[i][100 * j + 10 * y + z] = 1;
}
}
}
else if (s == 1 && b == 1) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * x + 10 * j + y] = 1;
ch[i][100 * x + 10 * z + j] = 1;
ch[i][100 * z + 10 * y + j] = 1;
ch[i][100 * j + 10 * y + x] = 1;
ch[i][100 * j + 10 * x + z] = 1;
ch[i][100 * y + 10 * j + z] = 1;
}
}
}
else if (s == 0 && b == 2) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * j + 10 * x + y] = 1;
ch[i][100 * y + 10 * x + j] = 1;
ch[i][100 * y + 10 * j + x] = 1;
ch[i][100 * z + 10 * j + x] = 1;
ch[i][100 * z + 10 * x + j] = 1;
ch[i][100 * j + 10 * z + x] = 1;
ch[i][100 * z + 10 * j + y] = 1;
ch[i][100 * y + 10 * z + j] = 1;
ch[i][100 * j + 10 * z + y] = 1;
}
}
}
else if (s == 1 && b == 0) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z) {
ch[i][100 * x + 10 * j + k] = 1;
ch[i][100 * j + 10 * y + k] = 1;
ch[i][100 * j + 10 * k + z] = 1;
}
}
}
}
else if (s==0 && b == 1) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z) {
ch[i][100 * j + 10 * x + k] = 1;
ch[i][100 * j + 10 * k + x] = 1;
ch[i][100 * y + 10 * j + k] = 1;
ch[i][100 * j + 10 * k + y] = 1;
ch[i][100 * z + 10 * j + k] = 1;
ch[i][100 * j + 10 * z + k] = 1;
}
}
}
}
else if (s == 0 && b == 0) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int t = 1; t <= 9; t++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z
&& x != t && y != t && z != t && k != t && j != t) {
ch[i][100 * j + 10 * t + k] = 1;
}
}
}
}
}
}
int ans = 0;
for (int i = 100; i < 1000; i++) {
bool flag = true;
for (int j = 0; j < n; j++) {
if (ch[j][i] == 0) {
flag = false;
break;
}
}
if (flag) ans++;
}
cout << ans << '\n';
return 0;
}