Algorithm

[๋ฐฑ์ค€ 1940] ์ฃผ๋ชฝ

osean 2023. 4. 17. 20:05

๋ฌธ์ œ

 

1940๋ฒˆ: ์ฃผ๋ชฝ

์ฒซ์งธ ์ค„์—๋Š” ์žฌ๋ฃŒ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 15,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ฐ‘์˜ท์„ ๋งŒ๋“œ๋Š”๋ฐ ํ•„์š”ํ•œ ์ˆ˜ M(1 ≤ M ≤ 10,000,000) ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์…‹์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์žฌ๋ฃŒ๋“ค์ด ๊ฐ€์ง„ ๊ณ 

www.acmicpc.net

์ฃผ๋ชฝ์€ ์ฒ ๊ธฐ๊ตฐ์„ ์–‘์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ์ ํŠธ์— ๋‚˜์„ฐ๋‹ค. ๊ทธ๋ž˜์„œ ์•ผ์ฒ ๋Œ€์žฅ์„ ํ†ตํ•ด ์ฒ ๊ธฐ๊ตฐ์ด ์ž…์„ ๊ฐ‘์˜ท์„ ๋งŒ๋“ค๊ฒŒ ํ•˜์˜€๋‹ค. ์•ผ์ฒ ๋Œ€์žฅ์€ ์ฃผ๋ชฝ์˜ ๋ช…์— ๋”ฐ๋ฅด๊ธฐ ์œ„ํ•˜์—ฌ ์—ฐ๊ตฌ์— ์ฐฉ์ˆ˜ํ•˜๋˜ ์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์‚ฌ์‹ค์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

๊ฐ‘์˜ท์„ ๋งŒ๋“œ๋Š” ์žฌ๋ฃŒ๋“ค์€ ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฐ‘์˜ท์€ ๋‘ ๊ฐœ์˜ ์žฌ๋ฃŒ๋กœ ๋งŒ๋“œ๋Š”๋ฐ ๋‘ ์žฌ๋ฃŒ์˜ ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋ฅผ ํ•ฉ์ณ์„œ M(1 ≤ M ≤ 10,000,000)์ด ๋˜๋ฉด ๊ฐ‘์˜ท์ด ๋งŒ๋“ค์–ด ์ง€๊ฒŒ ๋œ๋‹ค. ์•ผ์ฒ ๋Œ€์žฅ์€ ์ž์‹ ์ด ๋งŒ๋“ค๊ณ  ์žˆ๋Š” ์žฌ๋ฃŒ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ‘์˜ท์„ ๋ช‡ ๊ฐœ๋‚˜ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ์ด๋Ÿฌํ•œ ๊ถ๊ธˆ์ฆ์„ ํ’€์–ด ์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ N(1 ≤ N ≤ 15,000) ๊ฐœ์˜ ์žฌ๋ฃŒ์™€ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ช‡ ๊ฐœ์˜ ๊ฐ‘์˜ท์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ฒซ์งธ ์ค„์—๋Š” ์žฌ๋ฃŒ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 15,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ฐ‘์˜ท์„ ๋งŒ๋“œ๋Š”๋ฐ ํ•„์š”ํ•œ ์ˆ˜ M(1 ≤ M ≤ 10,000,000) ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์…‹์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์žฌ๋ฃŒ๋“ค์ด ๊ฐ€์ง„ ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋“ค์ด ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋Š” 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

ํ’€์ด

 

๊ณต์œ  ์†Œ์Šค ๋ณด๊ธฐ

 

www.acmicpc.net

์•„์ด๋””์–ด

  • n : ์žฌ๋ฃŒ์˜ ์ˆ˜
  • m : ๊ฐ‘์˜ท์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์žฌ๋ฃŒ ๊ณ ์œ ๋ฒˆํ˜ธ์˜ ํ•ฉ
  • i : ์žฌ๋ฃŒ ๊ณ ์œ ๋ฒˆํ˜ธ
  • r : m - i ํ•œ ๋‚˜๋จธ์ง€ ์žฌ๋ฃŒ ๊ณ ์œ ๋ฒˆํ˜ธ
    • m ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” 10,000,000 ์ด๊ณ  i ์˜ ์ตœ์†Œ ํฌ๊ธฐ๋Š” 1 ์ด๋‹ค.
    • ๋•Œ๋ฌธ์— 15,000 < r < 9,999,999 ์ผ ์ˆ˜ ์žˆ๋‹ค.
    • ๋”ฐ๋ผ์„œ ์žฌ๋ฃŒ๋ฅผ ๋‹ด๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” ์ตœ์†Œ 10,000,000 ์—ฌ์•ผ ํ•œ๋‹ค.
#include <bits/stdc++.h>

using namespace std;

long long n, m, cnt = 0L;
long long arr[10000010] = {};

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        long long input = 0L;
        cin >> input;
        arr[input]++;
    }

    int size = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < size; i++) {
        long r = m - i;
        if (r < 0) continue;
        if (arr[i] == 0 || arr[r] == 0) continue;
        if (i == r) continue;
        arr[i] = 0;
        arr[r] = 0;
        cnt++;
    }
    cout << cnt;
}

ํ’€์ด - 2

 

๊ณต์œ  ์†Œ์Šค ๋ณด๊ธฐ

 

www.acmicpc.net

์•„์ด๋””์–ด

  • ์ˆœ์„œ์— ๊ด€๊ณ„์—†์ด 2๊ฐœ๋ฅผ ๋”ํ•œ ๊ฐ’์ด m ์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š”๋‹ค.
    • nC2, ์กฐํ•ฉ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ์žฌ๋ฃŒ์˜ ์ˆ˜๋งŒํผ ์ ์ง„์ ์œผ๋กœ ํ˜„์žฌ ์žฌ๋ฃŒ์™€ ๋‹ค์Œ ์žฌ๋ฃŒ๋ฅผ ๋”ํ•œ ํ›„ ๊ฐ’์ด m ๊ณผ ๋™์ผํ•˜๋‹ค๋ฉด ์นด์šดํŠธํ•œ๋‹ค.

๊ฒฐ๊ณผ

  • ๋ฉ”๋ชจ๋ฆฌ : 2080 KB
  • ์‹œ๊ฐ„ : 96 ms
#include <bits/stdc++.h>

using namespace std;

int n, m = 0;
int arr[15010] = {};

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    if (200000 <= m) cout << 0;
    else {
        int cnt = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (arr[i] + arr[j] == m) cnt++;
            }
        }
        cout << cnt;
    }
}