본문 바로가기
백준 문제 해설

백준 20649 Stuck in a Rut

by toomanysegtrees 2022. 11. 28.

힘내자

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

 

20649번: Stuck in a Rut

Farmer John has recently expanded the size of his farm, so from the perspective of his cows it is effectively now infinite in size! The cows think of the grazing area of the farm as an infinite 2D grid of square "cells", each filled with delicious grass (t

www.acmicpc.net

풀이

ecow, ncow를 구분해주고 사건 정리 및 정렬 후 처리를 진행해준다.

 

/* basic setup {{{ */
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define compress(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define fast_io ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef const int ci;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
/*}}}*/
struct cow{
	int x, y, id;
};
struct eve{
	int t, gen, diec, blamc;
};
ci BN = 1e5+1;
int n, ans[BN];
int chk[BN], par[BN];
vector<cow> ncow, ecow;
vector<eve> event;
vector<int> chd[BN];
void init(){
	cin>>n;
	F0R(i, n){
		char c; int a, b; cin>>c>>a>>b;
		if(c == 'N') ncow.pb({a, b, i});
		else ecow.pb({a, b, i});
	}
}
int pe = 0;
void dps(int cur){
	int st = ++pe;
	for(int nx : chd[cur]){
		dps(nx);
	}
	ans[cur] = pe-st;
}
void pro(){
	for(int i=0;i<sz(ncow);i++){
		for(int j=0;j<sz(ecow);j++){
			if(ecow[j].x < ncow[i].x && ecow[j].y > ncow[i].y){
				int d1 = ncow[i].x - ecow[j].x, d2 = ecow[j].y-ncow[i].y;
				if(d1 == d2) continue;
				if(d1 > d2){
					event.pb({d1, d2, ecow[j].id, ncow[i].id});
				}
				else{
					event.pb({d2, d1, ncow[i].id, ecow[j].id});
				}
			}
		}
	}
	sort(all(event), [](eve l, eve r){ return l.t < r.t; });
	memset(par, -1, sizeof par);
	memset(chk, 0x3f, sizeof chk);
	for(eve f : event){
		if(chk[f.diec] != INF || f.gen > chk[f.blamc]) continue;
		chd[f.blamc].pb(f.diec);
		par[f.diec] = f.blamc;
		chk[f.diec] = f.t;
	}

	for(int i=0;i<n;i++){
		if(par[i] == -1){
			dps(i);
		}
	}

	for(int i=0;i<n;i++) cout<<ans[i]<<'\n';
}
int main(){
	fast_io;
	init();
	pro();
}

'백준 문제 해설' 카테고리의 다른 글

백준 25380 커다란 도시  (0) 2023.05.15
백준 18320 Loan Repayment  (0) 2022.11.27
백준 4435 중간계 전쟁  (0) 2022.11.26
백준 2493 탑  (0) 2022.11.25
백준 7595 Triangles  (0) 2022.11.24
백준 17267 상남자  (1) 2022.11.23
백준 16234 인구 이동  (0) 2022.11.22
백준 25386 라즈베리 파이  (0) 2022.11.21