알고리즘/구현,시뮬

SWEA ) 대관이의 대량할인

개발자가될수있을까? 2020. 2. 9. 19:40

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIseXoKEUcDFAWN&categoryId=AWIseXoKEUcDFAWN&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 


재관이는 3벌을사면 3벌중 가장 싼 옷을 무료로 받을수 있다.

 

예를들어서 10 7 8 4 2 2 4 옷을 사면

 

나올수 있는 조합에서 가장싸게 구하는 경우를 답으로 채택하면 된다.

 

잘 생각해보면 세일을 크게 받는 경우는 가격이 비싼 옷끼리 묶어 사는경우이다.

 

자연스럽게 배열의 정렬을 떠올리게되고 뒤에서부터 3번째 경우에만 따로 합을구해서 

 

전체합에서 빼면끝!

 

이런건 5분내로 푸는습관을 들이자..

 


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
 
 
public class 대량세일 {
    static int answer = 0;
    public static void main(String[] args) throws Exception {
        System.setIn(new FileInputStream("src/ans/대량세일.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st= new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();
        int t = Integer.parseInt(st.nextToken());
        for(int tc = 1; tc<= t ; tc++) {
            
            st = new StringTokenizer(br.readLine());
            int sum = 0;
            int n = Integer.parseInt(st.nextToken());
            int num[] = new int[n];
            st = new StringTokenizer(br.readLine());
            for(int index = 0 ; index < n ; index++){
                num[index] = Integer.parseInt(st.nextToken());
                sum+= num[index];
            }
            Arrays.sort(num);
            int sub =0;
            for(int index =n-3 ; index >=0 ; index-=3){
                sub += num[index];
            }
            answer = sum - sub;
            sb.append("#"+tc+" "+ answer+"\n");
            answer= 0;
        }
        System.out.println(sb);
    }
 
}