Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- BFS
- 다익스트라
- 스택
- 백트래킹
- 백준
- 자료구조
- DFS
- Unreal Engine 5
- 재귀
- 누적 합
- 알고리즘
- 투 포인터
- VR
- Team Fortress 2
- XR Interaction Toolkit
- 정렬
- 트리
- 유니티
- ue5
- 문자열
- 시뮬레이션
- 유니온 파인드
- 우선순위 큐
- 다이나믹 프로그래밍
- 브루트포스
- 수학
- c++
- 그리디 알고리즘
- 구현
- 그래프
Archives
- Today
- Total
1일1알
백준 15922번 아우으 우아으이야!! C++ 본문
https://www.acmicpc.net/problem/15922
15922번: 아우으 우아으이야!!
N개의 선분을 모두 그렸을 때, 수직선 위에 그어진 선분 길이의 총합을 출력한다아아어으잉에애야우아으아이아야아아아아아아이야!!!
www.acmicpc.net
선분이 (start, end)면 일단 start 기준으로 오름차순 정렬을 한다.
그러면 세가지 경우가 있다.
1. 현재 선분이 이전 선분과 모든 부분이 겹치는 경우 -> 아무것도 안해도 됨
2. 현재 선분이 이전 선분가 일정부분 겹치는 경우 -> 겹치는 부분을 제외하고 합을 증가시키고 ge(Global End) 값 갱신
3. 현재 선분과 이전 선분이 겹치지 않는 경우 -> 현재 선분의 길이만큼 합을 증가시키도 ge값 갱신
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
vector<pair<int, int>> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
v = vector<pair<int, int>>(n);
for (int i = 0; i < n; i++) {
int s, e;
cin >> s >> e;
v[i] = { s,e };
}
sort(v.begin(), v.end());
int sum = 0;
int ge = -1000000001;
for (int i = 0; i < n; i++) {
auto curr = v[i];
if (curr.second <= ge) continue;
if (curr.first < ge) sum += curr.second - ge;
else sum += curr.second - curr.first;
ge = curr.second;
}
cout << sum;
}
'알고리즘' 카테고리의 다른 글
백준 10025번 게으른 백곰 C++ (0) | 2022.11.24 |
---|---|
백준 3495번 아스키 도형 C++ (0) | 2022.11.23 |
백준 14395번 4연산 C++ (0) | 2022.11.21 |
백준 9011번 순서 C++ (0) | 2022.11.20 |
백준 15990번 1, 2, 3 더하기 5 C++ (0) | 2022.11.19 |