๐ ๋งํฌ
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 |