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
- 1174
- C++
- c++ #boj #
- N번째큰수
- 호반우 상인
- graph
- 16202
- graph #최단경로
- 진법변환 #2to10 #10to2 #이진법 #십진법 #변환 #bitset #c++
- 쌤쌤쌤
- 코딩
- LIS #가장긴증가하는부분수열 #
- 레드아보
- 3343
- boj #백준
- 최소 #공배수 #최대 #공약수 #유클리드 #호제법 #lcm #gcd #c++ #boj #3343 #백준 #장미
- 투포인터 #백준 #boj #20922 #22862
- 사이클 없는 그래프
- 백준 #다익스트라 #dijkstra #9370 #c++
- 줄어드는수
- BOJ
- backtracking #codetree #디버깅 #삼성코테
- 22869
- 이분탐색 #dp #11053
- 백준
- 3D #Reconstruction #computer #vision #volume #metric #tsdf #kinect #fusion
- c++ #입출력 #속도 #ios #sync_with_stdio #cin #cout #tie
- 30870
- hcpc
- 20117
Archives
- Today
- Total
hyunjin
[C++][BOJ 2170 선긋기] 본문
728x90
[실패 소스코드]
배열을 만들어 해당 구간 방문시 TRUE TREU인 곳에만 길이 구하기
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll len = 2000000010, bias = 1000000000; //2000000010 1000000000
bool line[len]={0,};
int n;
ll answer=0;
cin >> n;
for(int i=0;i<4;i++){
ll s,e;
cin>>s>>e;
for(ll j = s ; j<e;j++){
if(!line[j+bias]){
answer++;
line[j+bias]=true;
}
}
}
cout<<answer;
}
문제의 192MB로 메모리 제한이 있다.
지금 풀이의 배열만 1byte * 2,000,000,000 = 20MB정도한다.
정렬문제인데 이렇게 풀었다. seg fault 나왔다. 범위가 작다면 이 방법으로도 가능할 것 같다.
[성공한 소스 코드]
정렬해서 조건 맞춰서 구하기
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,answer=0;
cin >> n;
vector<pair<int,int>> v;
for(int i = 0 ; i < n ; i++){
int s,e;
cin >>s>>e;
v.push_back({s,e});
}
sort(v.begin(),v.end());
int s = v[0].first, e=v[0].second;
for(int i = 1 ; i < n ; i++){
int from= v[i].first,to=v[i].second;
if(from <= e){ // start 기준 정렬했으니까 이것만 검사
e = (to > e)? to : e; //조건 조심
}
else{//안겹치는 경우
answer+= (e-s);
s=from;
e=to;
}
}
answer+= (e-s);
cout<<answer;
}
728x90
'알고리즘 연습 > 백준' 카테고리의 다른 글
[C++][BOJ 7662 이중 우선순위 큐] multiset (0) | 2021.12.15 |
---|---|
[C++][BOJ 1629 곱셈] pow 연산 줄이기 (0) | 2021.12.06 |
[C++][BOJ 1174 줄어드는 수] (0) | 2021.09.09 |
[C++][BOJ 1461 도서관] 정렬 (0) | 2021.09.07 |
[C++][BOJ 20117 호반우 상인의 이상한 품질 계산법]정렬 (0) | 2021.09.06 |