Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[Meet in the middle] ๋ฐฑ์ค€ 1450 ๋ƒ…์ƒ‰ ๋ฌธ์ œ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[Meet in the middle] ๋ฐฑ์ค€ 1450 ๋ƒ…์ƒ‰ ๋ฌธ์ œ

WonderJay 2023. 3. 6. 13:31
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

1450๋ฒˆ: ๋ƒ…์ƒ‰๋ฌธ์ œ

์ฒซ์งธ ์ค„์— N๊ณผ C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์€ 30๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜, C๋Š” 109๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์— ๋ฌผ๊ฑด์˜ ๋ฌด๊ฒŒ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌด๊ฒŒ๋„ 109๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ์—๋Š” N ์ด ๋„ˆ๋ฌด ํฐ ๊ฒฝ์šฐ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋•Œ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ตœ์ ํ™”๋ฅผ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ,

 

๋ถ„ํ• ์ •๋ณต์ฒ˜๋Ÿผ N ์„ ์ ˆ๋ฐ˜์”ฉ ๋‚˜๋ˆ ์„œ ์—ฐ์‚ฐํ•œ ๋‹ค์Œ์— ๊ฒฐ๊ณผ๋ฅผ ํ•ฉ์ณ์„œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์„ 

 

meet in the middle ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•œ๋‹ค. 

 

๋‹ค๋งŒ, ์žฌ๊ท€์ ์œผ๋กœ ๋ฐ˜๋ณตํ•ด์„œ N/2, N/4 ... 1 ์ฒ˜๋Ÿผ ๊ณ„์† ์ชผ๊ฐœ๋Š” ๊ฒŒ ์•„๋‹ˆ๊ณ 

 

๋งจ ์ฒ˜์Œ์— 1ํšŒ๋งŒ ๋ฐ˜์œผ๋กœ ์ชผ๊ฐœ๋Š” ๊ฒƒ์ด๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด ๊ธธ์ด๊ฐ€ 40 ์ธ ์ˆ˜์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ˆ˜์—ด๋“ค์„ ์ด๋ฃจ๋Š” ์›์†Œ๋“ค ์ค‘ ๋ช‡ ๊ฐœ๋ฅผ 

 

์ค‘๋ณตํ•ด์„œ ๋”ํ•œ ๊ฐ’์ด ํŠน์ • ๊ฐ’์ด ๋˜๋„๋ก ํ•˜๋Š” ์กฐํ•ฉ์ด ์–ผ๋งˆ๋‚˜ ์กด์žฌํ•˜๋Š” ์ง€ ์•Œ์•„๋‚ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

 

๊ทธ๋Ÿฌ๋ฉด, ๋ชจ๋“  ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ i ๋ฒˆ์งธ ์›์†Œ๋ฅผ ํฌํ•จํ•  ์ง€ ๋ง ์ง€๋ฅผ ๊ฒฐ์ •ํ•ด์ฃผ๋ฉด ๋˜์ง€๋งŒ

 

ํ•ด๋‹น ๋ฐฉ๋ฒ•์€ 2^40 ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๊ณ  ์ด๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์ด๋ฅผ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ธธ์ด๊ฐ€ 40 ์ธ ์ˆ˜์—ด์„ ์ ˆ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„์–ด๋ณด์ž.

 

๊ฐ๊ฐ์˜ ์ˆ˜์—ด์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 2^20 ์ด๋ฏ€๋กœ 2^20*2 ~= 2^21 ๋งŒํผ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค.

 

๊ทผ๋ฐ ๋‚˜๋ˆ„์–ด์„œ ์–ด๋–ป๊ฒŒ ํ•˜๋ ค๋Š” ๊ฑธ๊นŒ?

 

๋ฐœ์ƒ์€ ๊ฐ„๋‹จํ•˜๋‹ค.

 

0~n/2 ๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ lv, n/2~n ๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ rv ๋ผ๊ณ  ํ•˜์ž.

 

๊ทธ๋Ÿฌ๋ฉด lv ์˜ ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ target -  lv[i] ๊ฐ€ rv ์— ๋ช‡ ๊ฐœ ์กด์žฌํ•˜๋Š” ์ง€ ๊ตฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์ด๋ถ„ ํƒ์ƒ‰์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค.

 

์ •๋ฆฌํ•˜๋ฉด meet in the middle ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ n^m ์˜ ์—ฐ์‚ฐ์„ 2*n^(m-1) ์˜ ์—ฐ์‚ฐ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

 

 ๋ฌธ์ œ์— ์ ์šฉํ•ด๋ณด์ž.

 

๋ƒ…์ƒ‰ ๋ฌธ์ œ(๋ฐฑ์ค€ 1450)๋Š” ์ „ํ˜•์ ์ธ meet in the middle ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

N ๊ฐœ์˜ ๋ฌผ๊ฑด์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ตœ๋Œ€ C ๋งŒํผ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์„ ๋•Œ

N ๊ฐœ์˜ ๋ฌผ๊ฑด์„ ๊ฐ€๋ฐฉ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์•„๋‚ด์•ผ ํ•œ๋‹ค.

 

์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

 

N <= 30 ์ด๋ฏ€๋กœ ์™„์ „ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ตœ๋Œ€ 2^30 ~= 1073741824 ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค.

 

๋Œ€์ถฉ 10 ์ดˆ ์ •๋„๊ฐ€ ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™์€๋ฐ, ์œ„ ๋ฌธ์ œ์˜ ์‹œ๊ฐ„ ์ œํ•œ์€ 1์ดˆ์ด๋ฏ€๋กœ ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

๋งŒ์•ฝ์— meet in the middle ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ?

 

2^15*2 ~=  65536 ๋ฒˆ์˜ ์—ฐ์‚ฐ๊ณผ ์ด๋ถ„ํƒ์ƒ‰(nlogn)์— ์˜ํ•ด

์ด 65536 + 65536 * long(65536) ~= 65536 + 5 * 65536 ~=  393216 ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ถ”์ธกํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฆ‰, meet in the middle ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์œ„ ๋ฌธ์ œ๋ฅผ ์‹œ๊ฐ„์ œํ•œ 1์ดˆ ์•ˆ์— ๋„‰๋„‰ํ•˜๊ฒŒ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•จ์„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๊ตฌํ˜„ํ•˜๊ธฐ ์ „์— ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ•  ์ง€ ์„ค๊ณ„๋ฅผ ํ•ด๋ณด์ž.

 

n ๊ฐœ์˜ ๋ฐฐ์—ด ์ค‘์—์„œ i ๊ฐœ๋ฅผ ์„ ํƒํ•˜์—ฌ ๋”ํ•œ ๊ฒƒ์ด c ์ดํ•˜์ธ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๊ณ 

 

์ด๋ฅผ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” meet in the middle ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

๊ธธ์ด๊ฐ€ n ์ธ arr ๋ฐฐ์—ด์˜ 0~n/2 ๊นŒ์ง€์˜ ์›์†Œ๋“ค๋กœ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„ ํ•ฉ์˜ ๋ฐฐ์—ด์„ lv

 

๊ธธ์ด๊ฐ€ n ์ธ arr ๋ฐฐ์—ด์˜ n/2~n ๊นŒ์ง€์˜ ์›์†Œ๋“ค๋กœ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„ ํ•ฉ์˜ ๋ฐฐ์—ด์„ rv ๋ผ๊ณ  ํ•˜์ž.

 

๋ถ€๋ถ„ํ•ฉ์„ ๊ตฌํ•˜๋ ค๋ฉด dfs ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

private static void dfs(int s, int e, long sum, ArrayList<Long> v) {
    if (s >= e) {
        v.add(sum);
        return;
    }
    dfs(s + 1, e, sum, v);
    dfs(s + 1, e, sum + arr[s], v);
}

์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์‹œ์ž‘์ ๊ณผ ๋ ์  ๊ทธ๋ฆฌ๊ณ  ๋ถ€๋ถ„ํ•ฉ์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

s>=e ์ธ ๊ฒฝ์šฐ (์œ ํšจํ•œ ๊ฒฝ์šฐ) ์—๋Š” ๋ฐฐ์—ด์— ๋ถ€๋ถ„ํ•ฉ์„ add ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  s ๋ฒˆ์งธ ์›์†Œ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ dfs(s+1, e, sum, v) 

s ๋ฒˆ์งธ ์›์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ dfs(s+1, e, sum+arr[s], v) ๋ฅผ ํƒ์ƒ‰ํ•ด์ค˜์„œ ๋ถ€๋ถ„ํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

๋ฉ”์ธ ํ•จ์ˆ˜์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์—ฌ lv, rv ๋ฅผ update ํ•  ์ˆ˜ ์žˆ๋‹ค.

dfs(0, n / 2, 0, lv);
dfs(n / 2, n, 0, rv);

lv ์™€ rv ๋Š” ๊ฐ๊ฐ 0~n/2 ,  n/2~n ๊นŒ์ง€์˜ ์›์†Œ๋“ค๋กœ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„ํ•ฉ์ด ์›์†Œ๋กœ ๋‹ด๊ฒจ์žˆ๋‹ค.

 

์ด์ œ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

 

์šฐ๋ฆฌ๊ฐ€ ํ•ด์•ผ ํ•  ๊ฒƒ์€ n ๊ฐœ์˜ ์›์†Œ ์ค‘์—์„œ i ๊ฐœ๋ฅผ ์„ ํƒํ•ด์„œ ๋”ํ•œ ๊ฒƒ์ด c ์ดํ•˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์ž‘์—…์ด๋‹ค.

 

rv ์˜ ์›์†Œ๋ฅผ ์ˆœ์ฐจ ํƒ์ƒ‰ํ•œ๋‹ค. ์ด๋•Œ ์›์†Œ๋ฅผ ele ๋ผ๊ณ  ํ•˜์ž.

 

ele + lv[i] ๊ฐ€ c ์ดํ•˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ชจ์กฐ๋ฆฌ ๋”ํ•ด์ฃผ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ?

 

์ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ถ„ํƒ์ƒ‰์„ ๊ตฌํ˜„ํ•˜์ž.

 

๋จผ์ € lv ๋ฅผ ์ •๋ ฌํ•œ๋‹ค. (์ด๋ถ„ ํƒ์ƒ‰์€ ๋ฐฐ์—ด์ด ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ์ ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.)

 

๊ทธ๋ฆฌ๊ณ  rv ์˜ ๊ฐ๊ฐ์˜ ์›์†Œ ele ์— ๋Œ€ํ•˜์—ฌ ele + lv[mid] ๊ฐ€ c ์ดํ•˜์ธ ๊ฒฝ์šฐ๋ฅผ answer ๋ณ€์ˆ˜์— ๋ˆ„์ ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

 

Collections.sort(lv);
long ans = 0;
for (long ele : rv) {
    if (ele > c)
        continue;
    int left = 0;
    int right = lv.size();
    int mid = 0;
    while (left < right) {
        mid = (left + right) / 2;
        if(ele + lv.get(mid) > c)
            right = mid;
        else
            left = mid+1;
    }
    ans += right;
}

 

์ด๋ถ„ ํƒ์ƒ‰์€ ํ•ญ์ƒ ํ—ท๊ฐˆ๋ฆฐ๋‹ค..

 

์ด๋ถ„ ํƒ์ƒ‰์€ ์กฐ๋งŒ๊ฐ„ ํ•œ๋ฒˆ ๊ฒŒ์‹œ๋ฌผ๋กœ ์ •๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค.

 

์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

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

public class Main {

    static int n;
    static int c;
    static int[] arr;
    static ArrayList<Long> lv = new ArrayList<>();
    static ArrayList<Long> rv = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        stk = new StringTokenizer(br.readLine());
        n = Integer.parseInt(stk.nextToken());
        c = Integer.parseInt(stk.nextToken());
        arr = new int[n];

        stk = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(stk.nextToken());
        }
        dfs(0, n / 2, 0, lv);
        dfs(n / 2, n, 0, rv);

        Collections.sort(lv);
        long ans = 0;
        for (long ele : rv) {
            if (ele > c)
                continue;
            int left = 0;
            int right = lv.size();
            int mid = 0;
            while (left < right) {
                mid = (left + right) / 2;
                if(ele + lv.get(mid) > c)
                    right = mid;
                else
                    left = mid+1;
            }
            ans += right;
        }
        bw.write(ans+"\n");
        bw.flush();
        bw.close();
    }

    private static void dfs(int s, int e, long sum, ArrayList<Long> v) {
        if (s >= e) {
            v.add(sum);
            return;
        }
        dfs(s + 1, e, sum, v);
        dfs(s + 1, e, sum + arr[s], v);
    }
}


์ฐธ๊ณ 

https://killerwhale0917.tistory.com/5

 

Meet in the middle / ์ค‘๊ฐ„์—์„œ ๋งŒ๋‚˜๊ธฐ

1. ๊ด€๋ จ ๋ฌธ์ œ https://www.acmicpc.net/problem/1208 1208๋ฒˆ: ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ 2 ์ฒซ์งธ ์ค„์— ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N๊ณผ ์ •์ˆ˜ S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 40, |S| ≤ 1,000,000) ๋‘˜์งธ ์ค„์— N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘

killerwhale0917.tistory.com

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments