개발/알고리즘
백준 15922 아우으 우아으이야!! C++
daisy-day
2021. 4. 3. 00:18
문제
15922번: 아우으 우아으이야!!
N개의 선분을 모두 그렸을 때, 수직선 위에 그어진 선분 길이의 총합을 출력한다아아어으잉에애야우아으아이아야아아아아아아이야!!!
www.acmicpc.net
코드
#include<bits/stdc++.h>
using namespace std;
int n, answer;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
int start = -1e9-1, end = -1e9-1;
for (int i = 0; i < n; i++)
{
int num1 = 0, num2 = 0;
cin >> num1 >> num2;
//겹치는 부분이 없음
if (num1 > end)
{
answer += end - start;
start = num1;
end = num2;
}
else
{
end = max(end, num2);
}
}
answer += end - start;
cout << answer << "\n";
return 0;
}
문제 아이디어