Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Input, Output
*.in
*.out
input.txt
output.txt
answer.txt

### Eclipse ###
.metadata
Expand Down
159 changes: 159 additions & 0 deletions problems/SWEA/p1952/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* {풀이 하는 중} (1952) [모의 SW 역량테스트] 수영장
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpFQaAQMDFAUq&categoryId=AV5PpFQaAQMDFAUq&categoryType=CODE&problemTitle=1952&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1
*/

import java.io.*;
import java.util.*;

public class Solution {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;

// --------------------------------------------------------

public static void main(String[] args) throws IOException {
final int testCount = Integer.parseInt(reader.readLine().trim());

for (int testCase = 1; testCase <= testCount; testCase++) {
new Solution(testCase).run();
}
}

// --------------------------------------------------------

static final int TICKET_TYPE_LEN = 4;
static final int MONTH_LEN = 12;

static final int ONE_DAY_TICKET = 0;
static final int ONE_MONTH_TICKET = 1;
static final int THREE_MONTH_TICKET = 2;
static final int ONE_YEAR_TICKET = 3;

int testCase;
int bestPrice;

boolean noPlan;
int[] priceArr;
int[] planArr;

int[] oneDayTicketArr;
boolean[] monthTicketArr;

public Solution(int testCase) {
this.testCase = testCase;
}

public void run() throws IOException {
input();
solve();
print();
}

private void input() throws IOException {
getLine();
priceArr = new int[TICKET_TYPE_LEN];
for (int index = 0; index < TICKET_TYPE_LEN; index++) {
priceArr[index] = Integer.parseInt(input.nextToken());
}

getLine();
noPlan = true;
planArr = new int[MONTH_LEN];
for (int index = 0; index < MONTH_LEN; index++) {
planArr[index] = Integer.parseInt(input.nextToken());
if (planArr[index] != 0) noPlan = false;
}
}

private void solve() {
// 만약 이용계획이 없는 경우 bestPrice는 0이 된다.
if (noPlan) {
bestPrice = 0;
return;
}

bestPrice = priceArr[ONE_YEAR_TICKET]; // 1년 이용권

oneDayTicketArr = new int[MONTH_LEN];
monthTicketArr = new boolean[MONTH_LEN];

offset = bestPrice * 2;
cache = new boolean[13 * offset];

searchBestPrice(0, 0);
}

int offset;
boolean[] cache;

private void searchBestPrice(int month, int currentPrice) {
if (month > MONTH_LEN || currentPrice >= bestPrice) return;

int cacheKey = month * offset + currentPrice;
// System.out.printf("searchBestPrice(%d, %d)\n", month, currentPrice);
if (cache[cacheKey]) return;
cache[cacheKey] = true;

while ( (month < MONTH_LEN) &&
(monthTicketArr[month] || oneDayTicketArr[month] >= planArr[month])
) {
month++;
}

// 기저조건
if (month >= MONTH_LEN) {
if (currentPrice < bestPrice) bestPrice = currentPrice;
cache[cacheKey] = true;
return;
}

// System.out.printf("[%d] searchBestPrice(%d, %d)\n", testCase, month, currentPrice);

int price;

// 1일 이용권
price = currentPrice + priceArr[ONE_DAY_TICKET];
if (price < bestPrice) {
oneDayTicketArr[month]++;
if (oneDayTicketArr[month] >= planArr[month]) {
searchBestPrice(month + 1, price);
} else {
searchBestPrice(month, price);
}
oneDayTicketArr[month]--;
}

// 1달 이용권
price = currentPrice + priceArr[ONE_MONTH_TICKET];
if (price < bestPrice) {
monthTicketArr[month] = true;
searchBestPrice(month + 1, price);
monthTicketArr[month] = false;
}

// 3달 이용권
price = currentPrice + priceArr[THREE_MONTH_TICKET];
if (price < bestPrice) {
for (int m = month; m < month + 2 && m < MONTH_LEN; m++) monthTicketArr[m] = true;
searchBestPrice(month + 3, price);
for (int m = month; m < month + 2 && m < MONTH_LEN; m++) monthTicketArr[m] = false;
}

cache[cacheKey] = true;
}

private void print() throws IOException {
writer.write("#" + testCase);
writer.write(" " + bestPrice);
writer.write("\n");
writer.flush();
}

// --------------------------------------------------------

private void getLine() throws IOException {
input = new StringTokenizer(reader.readLine().trim());
}
}
10 changes: 10 additions & 0 deletions problems/SWEA/p1952/answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#1 110
#2 100
#3 400
#4 530
#5 430
#6 1080
#7 1840
#8 800
#9 1980
#10 2260
21 changes: 21 additions & 0 deletions problems/SWEA/p1952/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
10
10 40 100 300
0 0 2 9 1 5 0 0 0 0 0 0
10 100 50 300
0 0 0 0 0 0 0 0 6 2 7 8
10 70 180 400
6 9 7 7 7 5 5 0 0 0 0 0
10 70 200 550
0 0 0 0 8 9 6 9 6 9 8 6
10 80 200 550
0 8 9 15 1 13 2 4 9 0 0 0
10 130 360 1200
0 0 0 15 14 11 15 13 12 15 10 15
10 180 520 1900
0 18 16 16 19 19 18 18 15 16 17 16
10 100 200 1060
12 9 11 13 11 8 6 12 8 7 15 6
10 170 500 1980
19 18 18 17 15 19 19 16 19 15 17 18
10 200 580 2320
12 28 24 24 29 25 23 26 26 28 27 22
9 changes: 9 additions & 0 deletions problems/SWEA/p1952/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#1 100
#2 50
#3 180
#4 200
#5 200
#6 360
#7 520
#8 200
#9 500
Loading