[C++] μŠ€μœ„ν•‘ μ•Œκ³ λ¦¬μ¦˜(Sweeping Algorithm)

2021. 8. 9. 20:33Β·πŸ“ Computer Science/✏ Algorithm

μŠ€μœ„ν•‘ μ•Œκ³ λ¦¬μ¦˜(Sweeping Algorithm)

  • κ³΅κ°„μ΄λ‚˜ 직선 μƒμ—μ„œ ν•œμͺ½ μ‹œμž‘μ μ„ κΈ°μ€€μœΌλ‘œ λ°˜λŒ€νŽΈ μ’…λ£Œ μ§€μ κΉŒμ§€ μ§€λ‚˜κ°€λŠ”λ°, λ§ˆμ£ΌμΉ˜λŠ” μš”μ†Œλ“€μ— λŒ€ν•΄ νŒλ‹¨μ΄ λ˜λŠ” 기쀀을 μ μš©ν•΄ 정닡을 κ΅¬ν•˜λŠ” 방식이닀.
  • 즉, μ •λ ¬λœ μš”μ†Œλ“€μ„ ν•œ 번만 μˆœνšŒν•˜λ©° μ—°μ‚°ν•˜λ©΄ 정닡이 λ‚˜μ˜€κ²Œ λœλ‹€.

 

문제

 

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

 

2261번: κ°€μž₯ κ°€κΉŒμš΄ 두 점

첫째 쀄에 μžμ—°μˆ˜ n(2 ≤ n ≤ 100,000)이 μ£Όμ–΄μ§„λ‹€. λ‹€μŒ n개의 μ€„μ—λŠ” μ°¨λ‘€λ‘œ 각 점의 x, yμ’Œν‘œκ°€ μ£Όμ–΄μ§„λ‹€. 각각의 μ’Œν‘œλŠ” μ ˆλŒ“κ°’μ΄ 10,000을 λ„˜μ§€ μ•ŠλŠ” μ •μˆ˜μ΄λ‹€. μ—¬λŸ¬ 점이 같은 μ’Œν‘œλ₯Ό κ°€μ§ˆ μˆ˜λ„

www.acmicpc.net

#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

const int INF = 1e9;

int dist(pair<int, int> p1, pair<int, int> p2) {
	return pow((p1.first - p2.first), 2) + pow((p1.second - p2.second), 2);
}

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

	vector<pair<int, int>> p(n);
	for (int i = 0; i < n; i++)
		cin >> p[i].second >> p[i].first;

	sort(p.begin(), p.end(), [](pair<int, int> p1, pair<int, int> p2) {
		if (p1.second == p2.second)
			return p1.first < p2.first;
		return p1.second < p2.second;
		});

	set<pair<int, int>> s;
	s.insert(p[0]);
	s.insert(p[1]);

	int answer = dist(p[0], p[1]), start = 0;
	for (int i = 2; i < n; i++) {
		// yμ’Œν‘œκ°€ answer 보닀 큰 점 μ œμ™Έ
		int x = p[i].second - p[start].second;
		while (start < i && x * x >= answer) {
			s.erase(p[start++]);
			x = p[i].second - p[start].second;
		}

		// xμ’Œν‘œκ°€ (xμ’Œν‘œ ± answer) 거리 내에 μžˆλŠ” 점 쀑 μ΅œλ‹¨ 거리λ₯Ό answer둜 κ°±μ‹ 
		auto start = s.lower_bound({ p[i].first - sqrt(answer), -INF });
		auto end = s.upper_bound({ p[i].first + sqrt(answer), INF });
		for (auto j = start; j != end; j++)
			answer = min(answer, dist(p[i], *j));

		s.insert(p[i]);
	}
	cout << answer;
}

 

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

 

3392번: ν™”μ„± 지도

첫째 쀄에 화성탐사선 μ„±ν™”κ°€ 보낸 μ§€λ„μ˜ 수 N(1 ≤ N ≤ 10,000)이 μ£Όμ–΄μ§„λ‹€. λ‹€μŒ N개의 μ€„μ—λŠ” 각 μ§€λ„μ˜ 정보가 μ£Όμ–΄μ§„λ‹€. μ§€λ„μ˜ μ •λ³΄λŠ” λ„€ μ •μˆ˜ x1, y1, x2, y2 (0 ≤ x1 < x2 ≤ 30,000, 0 ≤ y1 < y2 ≤ 30

www.acmicpc.net

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

#define SIZE 30002

struct Point {
    int x, y1, y2, val;
};

vector<Point> points;
int segtree[SIZE * 4];
int cnt[SIZE * 4];

void update(int node, int start, int end, int left, int right, int val)
{
    if (start > right || end < left)
        return;

    if (left <= start && end <= right) {
        cnt[node] += val;
    }
    else {
        int mid = (start + end) / 2;
        update(node * 2, start, mid, left, right, val);
        update(node * 2 + 1, mid + 1, end, left, right, val);
    }

    if (cnt[node])
        segtree[node] = end - start + 1;
    else {
        if (start == end)
            segtree[node] = 0;
        else
            segtree[node] = segtree[node * 2] + segtree[node * 2 + 1];
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int x1, x2, y1, y2;
        cin >> x1 >> y1 >> x2 >> y2;

        Point temp;
        temp.y1 = y1 + 1, temp.y2 = y2 + 1;
        temp.x = x1, temp.val = 1;
        points.push_back(temp);
        temp.x = x2, temp.val = -1;
        points.push_back(temp);
    }

    sort(points.begin(), points.end(), [](Point& a, Point& b) {
        return a.x < b.x;
        });

    int answer = 0;
    for (int i = 0; i < points.size(); i++) {
        if (i > 0)
            answer += (points[i].x - points[i - 1].x) * segtree[1];
        update(1, 1, SIZE, points[i].y1, points[i].y2 - 1, points[i].val);
    }
    cout << answer;
}
μ €μž‘μžν‘œμ‹œ (μƒˆμ°½μ—΄λ¦Ό)
'πŸ“ Computer Science/✏ Algorithm' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [C++] μœ„μƒ μ •λ ¬(Topological Sort)
  • [C++] νŽœμœ… 트리(Fenwick Tree)
  • [C++] κ°•ν•œ μ—°κ²° μš”μ†Œ(νƒ€μž”, 코사라주)
  • [C++] μ΅œμ†Œ μ‹ μž₯ 트리(크루슀칼)
Blxxming
Blxxming
CS 지식과 κ³΅λΆ€ν•˜λ‹€ 배운 것, κ²½ν—˜ν•œ 것 등을 κΈ°λ‘ν•˜λŠ” λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.
  • Blxxming
    πŸ’‘λ²ˆλœ©πŸ’‘
    Blxxming
  • 전체
    였늘
    μ–΄μ œ
  • 곡지사항

    • Tech Interview
    • πŸ“š Tech (246)
      • πŸ“ Computer Science (96)
        • ✏ OS (12)
        • ✏ Network & Web (10)
        • ✏ Database (11)
        • ✏ Data Structure (6)
        • ✏ Algorithm (40)
        • ✏ Design Pattern (9)
        • ✏ Cloud Computing (3)
        • ✏ (5)
      • πŸ“ Language (73)
        • ✏ Language (6)
        • ✏ C & C++ (11)
        • ✏ C# (19)
        • ✏ JAVA (37)
      • πŸ“ Game (43)
        • ✏ Computer Graphics (2)
        • ✏ Unity (14)
        • ✏ Unreal (26)
        • ✏ (1)
      • πŸ“ Book (34)
        • ✏ Effective (3)
        • ✏ Game Server (16)
        • ✏ Clean Code (14)
        • ✏ (1)
  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.0
Blxxming
[C++] μŠ€μœ„ν•‘ μ•Œκ³ λ¦¬μ¦˜(Sweeping Algorithm)
μƒλ‹¨μœΌλ‘œ

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