Algorithm/Baekjoon

[JavaScript/node.js] ๋ฐฑ์ค€ 14943๋ฒˆ - ๋ฒผ๋ฃฉ ์‹œ์žฅ

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 9. 19. 18:33

[๋ฌธ์ œ]

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

 

14943๋ฒˆ: ๋ฒผ๋ฃฉ ์‹œ์žฅ

๋ฒผ๋ฃฉ์‹œ์žฅ์—์„œ ์‚ฌ๋žŒ๋“ค์ด ๋ฒผ๋ฃฉ์„ ์‚ฌ๊ณ  ํŒ๋‹ค. ๋†€๋ž๊ฒŒ๋„ ๊ฐ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ๋ ค๊ณ  ํ•˜๋Š” ๋ฒผ๋ฃฉ์˜ ํ•ฉ๊ณผ ํŒŒ๋Š” ๋ฒผ๋ฃฉ์˜ ํ•ฉ์€ ๊ฐ™๋‹ค. ๋ฒผ๋ฃฉ์„ ์‚ฌ๊ฑฐ๋‚˜ ํŒŒ๋Š” ์‚ฌ๋žŒ๋“ค์€ ์„œ๋กœ ์ผ๋ ฌ๋กœ ๊ธธ๊ฒŒ ์„œ ์žˆ์œผ๋ฉฐ, ์ธ์ ‘ํ•œ ๊ฐ€๊ฒŒ ์‚ฌ์ด

www.acmicpc.net

[ํ’€์ด]

+๊ฐ’์€ ํŒ๋งค ๋ฐฐ์—ด(sale[]), -๊ฐ’์€ ๊ตฌ๋งค ๋ฐฐ์—ด(buy[])์— ๊ฐ๊ฐ ์ €์žฅํ•œ ํ›„ ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

1. info[]์— ๋‹ด๊ฒจ์žˆ๋Š” ๋ฒผ๋ฃฉ ์‹œ์žฅ ์ •๋ณด๋“ค์„ sale๊ณผ buy์— [๊ธˆ์•ก, index] ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

2. ํŒ๋งค ์ •๋ณด์™€ ๊ตฌ๋งค ์ •๋ณด๋กœ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • buy์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•œ left ๋ณ€์ˆ˜์™€, sale์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•œ right ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ฉ๋‹ˆ๋‹ค.
  • buy์˜ left ๊ธˆ์•ก๊ณผ sale์˜ right๊ธˆ์•ก์„ ๋น„๊ตํ•˜์—ฌ ๋น„์šฉ total๊ณผ left, right๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
    • buy์˜ ๊ธˆ์•ก๊ณผ sale์˜ ๊ธˆ์•ก์ด ๊ฐ™๋‹ค๋ฉด ํŒ๋งคํ•˜๋Š” ๋ฒผ๋ฃฉ์„ ๋ชจ๋‘ ๊ตฌ๋งคํ•˜์˜€์œผ๋ฏ€๋กœ left์™€ right๋ฅผ ๋ชจ๋‘ +1
      => ๋น„์šฉ์€ [left๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” buy์˜ index - right๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” sale์˜ index| * ๊ตฌ๋งค/ํŒ๋งค ๊ธˆ์•ก
    • buy๊ธˆ์•ก์ด ๋” ํฌ๋‹ค๋ฉด sale์˜ ๋ฒผ๋ฃฉ์„ ๋ชจ๋‘ ํŒ๋งคํ•˜๊ณ  right++
      => ๋น„์šฉ์€ [left๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” buy์˜ index - right๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” sale์˜ index| * ํŒ๋งค ๊ธˆ์•ก
      => buy ๊ธˆ์•ก์—์„œ sale์ด ํŒ๋งคํ•œ ๊ธˆ์•ก๋งŒํผ ๋นผ์„œ ๊ฐฑ์‹ 
    • sale๊ธˆ์•ก์ด ๋” ํฌ๋‹ค๋ฉด buy์˜ ๋ฒผ๋ฃฉ์„ ๋ชจ๋‘ ๊ตฌ๋งคํ•˜๊ณ  left++
      =>  ๋น„์šฉ์€ [left๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” buy์˜ index - right๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” sale์˜ index| * ๊ตฌ๋งค ๊ธˆ์•ก
      => sale ๊ธˆ์•ก์—์„œ buy๊ฐ€ ๊ตฌ๋งคํ•œ ๊ธˆ์•ก๋งŒํผ ๋นผ์„œ ๊ฐฑ์‹ 

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•˜๋ฉด ์ตœ์†Œ ๋น„์šฉ์€ 950์ด ๋ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

// ๋ฒผ๋ฃฉ ์‹œ์žฅ
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';

const input = fs.readFileSync(filePath).toString().split('\n');

const [N] = input[0].split(" ").map(Number);
const info = input[1].split(" ").map(Number);

const buy = [];
const sale = [];

for(let i=0;i<info.length;i++) {
    if(info[i] > 0) {
        sale.push([info[i], i]);
    } else {
        buy.push([info[i], i]);
    }
}

let left = 0;
let right = 0;
let total = 0;

while(left < buy.length && right < sale.length) {
    const diff = Math.abs(buy[left][0] + sale[right][0]);

    if(diff == 0) {
        // ํŒ๋งค ๊ธˆ์•ก === ๊ตฌ๋งค ๊ธˆ์•ก
        total += sale[right][0] * Math.abs(buy[left][1] - sale[right][1]);
        left++;
        right++;
    } else if(buy[left][0] + sale[right][0] < 0) {
        // ๊ตฌ๋งค ๊ธˆ์•ก์ด ๋” ํผ
        total += sale[right][0] * Math.abs(buy[left][1] - sale[right][1]);
        buy[left][0] = diff*-1;
        right++;
    } else if(buy[left][0] + sale[right][0] > 0) {
        // ํŒ๋งค ๊ธˆ์•ก์ด ๋” ํผ
        total += buy[left][0] * -1 * Math.abs(buy[left][1] - sale[right][1]);
        sale[right][0] = diff;
        left++;
    }
}

console.log(total);