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

ABC_230_D - Destroyer Takahashi #42

Open
zeikar opened this issue Dec 3, 2021 · 0 comments
Open

ABC_230_D - Destroyer Takahashi #42

zeikar opened this issue Dec 3, 2021 · 0 comments
Assignees
Labels
study Study

Comments

@zeikar
Copy link
Owner

zeikar commented Dec 3, 2021

Problem link

https://atcoder.jp/contests/abc230/tasks/abc230_d

Problem Summary

모든 벽을 부수기 위해 최소 몇 번 펀치를 해야 하는지 구하는 문제.

Solution

모든 벽을 부숴야 하므로 정렬 후 왼쪽부터 쭉 나가면 될 것 같다.
근데 이게 생각보다 안돼서... 대회 중에 결국 못 풀었다

에디토리얼을 참고해서 재작성.

일단 정렬은 맞는데 오른쪽 좌표를 기준으로 정렬해야 한다. 해당 벽을 제거할 때 최대한 오른쪽을 제거해야 다른 벽도 같이 제거할 수 있기 때문이다.
제거하는 벽은 왼쪽 좌표가 제거할 좌표 + d - 1 이내이면 가능하다.
이 좌표를 넘어가면 한번에 부술 수 없으므로 다시 오른쪽 좌표에서 펀치를 날려야 한다.

Source Code

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

int main() {
	int n, d;
	cin >> n >> d;

	vector< pair<int, int> > walls;

	for (int i = 1; i <= n; i++)
	{
		int l, r;
		cin >> l >> r;

		walls.push_back(make_pair(r, l));
	}

	sort(walls.begin(), walls.end());

	int ans = 0, x = -1987654321;
	for (int i = 0; i < walls.size(); i++)
	{
		int l = walls[i].second;
		int r = walls[i].first;

		if (l > x + d - 1)
		{
			ans++;

			x = r;
		}
	}

	cout << ans << endl;
}
@zeikar zeikar added the study Study label Dec 3, 2021
@zeikar zeikar self-assigned this Dec 3, 2021
zeikar added a commit that referenced this issue Dec 3, 2021
zeikar added a commit that referenced this issue Jan 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
study Study
Projects
None yet
Development

No branches or pull requests

1 participant