Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 25, 2019 11:20
Show Gist options
  • Save surinoel/a413b95684d929de739389c3604af0f5 to your computer and use it in GitHub Desktop.
Save surinoel/a413b95684d929de739389c3604af0f5 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool check[1001];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
check[a[i]] = true;
}
int k;
cin >> k;
if (check[k]) {
cout << 0 << '\n';
}
else {
a.push_back(k);
sort(a.begin(), a.end());
int idx;
for (int i = 0; i < n + 1; i++) {
if (a[i] == k) {
idx = i;
break;
}
}
int left, right;
left = (k - 1) - a[idx - 1];
if (left < 0) left = 0;
right = (a[idx + 1] - 1) - k;
if (right < 0) right = 0;
int ans = left + right + left*right; // 왼쪽, 오른쪽, 왼쪽*오른쪽의 경우의 수를 모두
cout << ans << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment