티스토리 뷰

15465-Milk Measurement(Bronze)(set)


각 날짜마자 소의 우유 생산 변동량이 제시되고, 그동안 우유 생산량이 가장 많은 소는 변동이  총 몇 번 일어나는지 묻고있다. 우선 day를 오름차순으로 정렬한 뒤, 매 변동량을 update해준다.

여기서 우유 생산량이 가장 많은 소가 둘 이상일 수도 있다. 그럴 때는 여러 소를 동시에 포함해야 하므로 stl set을 사용했다. 따라서 내가 고려해야할 문제는 매 day마다 set의 원소가 이전 day보다 같은지 아닌지만 확인하면 된다. 처음에 두 set의 원소가 일치하는 지의 여부를 직접 구현했는데, 찾아보니 단순하게 == 연산자를 쓰면 된다고 한다. set(a) == set(b) 는 set인 a와 b의 원소가 모두 일치하면 true, 하나라도 다른게 있으면 false를 반환한다.




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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<set>
using namespace std;
#define SIZE 110
struct Cow{
    int day,vol;
    char *name;
 
    Cow(){}
    Cow(int _day,char* _name,int _vol){
        day=_day;
        name=_name;
        vol=_vol;
    }
 
    const bool operator <(const Cow &o) const {
        return day<o.day;
    }
};
int n;
Cow cow[SIZE];
int main(){
    scanf("%d ",&n);
    int cnt=0;
 
    char *name_array[SIZE];
    char b[SIZE][SIZE];
    int milk[SIZE];
 
    for(int i=0 ; i<n ; i++){
        bool flag=false;
        int a,c;    
 
        scanf("%d %s%d",&a,b[i],&c);
        cow[i]=Cow(a,b[i],c);
 
 
        for(int j=0 ; j<cnt ; j++){
            if(strcmp(name_array[j],b[i])==0){
                flag=true;
                break;
            }
        }
 
        if(!flag) {
            name_array[cnt]=b[i];
            milk[cnt]=7;
            cnt++;
        }
 
    }
 
    sort(cow,cow+n);
 
    int ans=0;
 
    set<int> prev_set[SIZE];
 
    for(int i=0 ; i<n ; i++){
    int max_milk=-(1<<20);
        set<int> cur_set;
        for(int j=0 ; j<cnt ; j++){
            if(strcmp(name_array[j],cow[i].name)==0){
                milk[j]+=cow[i].vol;
                break;
            }
        }
 
        for(int j=0 ; j<cnt ; j++){
            max_milk=max(max_milk,milk[j]);
        }
 
        for(int j=0 ; j<cnt ; j++){
            if(max_milk == milk[j]){
                cur_set.insert(j);
                prev_set[i].insert(j);
            }
        }
 
        if(i==0){
            ans++;
            continue;
        }
 
        if(cur_set!=prev_set[i-1]){
            ans++;
        }
    }
 
    printf("%d\n",ans);
    return 0;
}
cs


댓글