SW Expert Academy - 1208. Flatten

1 minute read

๋ฌธ์ œ๋งํฌ

๋ฌธ์ œ์„ค๋ช…

  • ํ‰ํƒ„ํ™”๋Š” ๋†’์€ ๊ณณ์˜ ์ƒ์ž๋ฅผ ๋‚ฎ์€ ๊ณณ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ๊ฐ„๊ฒฉ์„ ์ค„์ด๋Š” ์ž‘์—…์„ ์˜๋ฏธํ•œ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์ด๊ฐ€ ๋†’์€ ๊ณณ์˜ ์ƒ์ž๋ฅผ ๊ฐ€์žฅ ๋†’์ด๊ฐ€ ๋‚ฎ์€ ๊ณณ์˜ ์ƒ์ž๋กœ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ๋คํ”„๋ผ๊ณ  ํ•œ๋‹ค.
  • ๋คํ”„ ํšŸ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋คํ”„๋ฅผ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚œ ํ›„ ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ์ฐจ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ
    • ๋คํ”„ ํšŸ์ˆ˜ ์ด๋‚ด์— ํ‰ํƒ„ํ™”(์ตœ๋Œ€ ๋†’์ด ์ฐจ๊ฐ€ 0 ์ด๋‚˜ 1์ธ ๊ฒฝ์šฐ)๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๊ทธ ๋•Œ์˜ ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ๋†’์ด ์ฐจ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ

๋ฌธ์ œ์ ‘๊ทผ

  • vector<int> height ์— ๋ชจ๋“  ์ƒ์ž์˜ ๋†’์ด ์ •๋ณด๋ฅผ ๋‹ด์€ ํ›„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ์ตœ๊ณ ์ ๊ณผ ์ตœ์ €์ ์˜ ๋†’์ด ์ฐจ๋ฅผ ๊ตฌํ•œ๋‹ค.
  • ๋†’์ด ์ฐจ๊ฐ€ 0 ๋˜๋Š” 1์ธ ๊ฒฝ์šฐ ํ‰ํƒ„ํ™”๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค.
  • ์œ„ ๊ณผ์ •์„ ๋คํ”„ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

๊ตฌํ˜„

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(int argc, char** argv)
{
	int test_case;
	int T = 10;
	freopen("input.txt", "r", stdin);
	for (test_case = 1; test_case <= T; ++test_case)
	{
		vector<int> heightInfo;
		int dumpNum, diff;
		scanf("%d", &dumpNum);
		int height;
		for (int row = 0; row < 100; row++) {
			scanf("%d", &height);
			heightInfo.push_back(height);
		}
		
		for (int cnt = 0; cnt <= dumpNum; cnt++) {
			// ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
			sort(heightInfo.begin(), heightInfo.end());
			diff = heightInfo[heightInfo.size() - 1] - heightInfo[0];
			if (diff == 0 || diff == 1) break;
			// dump
			heightInfo[0]++;
			heightInfo[heightInfo.size() - 1]--;
			
		}
		printf("#%d %d\n", test_case, diff);
	}
	return 0;//์ •์ƒ์ข…๋ฃŒ์‹œ ๋ฐ˜๋“œ์‹œ 0์„ ๋ฆฌํ„ดํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
}

Categories:

Updated:

Leave a comment