Koder / 박성훈
article thumbnail

랭작문제

정렬을 열심히 해주고

범위에 상관없이

가져갈수있는 책들 중 가장 앞번호만 가져가게 해주었다

모두가 앞번호를 가져가려 하고,

앞번호를 가져갈 수 있는 사람들부터 책을 나눠주게 되면,

가장 많은 수의 책을 나눠줄 수 있게 된다.

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

 

9576번: 책 나눠주기

백준이는 방 청소를 하면서 필요 없는 전공 서적을 사람들에게 나눠주려고 한다. 나눠줄 책을 모아보니 총 N권이었다. 책이 너무 많기 때문에 백준이는 책을 구분하기 위해 각각 1부터 N까지의

www.acmicpc.net

#include <stdio.h>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

bool chk[1010] = {0};
int n,m,a,b,cnt,t;
vector<pair<int,int>> v;

bool compare(pair<int,int> a, pair<int,int> b){
	if(a.second == b.second) return a.first < b.first;
	return a.second < b.second;
}

int main(){
	scanf("%d", &t);
	while(t--){
		cnt = 0;
		v.clear();
		for(int i=0; i<1010; i++) chk[i] = false;
		
		scanf("%d %d", &n, &m);
		for(int i=0; i<m; i++){ scanf("%d %d", &a, &b); v.push_back({a,b}); }	
		
		sort(v.begin(), v.end(), compare);
		for(int i=0; i<m; i++){
			for(int j=v[i].first; j<=v[i].second; j++) if(chk[j] == false){ chk[j] = true; cnt++; break; }
		}
		printf("%d\n", cnt);
	}
	return 0;
}

얘도 적당히 짜주면 무난하게 AC

와 내가 골드1을 무난하게 AC라 말할수 있는 날이 오다니

감회가 새롭군

깔끔하게 AC 받았다.

반응형