Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- npm
- Effective Java
- git
- 정규표현식
- mysql 5.5
- migration
- JavaScript
- MySQL
- current_date
- spring cloud
- log_bin
- Regular expression
- nodejs
- REACTJS
- Express
- 퀵소트
- spring
- eslint
- regex
- log4j2
- REACT
- update
- Webpack
- Spring Batch
- Chunk
- java
- Node
- expire_logs_days
- upgrade
- Effective Java 3/e
Archives
- Today
- Total
내 세상
[Algorithm] 백준 14889번 스타트와 링크 본문
728x90
백준 14889번 스타트와 링크
팀원들 한명씩 돌면서 start팀으로 사용할까 말까를 구현해서 코딩하면 됨.
확인은 안해봤지만, 아래와 같은 경우는 똑같은 계산을 2번 하게 되는 문제가 생길 수 있음
ex) 4명의 사원(1~4)
start팀(1,2) link팀 (3,4)
start팀(3,4) link팀 (1,2)
그래서 미리 전체 조합수(totalCase)를 계산해서 그 절반까지만 계산을 하도록 하였음. (Line 41)
어차피 순차적으로 앞에서부터 한명씩 고르기 때문에 총 진행에 절반만큼만 하면 중복되는 경우는 하지 않기 때문입니다.
참 쉽쥬?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream> #include <stdio.h> #include <algorithm> #include <vector> #include <math.h> #define ll long long int using namespace std; int N; int synergy[21][21]; bool stVisit[21]; ll answer = 1e10; ll totalCase = 0; void calSynergy() { int startSynergy = 0; int linkSynergy = 0; for (int i = 0; i < N; i++) { if (stVisit[i]) { for (int j = i + 1; j < N; j++) { if (stVisit[j]) { startSynergy += (synergy[i][j] + synergy[j][i]); } } } else { for (int j = i + 1; j < N; j++) { if (!stVisit[j]) { linkSynergy += (synergy[i][j] + synergy[j][i]); } } } } if (answer > abs(startSynergy - linkSynergy)) { answer = abs(startSynergy - linkSynergy); } } int choiceCase = 0; void choiceStartTeam(int pos, int stCnt) { if (pos > N || choiceCase == totalCase) return; if (stCnt == N / 2) { calSynergy(); choiceCase++; return; } stVisit[pos] = true; choiceStartTeam(pos + 1, stCnt + 1); stVisit[pos] = false; choiceStartTeam(pos + 1, stCnt); } void calTotalCase() { totalCase = 1; for (int i = N; i >= 2; i--) { totalCase *= i; } for (int i = N / 2; i >= 2; i--) { totalCase /= pow(i, 2); } } int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> synergy[i][j]; } } calTotalCase(); choiceStartTeam(0, 0); cout << answer; return 0; } | cs |
728x90
'Coding > Algorithms' 카테고리의 다른 글
[Algorithm] 백준 14890번 경사로 (0) | 2019.03.21 |
---|---|
[Algorithm] 백준 15683번 감시 (0) | 2019.03.17 |
[Algorithm] 백준 14888번 연산자 끼워넣기 (0) | 2019.03.17 |
[Algorithm] 백준 13458번 시험 감독 (0) | 2019.03.17 |
[Algorithm] 백준 14501번 퇴사 (1) | 2019.03.17 |