๋ฐ์ํ
๋ฌธ์ |
1107๋ฒ: ๋ฆฌ๋ชจ์ปจ
์ฒซ์งธ ์ค์ ์๋น์ด๊ฐ ์ด๋ํ๋ ค๊ณ ํ๋ ์ฑ๋ N (0 โค N โค 500,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์๋ ๊ณ ์ฅ๋ ๋ฒํผ์ ๊ฐ์ M (0 โค M โค 10)์ด ์ฃผ์ด์ง๋ค. ๊ณ ์ฅ๋ ๋ฒํผ์ด ์๋ ๊ฒฝ์ฐ์๋ ์ ์งธ ์ค์๋ ๊ณ ์ฅ๋ ๋ฒํผ์ด ์ฃผ์ด์ง๋ฉฐ, ๊ฐ์ ๋ฒํผ์ด ์ฌ๋ฌ ๋ฒ ์ฃผ์ด์ง๋ ๊ฒฝ์ฐ๋ ์๋ค.
www.acmicpc.net
ํ์ด |
๋ถ๋ฅ : ์์ ํ์, ์ํ
์ฝ๋(O(nm)) *O(nm)์ด ์๋๋ผ๋ฉด ์๋ ค์ฃผ์ธ์. ๊ฐ์ฌํฉ๋๋ค(--)(__)* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <algorithm> | |
#include <cmath> | |
#define INF 987654321 | |
#define MAX 1000001//์๋น์ด๊ฐ ์ด๋ํ๋ ค๋ ์ฑ๋ | |
using namespace std; | |
int n, m; | |
vector<int> broken; | |
//๊ฐ๋ ค๋ ์ฑ๋์ด 532์ผ๋ 5,3,2๋ฒํผ ์ค ํ๋๋ผ๋ ๋ง๊ฐ์ก๋์ง ํ์ธ | |
//ํ๋๋ผ๋ ๋ง๊ฐ์ง : false | |
bool possi(int channel) { | |
string c = to_string(channel); | |
int size = c.size(); | |
for (int i = 0; i < size; i++) { | |
if (find(broken.begin(), broken.end(), c[i] - '0') != broken.end()) | |
return false; | |
} | |
return true; | |
} | |
//100์์ ์ฑ๋ ๋๋ฅด๊ณ +/- | |
int changeChannelWithBtn() { | |
int ret = INF; | |
int find = 0; | |
for (int i = 0; i < MAX; i++) { | |
if (possi(i)) {//ํด๋น ์ฑ๋์ ๋๋ฅผ ์ ์๋ค๋ฉด | |
int pushCnt = abs(n - i); | |
if (ret > pushCnt) { | |
ret = pushCnt; | |
find = i; | |
} | |
} | |
} | |
return ret + to_string(find).size(); | |
} | |
int main() { | |
int cnt = 0, channel = 100; | |
cin >> n >> m; | |
for (int i = 0, idx = 0; i < m; i++) | |
cin >> idx, broken.push_back(idx); | |
//1. ๊ธฐ์กด์ฑ๋์์ +,- | |
//2. ๋ฒํผ๋๋ฌ ์ฑ๋ ์ ๋ ฅ ํ +,- | |
cout << min(abs(n-100), changeChannelWithBtn()) << endl; | |
return 0; | |
} |
๋ฐ์ํ
'๐ค PS(Problem Solving) > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/c++] 1012๋ฒ - ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2019.12.01 |
---|---|
[๋ฐฑ์ค/c++] 11729๋ฒ - ํ๋ ธ์ด ํ ์ด๋ ์์ (0) | 2019.11.20 |
[๋ฐฑ์ค/c++] 10819๋ฒ - ์ฐจ์ด๋ฅผ ์ต๋๋ก (0) | 2019.11.14 |
[๋ฐฑ์ค/c++] 10610๋ฒ - 30 (0) | 2019.11.14 |
[๋ฐฑ์ค/c++] 10971๋ฒ - ์ธํ์ ์ํ2 (0) | 2019.11.13 |