๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ฐฝ๊ณ  ๋‹ค๊ฐํ˜• (Java)

by 1mj 2021. 10. 21.

www.acmicpc.net/problem/2304

 

2304๋ฒˆ: ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜•

์ฒซ ์ค„์—๋Š” ๊ธฐ๋‘ฅ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1 ์ด์ƒ 1,000 ์ดํ•˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N ๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„์— ๊ฐ ๊ธฐ๋‘ฅ์˜ ์™ผ์ชฝ ๋ฉด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ L๊ณผ ๋†’์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ H๊ฐ€ ํ•œ ๊ฐœ์˜

www.acmicpc.net


๋ฌธ์ œ ์กฐ๊ฑด์„ ์ฝ์–ด๋ณด๋ฉด '์ง€๋ถ•์˜ ์–ด๋Š ๋ถ€๋ถ„๋„ ์˜ค๋ชฉํ•˜๊ฒŒ ๋“ค์–ด๊ฐ„ ๋ถ€๋ถ„์ด ์—†์–ด์•ผ ํ•œ๋‹ค.'๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ๋‹ค. ์ด ์กฐ๊ฑด์€ ์ปค์กŒ๋‹ค ์ž‘์•„์กŒ๋‹ค ๋‹ค์‹œ ์ปค์งˆ ์ˆ˜ ์—†๋‹ค๋Š” ๋ง์ด๊ณ , ๊ทธ๋Ÿฌ๋ ค๋ฉด ๊ฐ€์žฅ ์ปค์ง€๋Š” ์ตœ๋Œ€ ์ง€์ ์ด ์–ด๋”˜์ง€ ์•Œ์•„์•ผ ํ–ˆ๋‹ค.


์ฒ˜์Œ ์ƒ๊ฐ๋‚ฌ๋˜ ๊ฑด ์™ผ์ชฝ → ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ˜๋ณต์„ ๋Œ๋ฉด์„œ ์ด์ „๋ณด๋‹ค ํฐ ๊ฐ’, ๋˜๋Š” ์ž‘์€ ๊ฐ’์„ ๋ถ„๋ฅ˜ํ•ด์„œ ๊ตฌ๋ถ„ํ•ด๋‚ผ ์˜ˆ์ •์ด์—ˆ์œผ๋‚˜ ๊ฐ€์žฅ ์ปค์ง€๋Š” ์ง€์ ์ด ์–ด๋”˜์ง€ ๋ชฐ๋ผ์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ƒ๊ฐํ•ด๋‚ธ ๋ฐฉ๋ฒ•์€ ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐˆ ์ˆ˜๋ก ์ ์  ๋†’์ด๊ฐ€ ์ž‘์•„์ ธ์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ๊ฐ€์žฅ ๋†’์ด๊ฐ€ ๋†’์€ (8, 10)์„ ๊ฐ€์šด๋ฐ์— ๋‘๊ณ  ๊ฐ€์žฅ ์™ผ์ชฝ์€ (2, 4), ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์€ (15, 8)์ด๋‹ค. ์™ผ์ชฝ, ๊ฐ€์šด๋ฐ, ์˜ค๋ฅธ์ชฝ ์•ฝ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด๋ณผ ๋•Œ ์™ผ์ชฝ๊ณผ ๊ฐ€์šด๋ฐ ์‚ฌ์ด์—๋Š” ์™ผ์ชฝ์—์„œ ๊ฐ€์šด๋ฐ๋กœ ๊ฐˆ ์ˆ˜๋ก ์ ์  ์ปค์ง€๋Š” ๊ฒƒ๋“ค๋งŒ ์ถ”๋ ค๋‚ด์•ผ ํ•˜๊ณ  ๊ฐ€์šด๋ฐ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐˆ ์ˆ˜๋ก ์ ์  ์ž‘์•„์ง€๋Š” ๊ฒƒ๋“ค๋งŒ ์ถ”๋ ค๋‚ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, ์ถ”๋ ค๋‚ผ ๋•Œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์€ ์„ ํƒ๋˜์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.




์ฝ”๋“œ๋ฅผ ์งœ๋ฉด์„œ ๊ณ ๋ คํ•ด๋ด์•ผ ํ•  ์‚ฌํ•ญ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
๊ฐ€์žฅ ํฐ ๊ฐ’์ด ๊ฐ€์šด๋ฐ์— ์žˆ์„์ง€, ์™ผ์ชฝ ๋˜๋Š” ์˜ค๋ฅธ์ชฝ์— ์žˆ์„์ง€ ๊ณ ๋ ค
๊ฐ€์žฅ ํฐ ๊ฐ’์ด ํ•˜๋‚˜๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ๊ณ ๋ ค
๋ชจ๋‘ ๊ฐ’์ด ๊ฐ™์„ ๊ฒฝ์šฐ ๊ณ ๋ ค


์ž๋ฐ”๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

package codingTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Beakjoon2304 {
	
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static List<LH> columnList = new ArrayList<>();
	static String sample =
			"7\r\n" + 
			"2 4\r\n" + 
			"11 4\r\n" + 
			"15 8\r\n" + 
			"4 6\r\n" + 
			"5 3\r\n" + 
			"8 10\r\n" + 
			"13 6";
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		// ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์„ธํŒ…
		BufferedReader input = new BufferedReader(new StringReader(sample));	// ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
		int numberOfColumn = Integer.parseInt(input.readLine());				// ๊ธฐ๋‘ฅ์˜ ๊ฐœ์ˆ˜(N) ์ฝ๊ธฐ
		for (int i = 0; i < numberOfColumn; i++) {								// ์œ„์น˜(L), ๋†’์ด(H) ๋ชฉ๋ก ์ฝ๊ธฐ
			columnList.add(new LH(input.readLine()));
		}
		
		// ์œ„์น˜(L) ๊ธฐ์ค€ ์ •๋ ฌ
		Collections.sort(columnList, new Comparator<LH>() {
			@Override
			public int compare(LH o1, LH o2) {
				// TODO Auto-generated method stub
				Integer L1 = (Integer) o1.getL();
				Integer L2 = (Integer) o2.getL();
				return L1.compareTo(L2);
			}
		});
		
		// ๋‹ค๊ฐํ˜• ๋ฉด์  ์ถœ๋ ฅ
		System.out.println(calcPolygon(numberOfColumn, columnList));
		
	}
	
	static int calcPolygon(int numberOfColumn, List<LH> columnList) {
		// ๋ณ€์ˆ˜ ์„ธํŒ…
		int startH = columnList.get(0).getH();					// ์‹œ์ž‘ ๋†’์ด
		int endH = columnList.get(numberOfColumn - 1).getH();	// ๋ ๋†’์ด
		int area = 0;											// ๋‹ค๊ฐํ˜• ๋„“์ด
		
		// ๋†’์ด ์ตœ๋Œ“๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์„ ๋‚˜๋ˆŒ ๊ฒƒ
		List<Integer> Hlist = new ArrayList<>();
		int maxH = 0;
		for (int i = 0; i < numberOfColumn; i++) {
			Hlist.add(columnList.get(i).getH());
			if (maxH < columnList.get(i).getH()) {
				maxH = columnList.get(i).getH();
			}
		}
		// ๋†’์ด ์ตœ๋Œ“๊ฐ’ ์ธ๋ฑ์Šค (์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ์ตœ์†Œ, ์ตœ๋Œ€)
		int index = Hlist.indexOf(maxH);
		int lastIndex = Hlist.lastIndexOf(maxH);
		
		// ์‹œ์ž‘๊ฐ’ - ์ตœ๋Œ“๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” ๊ฐ’ ๊ณ ๋ฅด๊ธฐ
		// ์ตœ๋Œ“๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ์™ผ์ชฝ ์˜์—ญ, ์ปค์ง€๋Š” ๊ฐ’
		List<LH> leftList = new ArrayList<>();
		int temp = startH;
		for (int i = 0; i < index + 1; i++) {
			LH current = columnList.get(i);
			if (temp < current.getH()) {
				temp = current.getH();
			}

			if ((current.getH() >= startH) && (current.getH() <= maxH)) {
				if (temp <= current.getH()) {
					leftList.add(current);
				}
			}
		}
		// ์™ผ์ชฝ ๋„“์ด ๊ตฌํ•˜๊ธฐ
		for (int i = 0; i < leftList.size() - 1; i++) {
			area += ((leftList.get(i + 1).getL() - leftList.get(i).getL()) * leftList.get(i).getH());
		}
		
		// ์ตœ๋Œ“๊ฐ’ ๊ณ ๋ฅด๊ธฐ
		List<LH> maxList = new ArrayList<>();
		for (int i = index; i < lastIndex + 1; i++) {
			LH current = columnList.get(i);
			if (current.getH() == maxH) {
				maxList.add(current);
			}
		}
		// ์ตœ๋Œ“๊ฐ’ ๋„“์ด ๊ตฌํ•˜๊ธฐ (ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ *1, ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ ๊ทธ ๋„“์ด๋งŒํผ)
		if (maxList.size() == 1) {
			area += (1 * maxList.get(0).getH());
		} else {
			area += (((maxList.get(maxList.size() - 1).getL() + 1) - maxList.get(0).getL()) * maxList.get(0).getH());
		}
		
		// ์ตœ๋Œ“๊ฐ’ - ๋๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” ๊ฐ’ ๊ณ ๋ฅด๊ธฐ
		List<LH> rightList = new ArrayList<>();
		temp = endH;
		for (int i = lastIndex; i < numberOfColumn; i++) {
			LH current = columnList.get(i);
			if (temp > current.getH()) {
				temp = current.getH();
			}
			
			if ((current.getH() >= endH) && (current.getH() <= maxH)) {
				if (temp <= current.getH()) {
					rightList.add(current);
				}
			}
		}
		// ์˜ค๋ฅธ์ชฝ ๋„“์ด ๊ตฌํ•˜๊ธฐ
		for (int i = 0; i < rightList.size() - 1; i++) {
			area += (((rightList.get(i + 1).getL() + 1) - (rightList.get(i).getL() + 1)) * rightList.get(i + 1).getH());
		}
		
		return area;
	}

}

class LH {
	private int L;
	private int H;
	
	public LH(String text) {
		super();
		String[] temp = text.split(" ");
		L = Integer.parseInt(temp[0]);
		H = Integer.parseInt(temp[1]);
	}
	
	public int getL() {
		// TODO Auto-generated method stub
		return L;
	}
	
	public int getH() {
		// TODO Auto-generated method stub
		return H;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "LH [L=" + L + ", H=" + H + "]\n";
	}
}


์ฝ”๋“œ๋ฅผ ์งœ๋‹ค๋ณด๋‹ˆ ์ƒ๊ฐํ•œ๋Œ€๋กœ ํ•˜๊ธฐ์—๋Š” ์•ฝ๊ฐ„ ๋ฌด๋ฆฌ๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์ด ๋Š๊ปด์กŒ๋‹ค.
ํ›จ์”ฌ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๊ฐ€ ๋งŽ๊ธฐ๋„ ํ•ด์„œ ์ฐพ์•„์„œ ์ฝ์–ด๋ดค๋Š”๋ฐ ์ ‘๊ทผ๋ฒ•์€ ๋น„์Šทํ–ˆ๋‹ค.

์™ผ์ชฝ, ๊ฐ€์šด๋ฐ, ์˜ค๋ฅธ์ชฝ์„ ๋”ฐ๋กœ ๋‚˜๋ˆ ์„œ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋‹ค๊ฐ€ ์ตœ๋Œ“๊ฐ’์—์„œ ์ค‘๋‹จ, ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜๋Œ€๋กœ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋‹ค๊ฐ€ ์ตœ๋Œ“๊ฐ’์—์„œ ์ค‘๋‹จํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด๋ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€