Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6-wnsmir #26

Merged
merged 3 commits into from
Nov 24, 2024
Merged

6-wnsmir #26

merged 3 commits into from
Nov 24, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Nov 12, 2024

πŸ”— 문제 링크

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

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h

✨ μˆ˜λ„ μ½”λ“œ

제일 λ¨Όμ € ν•΄μ•Όν•  μž‘μ—…μ€ λ†’μ΄μ„€μ •μž…λ‹ˆλ‹€.
μ²˜μŒμ—” λ‹¨μˆœν•˜κ²Œ 제일 많이 λ‚˜μ˜¨ land값이 졜적의 높이라고 μƒκ°ν–ˆλŠ”λ°
222
223
333
같은 κ²½μš°μ—” 2κ°€ κ°€μž₯ λ§Žμ€ 경우의 μˆ˜μ§€λ§Œ, 3으둜 λ§žμΆ”λ©΄ 5개만 올렀주면 되기 λ•Œλ¬Έμ—
μ˜¬λ¦΄λ•Œμ˜ μ‹œκ°„ 1초 * 5 -> 5초
2둜 λ§žμΆ˜λ‹€λ©΄ 2초 * 4 -> 8초
즉 3이 졜적의 높이가 λ©λ‹ˆλ‹€.

λ”°λΌμ„œ 브루트포슀둜 문제λ₯Ό ν‘ΈλŠ”κ²ƒμ΄ κ°€μž₯ μ ν•©ν•˜λ‹€κ³  κ²°λ‘  λ‚΄λ ΈμŠ΅λ‹ˆλ‹€.
λΈŒλ£¨νŠΈν¬μŠ€λŠ” 각 높이에 λŒ€ν•΄ μ§„ν–‰ν•΄μ€λ‹ˆλ‹€. 즉 졜적의 높이λ₯Ό 정해두고,
μ§€ν˜•μ˜ λͺ¨λ“  μ’Œν‘œλ₯Ό μˆœνšŒν•˜λ©° ν˜„μž¬ 높이와 λͺ©ν‘œλ†’μ΄μ˜ μ°¨λ₯Ό μ΄μš©ν•΄ 인벀토리에 λ“€μ–΄κ°ˆμ§€, μΈλ²€ν† λ¦¬μ—μ„œ 뺄지λ₯Ό μ •ν•˜κ³ , 그에따λ₯Έ μ‹œκ°„λ„ μ—…λ°μ΄νŠΈν•΄μ€λ‹ˆλ‹€.

이 κ³Όμ •μ—μ„œ 초기 인벀토리 블둝 수 + μ œκ±°ν•œ 블둝 수 - μΆ”κ°€ν•œ 블둝 수 κ°€ μŒμˆ˜κ°€ λ‚˜μ˜¨λ‹€λ©΄ κ·Έ λ‹¨κ³„μ˜ λ†’μ΄λŠ” λΆˆκ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— λ‹€μŒλ‹¨κ³„λ‘œ λ„˜μ–΄κ°‘λ‹ˆλ‹€.

μ—¬κΈ°μ„œ κ°€μž₯ μž‘μ€ μ‹œκ°„μœΌλ‘œ μ—…λ°μ΄νŠΈ ν• λ•Œμ˜ 높이λ₯Ό 졜적의 λ†’μ΄λ‘œ μ„€μ •ν•˜κ³  λ§Œμ•½ μ‹œκ°„μ΄ κ°™μ•„λ©΄, λ†’μ΄λ§Œ μ—…λ°μ΄νŠΈ ν•΄μ€λ‹ˆλ‹€.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

브루트포슀둜 λΆ„λ₯˜λœ λ¬Έμ œλΌμ„œ μ‰¬μš΄κ±΄μ€„ μ•Œμ•˜λŠ”λ° μ•„λ‹ˆλ„€μš”..

λ§ˆμŒμ†μœΌλ‘œ λΈŒλ£¨νŠΈν¬μŠ€λŠ” 쉽지 λΌλŠ” 생각이 μžˆμ—ˆλŠ”λ°,
쑰금 λ°˜μ„±ν•˜κ²Œ λ˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€...

μ–΄λ–€ 것을 κΈ°μ€€μœΌλ‘œ 브루트포슀 해야할지 감이 μ•ˆμž‘ν˜€μ„œ μ€€μš©λ‹˜ μ½”λ“œ λ΄€μŠ΅λ‹ˆλ‹€.
κ°€λŠ₯ν•œ 높이λ₯Ό 0~256으둜 μž‘λŠ” 것을 μ•Œμ•˜κ³ ,
직접 κ΅¬ν˜„ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

κ°€λŠ₯ν•œ 높이λ₯Ό κΈ°μ€€μœΌλ‘œ 브루트포슀둜 κ²€μƒ‰ν•œλ‹€λŠ” 것을 μ•Œκ²Œλ˜λ‹ˆκΉŒ,
κ·Έλž˜λ„ λ‚˜λ¦„ 잘 ν’€λ¦¬λ”κ΅°μš”.

이번 문제λ₯Ό ν’€λ©΄μ„œ λΈŒλ£¨νŠΈν¬μŠ€ν•  λ³€μˆ˜ 같은 것을 잘 μ •ν•˜λŠ” 것이 μ€‘μš”ν•˜λ‹€λŠ” 것을 μ•Œκ²Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.
쒋은 문제 κ°μ‚¬ν•©λ‹ˆλ‹€.

μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.
(μ½”λ“œλ₯Ό 살짝살짝 λ¦¬νŒ©ν† λ§ν–ˆλŠ”λ° κ²°κ΅­ μ€€μš©λ‹˜ μ½”λ“œλž‘ μ™„μ „νžˆ λΉ„μŠ·ν•΄μ Έλ²„λ Έλ„€μš”...)

CPP CODE
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define PLACE 1
#define REMOVE 2

using namespace std;

int main() {
    int N, M, B;
    cin >> N >> M >> B;

    vector<vector<int>> field(N, vector<int>(M));
    for (int y = 0; y < N; y++) 
        for (int x = 0; x < M; x++) 
            cin >> field[y][x];

    int bestHeight = 0;
    int minTime = INT_MAX;
    int height = -1;

    while (height++ < 257) {
        int remove = 0;
        int place = 0;

        for (int y = 0; y < N; y++) {
            for (int x = 0; x < M; x++) {
                int diff = field[y][x] - height;
                if (diff > 0) remove += diff;
                else place -= diff;
            }
        }

        if (remove + B >= place) {
            int time = remove * REMOVE + place * PLACE;
            minTime = min(time, minTime);
            if (time == minTime && height > bestHeight)
                bestHeight = height;
        }
    }

    cout << minTime << ' ' << bestHeight;

    return 0;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맀번 μž¬λ°ŒλŠ” 문제λ₯Ό κ°€μ Έμ˜€μ‹œλŠ”κ±° κ°™λ„€μš” ! 이번 λ¬Έμ œλŠ” λ­”κ°€ 풀릴듯 λ§λ“―ν•΄μ„œ λκΉŒμ§€ μ§‘μš”ν•˜κ²Œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€. 1μ‹œκ°„μ΄ μ’€ λ„˜κ²Œ κ±Έλ Έλ„€μš”

문제λ₯Ό 보면 닡이 μ—¬λŸ¬κ°œλΌλ©΄ κ°€μž₯ 높은 높이λ₯Ό κ³ λ₯Έλ‹€. κ°€ μžˆλŠ”λ° 첨엔 λ¬΄μŠ¨λ§μΈμ§€ λͺ°λΌμ„œ κ·Έλƒ₯ λ„˜κ²¨λ²„λ ΈλŠ”λ° 이것 λ•Œλ¬Έμ— 30뢄을 더 μ“΄ κ±° κ°™μ•„μš”. μ€€μš©λ‹˜ μ½”λ“œλž‘μ€ 접근이 많이 λ‹¬λΌμ„œ μ΄ν•΄ν•˜κΈ° μ–΄λ €μšΈκ±° κ°™μ•„ 주석을 μ’€ λ‚¨κΈ°κ² μŠ΅λ‹ˆλ‹€.!

code
#include <iostream>
#include <climits>
using namespace std;

#define MAX	500

int map[MAX][MAX] = { 0, };
int baseMap[MAX][MAX] = { 0, };
int n, m, b;
int t = 0;
int tmp, inven;
int minHeight = INT_MAX;
int height = INT_MIN, timeTaken = INT_MAX;

bool isFlat() {
	int k = baseMap[0][0];
	for (int y = 0; y < m; y++) {
		for (int x = 0; x < n; x++) {
			if (k != baseMap[y][x]) return false;
		}
	}
	return true;
}

// ν•œ 측을 μ˜¬λ €κ°€λ©΄μ„œ μ‹œκ°„μ„ μ„€μ •ν•˜κ³  μΈλ²€ν† λ¦¬μ—μ„œ item을 λΊ€λ‹€.
void update() {
	for (int y = 0; y < m; y++) {
		for (int x = 0; x < n; x++) {
			if (map[y][x] > baseMap[y][x]) t -= 2;
			else t += 1;
			baseMap[y][x]++;
			inven--;
		}
	}
}

void read() {
	for (int y = 0; y < m; y++) {
		for (int x = 0; x < n; x++) {
			cin >> tmp;
			map[y][x] = tmp;
			inven += tmp;
			t += tmp * 2;
			if (minHeight > tmp) minHeight = tmp;
		}
	}
}

int main() {
	//μΈλ²€ν† λ¦¬μ˜ ν¬κΈ°λŠ” 64,000,000 = 500 x 500 x 256 μž„μ— μœ μ˜ν•˜μž!
	//λͺ¨λ“  λΈ”λŸ­μ„ λ‹€ λΉΌκ³  μ›λž˜ 있던 μžλ¦¬μ— λ„£μœΌλ©΄ μ‹œκ°„μ€ +1이 μ•„λ‹ˆλΌ -2이닀.
	//isFlatν•˜λ©΄ μ΅œμ†Œ μ‹œκ°„μ„ μ΄ˆκΈ°ν™”ν•œλ‹€.
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m >> b;
	inven = b;
	read();

	//minHeightλΆ€ν„° ν•œμΈ΅μ”© μŒ“μ•„ 올라감
	for (int y = 0; y < m; y++) {
		for (int x = 0; x < n; x++) {
			baseMap[y][x] = minHeight;
			t -= minHeight * 2;
			inven -= minHeight;
		}
	}
	
	while (inven >= 0) {
		if (isFlat()) {
			if (t >= 0 && timeTaken > t || timeTaken == t && height < baseMap[0][0]) {
				timeTaken = t;
				height = baseMap[0][0];
			}
			if (inven == 0) break;
		}
		update();
	}

	cout << timeTaken << ' ' << height;
	return 0;
}

Comment on lines +5 to +8
# κ°€λŠ₯ν•œ 높이 hλ₯Ό 0μ—μ„œ 256κΉŒμ§€ 순회
for h in range(257):
remove_blocks = 0
add_blocks = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ΄λ ‡κ²Œ 맀번 κ°€λŠ₯ν•œ 높이에 λŒ€ν•΄μ„œλ§Œ 연산을 ν•˜λ©΄ map이 flatν•œμ§€ 검사λ₯Ό μ•ˆν•΄λ„ λ˜λ„€μš”. μ—„μ²­ 효율적인 μ½”λ“œμΈ 것 κ°™μŠ΅λ‹ˆλ‹€.. 아이디어 λŒ€λ°•..

Comment on lines +20 to +25
if remove_blocks + B >= add_blocks:
time = remove_blocks * 2 + add_blocks
# μ΅œμ†Œ μ‹œκ°„ κ°±μ‹  및 μ΅œλŒ€ 높이 선택
if time < min_time or (time == min_time and h > best_height):
min_time = time
best_height = h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ €λŠ” μ—¬κΈ°μ„œ if쀑첩이 μ—„μ²­ λ§Žμ•˜λŠ”λ° μˆ˜μ •ν•΄μ•Όκ² λ„€μš”γ…Ž

@wnsmir wnsmir merged commit 262ad54 into main Nov 24, 2024
1 check passed
@wnsmir wnsmir deleted the 6-wnsmir branch November 24, 2024 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants