반응형

https://www.acmicpc.net/problem/20311

 

20311번: 화학 실험

화학 실험을 하던 윤이는 일렬로 나열해 놓은 $N$개의 시험관에서 재밌는 특징을 발견했다. 그 특징은 모든 이웃한 시험관 쌍에 대해, 두 시험관에 들어 있는 시약의 색깔이 서로 다르다는 점이

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 시험관의 개수를 입력받고, priority_queue에 내림차순으로 정렬합니다.

 

2. 만약 수가 가장 많은 시험관의 개수가 (N + 1) / 2개보다 많다면 -1을 출력합니다.

 

3. pq에서 값을 하나씩 뽑아 arr 배열에 시험관의 색깔 번호를 넣습니다.

 

4. 0 ~ (N + 1) / 2 - 1까지 arr 배열의 값을 ans의 0부터 2씩 인덱스를 증가시켜 가면서 넣습니다.

 

5. (N + 1) / 2 ~ N - 1까지 arr배열의 값을 ans의 1부터 2씩 인덱스를 증가시켜 가면서 넣습니다.

 

6. ans 배열을 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<vector>
#include<queue>
 
using namespace std;
 
int N, K;
vector<int> arr;
int ans[300000];
 
int main()
{
    priority_queue<pair<intint>>pq;
    scanf("%d %d"&N, &K);
 
    for (int i = 1; i <= K; i++) {
        int c;
        scanf("%d"&c);
 
        pq.push({ c,i });
    }
 
    if (pq.top().first > (N + 1/ 2) {
        printf("-1");
        return 0;
    }
 
    while (!pq.empty()) {
        const int cnt = pq.top().first;
        const int num = pq.top().second;
        pq.pop();
 
        for (int i = 0; i < cnt; i++) {
            arr.push_back(num);
        }
    }
    
    int num = 0;
 
    for (int i = 0; i < (N + 1/ 2; i++) {
        ans[num] = arr[i];
        num += 2;
    }
 
    num = 1;
 
    for (int i = (N+1)/2; i < N; i++) {
        ans[num] = arr[i];
        num += 2;
    }
 
    for (int i = 0; i < N; i++) {
        printf("%d ", ans[i]);
    }
}
cs
반응형

+ Recent posts