ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

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

 

2457๋ฒˆ: ๊ณต์ฃผ๋‹˜์˜ ์ •์›

์ฒซ์งธ ์ค„์—๋Š” ๊ฝƒ๋“ค์˜ ์ด ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฝƒ์ด ํ”ผ๋Š” ๋‚ ์งœ์™€ ์ง€๋Š” ๋‚ ์งœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ•˜๋‚˜์˜ ๋‚ ์งœ๋Š” ์›”๊ณผ ์ผ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ˆซ์ž๋กœ ํ‘œํ˜„๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ,

www.acmicpc.net

[ํ’€์ด]

find()๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ ๊ฒŒ ๊ฝƒ์„ ํ”ผ์šธ ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ๊ตฌํ•ด์ฃผ์—ˆ๋Š”๋ฐ, find()๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

  • ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€(ํ”ผ์šฐ์ง€ ์•Š์€) ๊ฝƒ ์ค‘์—์„œ ํ˜„์žฌ ์กฐํšŒ์ค‘์ธ ์‹œ์ž‘ ๋‚ ์งœ์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ด์ „์— ํ”ผ์–ด์„œ ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ์ง€๋Š” ๊ฝƒ์„ ์ฐพ์•„์„œ ํ•ด๋‹น ๊ฝƒ์˜ index๋ฅผ ์ €์žฅ
  • ๋งŒ์•ฝ ๊ฝƒ์„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜์—ฌ 0์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒ
  • ๋งŒ์•ฝ ์กฐํšŒ๋œ index์˜ ์ข…๋ฃŒ ๋‚ ์งœ๊ฐ€ 12์›” 1์ผ ์ดํ›„๋ผ๋ฉด ์ด์ œ๊นŒ์ง€ ํ•€ ๊ฝƒ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒ
  • index๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ํ•˜๊ณ  ๊ฝƒ์˜ ๊ฐœ์ˆ˜ + 1์„ ํ†ตํ•ด ๊ฐฑ์‹ 

๋”ฐ๋ผ์„œ ์˜ˆ์‹œ 1๋ฒˆ ์ฝ”๋“œ์˜ ๋™์ž‘ ๊ณผ์ •์„ ์‚ดํŽด๋ณด๋ฉด

  • index 1์˜ [ 1, 1, 6, 30 ] ์„ ๊ณ ๋ฅธ ํ›„, ์กฐํšŒํ•  ๊ธฐ์ค€ ๋‚ ์งœ๋ฅผ 6 / 30๋กœ ๊ฐฑ์‹ ํ•˜๊ณ  cnt++
  • index 3์˜ [ 6, 10, 12, 10 ] ์„ ๊ณ ๋ฅธ ํ›„, ์ข…๋ฃŒ ๋‚ ์งœ๊ฐ€ 12 / 10๋กœ 12์›” 1์ผ ์ดํ›„์˜ ๋‚ ์งœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ข…๋ฃŒํ•˜๊ณ  cnt++์„ ์ถœ๋ ฅ

[์ฝ”๋“œ]

const fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().split('\n');

const N = parseInt(input[0]);

const arr = [];

for(let i=1;i<=N;i++) {
    arr.push(input[i].split(" ").map(Number));
}

// ๊ธฐ์ค€ ๋‚ ์งœ m, d๋ฅผ ๋„˜์ง€ ์•Š๊ณ  ๊ฐ€์žฅ ์˜ค๋ž˜ ์ง€์†๋˜๋Š” ๊ฝƒ์„ ๊ณ ๋ฆ„

let cnt = 0;

const find = () => {
    let m = 3;
    let d = 1;
    let index = -1;
    
    let endMonth = 0;
    let endDay = 0;

    const visit = Array(N).fill(false);

    while(true) {
        for(let i=0;i<N;i++) {
            if(visit[i]) {
                continue;
            }
            if(arr[i][0] < m || (arr[i][0] === m && arr[i][1] <= d)) {
                if(arr[i][2] > endMonth || (arr[i][2] === endMonth && arr[i][3] > endDay)) {
                    index = i;
                    endMonth = arr[i][2];
                    endDay = arr[i][3];
                }
            }
        }
        if(index == -1) {
            console.log(0);
            break;
        }
        m = arr[index][2];
        if(m === 12) {
            console.log(cnt + 1);
            break;
        }
        d = arr[index][3];
        visit[index] = true;
        cnt++;
        endMonth = 0;
        endDay = 0;
        index = -1;
    }
}

find();