fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int N;
  6.  
  7. int test(int st, vector<int> &weak, vector<int> &dist) {
  8. // weak 의 st 번째 부터 N개의 weak한 지점을 막는데,
  9. // dist 의 순서대로 사람들을 배치할 때 필요한 최소 회수
  10.  
  11. int p = 0;
  12. // 이제 막아야 될 weak의 인덱스
  13. for (int i = 0; i < dist.size(); i++) {
  14. int a = weak[st+p];
  15. // i 번 애가 탐색을 시작할 위치
  16. while(p < N and weak[st+p] <= a+dist[i]) p++;
  17. // 탐색 범위 안에 있는 동안 p를 증가시킨다.
  18. if (p == N) return i+1;
  19. // 만약 N개를 모두 탐색했으면, 지금까지 탐색했던 애들의 수를 반환
  20. }
  21.  
  22. return 1000;
  23. // 끝까지 못찾으면 1000을 반환
  24. }
  25.  
  26. int solution(int n, vector<int> weak, vector<int> dist) {
  27. N = weak.size();
  28. // N 은 우리가 막아야 될 약한 지점의 개수
  29.  
  30. for (int i = 0, _i = N; i < _i; i++) {
  31. weak.push_back(weak[i]+n);
  32. }
  33. // 원주 상에 있으니까 배열을 2배로 늘려서 직선처럼 변형
  34. // 이 아래 부터는 weak가 원(circle)인 것은 무시하고 직선처럼 취급
  35. // 2배로 늘렸으니까 이 배열의 길이는 2N인것에 주의
  36.  
  37. sort(dist.begin(), dist.end());
  38. // next_permutation을 쓰기 위한 전처리 작업
  39.  
  40. int result = 1000;
  41. // 결과를 저장할 변수
  42.  
  43. for (int i = 0; i < N; i++) {
  44. // i 는 2배로 늘린 weak 배열에서 막기 시작할 인덱스
  45. // 잘 생각해 보면, 길이가 2N인 직선(위에서 만든)에서 임의의 연속된 N개를 고르면
  46. // 원래 원에서 겹치지 않는 애들로 구성됨을 알 수 있음
  47. // 자세한건 설명 힘드니 본인이 생각
  48.  
  49. do {
  50. result = min(result, test(i, weak, dist));
  51. } while(next_permutation(dist.begin(), dist.end()));
  52. // next_permutation 함수는 입력받은 배열을 재배열한 것들 중 사전순으로 다음 배열을 반환해줌.
  53. // 만약 주어진 배열이 사전순으로 마지막 배열이었으면 0을 반환
  54. }
  55.  
  56. if (result == 1000) return -1;
  57. // 결과가 1000 이면 어떤 경우도 성공시키지 못함
  58. else return result;
  59. // 그 외에는 결과를 반환
  60. }
  61.  
  62.  
  63.  
  64.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty