[BOJ/13334] 철둜, c++

2021. 5. 20. 14:53Β·DEV/PS

πŸ”— 링크

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

 

13334번: 철둜

μž…λ ₯은 ν‘œμ€€μž…λ ₯을 μ‚¬μš©ν•œλ‹€. 첫 번째 쀄에 μ‚¬λžŒ 수λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ–‘μ˜ μ •μˆ˜ n (1 ≤ n ≤ 100,000)이 μ£Όμ–΄μ§„λ‹€. λ‹€μŒ n개의 각 쀄에 μ •μˆ˜ 쌍 (hi, oi)κ°€ μ£Όμ–΄μ§„λ‹€. μ—¬κΈ°μ„œ hi와 oiλŠ” −100,000,000이상, 100,000,0

www.acmicpc.net

https://github.com/jokj624/PS/blob/master/10000-15000/13334.cpp

 

jokj624/PS

BOJ, CodeForces μ•Œκ³ λ¦¬μ¦˜ 문제 μ†ŒμŠ€μ½”λ“œ. Contribute to jokj624/PS development by creating an account on GitHub.

github.com

 

🀯 ν•œμ€„ ν›„κΈ°

λ„ˆ - 무 μ–΄λ ΅λ‹€. λ°˜λ‘€λ§Œ μ°Ύλ‹€ μ‹€νŒ¨

🀷 문제

πŸ‘©‍πŸ’» 풀이

λŒ€μΆ© μˆ˜μ§μ„  상 μ’Œν‘œκ°€ μžˆμ„ λ•Œ, 철둜 길이 d둜 κ°€μž₯ 많이 포함 ν•  수 μžˆλŠ” μ’Œν‘œ 개수λ₯Ό 찾으면 λ˜λŠ” λ¬Έμ œμ΄λ‹€.

처음 μƒκ°ν•œ 방법은 정말 κ·Έλ¦¬λ””ν•˜κ²Œ 

μš°μ„ μˆœμœ„ νμ—μ„œ xμ’Œν‘œ μˆœμ„œλŒ€λ‘œ μ •λ ¬ν•œ ν›„, xκ°€ 같을 μ‹œ yμ’Œν‘œ μˆœμ„œλŒ€λ‘œ μ •λ ¬ν•΄μ€€λ‹€.

그리고 x-y μ’Œν‘œ 차이가 d보닀 μž‘λ‹€λ©΄

 

1. start-end κ°€ 없을 λ•Œ start-endλ₯Ό 각 각 ν˜„μž¬ x-x+d 둜 갱신해쀬닀. 

2. start-end κ°€ μ‘΄μž¬ν•  λ•Œ, ν˜„μž¬ μ’Œν‘œκ°€ κ·Έ μ•ˆμ— μžˆλŠ”μ§€ 확인

   2-1. μ•ˆμ— μžˆλ‹€λ©΄ μΉ΄μš΄νŒ…

   2-2. μ•ˆμ— μ—†λ‹€λ©΄ start-end μƒˆλ‘œ κ°±μ‹ 

 

뭐 이런 방법이닀. WA만 μ—„μ²­ λ°›μ•˜λ‹€. ν‹€λ¦° μ½”λ“œλŠ” μ•„λž˜μ—!

더보기
//WA
//BOJ 13334 철둜
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef long long ll;
struct point {
	int x;
	int y;
};
struct compare {
	bool operator()(const point& p1, const point& p2) {
		if(p1.x == p2.x)	return p1.y > p2.y;
		else	return p1.x > p2.x;
	}
};
bool cmp(const point& p1, const point& p2){
	return p1.x < p2.x;
}
priority_queue<point, vector<point>, compare> pq;
int main()
{
	int N;
	vector<point> v;
	cin >> N;
	for(int i=0; i<N; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		v.push_back({a, b});
		if(v[i].x > v[i].y)	swap(v[i].x, v[i].y);
	}
	int d;
	cin >> d;
	sort(v.begin(), v.end(), cmp);
	for(point tmp: v){
		pq.push(tmp);
	}
	ll start = LLONG_MAX, end = LLONG_MAX;
	int ans = 0, count = 0;
	while(!pq.empty()){
		point current = pq.top();
		pq.pop();
	//	cout << current.x << " " << current.y << endl;
		if((current.y - current.x) > d ) 	continue;
		if(start == LLONG_MAX || end == LLONG_MAX){
			start = current.x;
			end = current.x + d;
			count += 1;
			ans = max(ans, count);
		}
		else{
			if(current.x >= start && current.y <= end){
				count += 1;
				ans = max(count, ans);
			}
			else{
				start = current.x;
				end = current.x + d;
				ans = max(count, ans);
				count = 1;
			}
		}
	}
	printf("%d", ans);
	return 0;
}

 

λ°˜λ‘€λ₯Ό 고쳐도 계속 ν‹€λ € λ„ˆλ¬΄ νž˜λ“€μ—ˆλ‹€. κ²°κ΅­ 풀이λ₯Ό λ΄€λŠ”λ° μš°μ„ μˆœμœ„ 큐 2개λ₯Ό μ‚¬μš©ν•˜λŠ” ν’€μ΄μ˜€λ‹€.

 

처음 μš°μ„ μˆœμœ„ νμ—λŠ” 도착점을 κΈ°μ€€μœΌλ‘œ μ •λ ¬

λ‘λ²ˆμ§Έ μš°μ„ μˆœμœ„ νλŠ” μ‹œμž‘μ μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ 

 

처음 νμ—λŠ” 미리 λͺ¨λ“  μ’Œν‘œλ₯Ό 넣어놓고 ν•΄λ‹Ή 큐에 μ›μ†Œκ°€ μ—†μ„λ•Œ κΉŒμ§€ 거리 비ꡐλ₯Ό ν•΄ λ‘λ²ˆμ§Έ 큐에 μ‚½μž…ν•œλ‹€.

 

처음 큐의 top μ›μ†Œμ˜ x μ’Œν‘œ + d κ°€ 처음 큐의 top μ›μ†Œμ˜ yμ’Œν‘œ 보닀 μž‘λ‹€λ©΄ μ“Έλͺ¨ μ—†μœΌλ‹ˆ 버린닀.

그게 아닐 경우 이제

λ‘λ²ˆμ§Έ 큐의 topμ›μ†Œμ˜ xμ’Œν‘œ + d 보닀 처음 큐의 top μ›μ†Œμ˜ yμ’Œν‘œκ°€ 더 크닀면 κ³„μ†ν•΄μ„œ λ‘λ²ˆμ§Έ 큐의 top μ›μ†Œλ₯Ό pop μ‹œν‚¨λ‹€. (xμ’Œν‘œ κΈ°μ€€ μ˜€λ¦„μ°¨μˆœ νμ΄λ―€λ‘œ x μ’Œν‘œλ₯Ό 더 크게 λŠ˜λ¦¬λŠ”κ±°λ‹€!) 

 

κ·ΈλŸ¬λ‹€ μ–΄λŠ μˆœκ°„ x+d κ°€ ν˜„μž¬ y보닀 컀진닀면 λ©ˆμΆ”κ³  처음 큐(ν˜„μž¬ μ’Œν‘œ)의 topμ›μ†Œλ₯Ό μ‚½μž…ν•œλ‹€.

λ§€ μˆœκ°„ λ‘λ²ˆμ§Έ 큐의 μ‚¬μ΄μ¦ˆκ°€ 거리 d둜 λ§Œλ“€ 수 μžˆλŠ” μ² λ‘œλ³„ μ’Œν‘œ κ°œμˆ˜κ°€ λœλ‹€.

 

μ–΄λ ΅λ‹€.. μ–΄λ €μ›Œ! 큐λ₯Ό λ‘κ°œ μ“΄λ‹€κ³  μƒκ°ν•˜κΈ° μ–΄λ €μš΄ 문제 κ°™λ‹€.

 

πŸ’» μ½”λ“œ

//AC
//BOJ 13334 철둜
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
struct point {
	int x;
	int y;
};
struct compare {
	bool operator()(const point& p1, const point& p2) {
		if(p1.y == p2.y)	return p1.x > p2.x;
		else	return p1.y > p2.y;
	}  // 끝점 κΈ°μ€€ μ •λ ¬
};
struct cmp{
	bool operator()(const point& p1, const point& p2){
		return p1.x > p2.x;
	}
};  // μ‹œμž‘μ  κΈ°μ€€ μ •λ ¬
priority_queue<point, vector<point>, compare> pq;  // 끝점 κΈ°μ€€ μ •λ ¬
priority_queue<point, vector<point>, cmp> spq; //μ‹œμž‘μ  κΈ°μ€€ μ •λ ¬ 
int main()
{
	int N;
	vector<point> v;
	cin >> N;
	for(int i=0; i<N; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		v.push_back({a, b});
		if(v[i].x > v[i].y)	swap(v[i].x, v[i].y);
	}
	int d;
	cin >> d;
	for(point tmp: v){
		pq.push(tmp);
	}
	int ans = 0, count = 0;
	while(!pq.empty()){
		if(pq.top().x + d < pq.top().y){   //ν˜„μž¬ x-y 거리가 d보닀 크면 ν•„μš” μ—†μŒ
			pq.pop();
		}
		else{
			while(!spq.empty() && spq.top().x + d < pq.top().y){
				spq.pop();   //ν˜„μž¬ y값이 정닡큐에 μžˆλŠ” κΈ°μ‘΄ 철둜 xκ°’+d(거리) 보닀 크닀면 κΈ°μ‘΄ 철둜 μ‹œμž‘μ  xλ₯Ό 버리고 κ³„μ†ν•΄μ„œ μ¦κ°€μ‹œν‚¨λ‹€.
			}
			spq.push(pq.top()); // ν˜„μž¬ x-yλ₯Ό μ •λ‹΅ 큐에 λ„£λŠ”λ‹€.
			pq.pop();
			int len = spq.size();   
			ans = max(ans, len);  // 맀번 μ‚¬μ΄μ¦ˆ λΉ„κ΅ν•˜μ—¬ μ΅œλŒ“κ°’ κ°±μ‹ 
		}
	}
	printf("%d", ans);
	return 0;
}
μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ λ³€κ²½κΈˆμ§€ (μƒˆμ°½μ—΄λ¦Ό)

'DEV > PS' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[BOJ/21872] Deque Game, c++  (0) 2021.06.04
[BOJ/1103] κ²Œμž„, c++  (0) 2021.05.29
[BOJ/18405] 경쟁적 μ „μ—Ό, c++  (0) 2021.05.17
[BOJ/20127] Y-μˆ˜μ—΄, C++  (0) 2021.05.14
제 1회 SMUPC μˆ™λͺ… ν”„λ‘œκ·Έλž˜λ° κ²½μ§„λŒ€νšŒ μ°Έκ°€ ν›„κΈ°  (4) 2021.05.09
'DEV/PS' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [BOJ/21872] Deque Game, c++
  • [BOJ/1103] κ²Œμž„, c++
  • [BOJ/18405] 경쟁적 μ „μ—Ό, c++
  • [BOJ/20127] Y-μˆ˜μ—΄, C++
jobchae
jobchae
λ§ν•˜λŠ” κ°μžμ§€λ§Œ, μ½”λ“œλ₯Ό λ„μ μ΄λŠ” Node.js λ°±μ—”λ“œ κ°œλ°œμžμž…λ‹ˆλ‹€.
  • jobchae
    JOBCHAE
    jobchae
  • 전체
    였늘
    μ–΄μ œ
    • πŸš€ JOBCHAE (182)
      • DEV (151)
        • PS (108)
        • Node.js (12)
        • React (3)
        • docker (1)
        • μž‘λ‹€ν•œ 개발 일지 (21)
        • injection (1)
        • JS, TS (3)
        • DB (2)
      • 좕ꡬ (0)
      • 일상 (19)
      • μ˜ν™” (3)
      • μŒμ•… (8)
  • λΈ”λ‘œκ·Έ 메뉴

    • πŸ’» Github
    • πŸ™‹πŸ» Linkedin
    • πŸ“– λ°©λͺ…둝
  • 링크

    • PS Github
  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    Nest.js
    boj
    node.js
    typescript
    db
    mongoDB
    μš°μ„ μˆœμœ„ν
    DP
    μ†νŠΈ
    μ•Œκ³ λ¦¬μ¦˜
    JavaScript
    BFS
    Express
    GitHub
    λ°±μ€€
    회고
    λ ›μΈ λ½νŽ˜μŠ€ν‹°λ²Œ
    react
    일상
    μœ„μƒμ •λ ¬
    PS
    λ¦¬μ•‘νŠΈ
    nodejs
    개발
    이뢄탐색
    μŠ¬λž™λ΄‡
    μ•±μžΌ
    SOPT
    μŠ¬λž™
    DFS
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.5
jobchae
[BOJ/13334] 철둜, c++
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”