일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- DP
- 백트래킹
- 다익스트라
- 너비 우선 탐색
- Overfitting
- 크루스칼
- 분할 정복
- NEXT
- back propagation
- 가끔은_말로
- 플로이드 와샬
- 회고록
- 가끔은 말로
- 조합론
- 2023
- pytorch
- lazy propagation
- dropout
- 이분 탐색
- 알고리즘
- 미래는_현재와_과거로
- 우선 순위 큐
- 세그먼트 트리
- c++
- object detection
- 자바스크립트
- tensorflow
- dfs
- 문자열
- BFS
Archives
- Today
- Total
Doby's Lab
백준 12867번: N차원 여행 (C++) 본문
https://www.acmicpc.net/problem/12867
12867번: N차원 여행
예제 2의 경우에 (0,0) -> (1,0) -> (1,1) -> (0,1) -> (0,0)으로 이동하게 되어서 (0, 0)을 두 번 방문하게 되고, 예제 3의 경우에는 (0,0,0) -> (1,0,0) -> (1,1,0) -> (1,1,1) -> (0,1,1) -> (0,0,1) 으로 방문하게 되어서 모
www.acmicpc.net
Solved By: Coordinate Compression
차원의 크기 N은 [1, 1,000,000,000]으로 너무 큽니다. 크기가 1,000,000,000인 배열을 선언하기는 부담스러우니 차원(값) 압축을 시킵시다.
하지만, 문제는 중복된 좌표를 어떻게 알고 처리할지가 문제였습니다. string으로 처리하자니 한 차원의 자리가 두 자리가 넘어가면 난감했습니다.
그때 발견한 테크닉은 신기했습니다. set에서 vector<int>까지 담을 수 있는 것이었습니다.
이 점을 활용하여 좌표가 이동할 때마다 해당 배열이 있는지 set을 통해 확인하여 문제를 풀 수 있었습니다.
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
set<vector<int>> st; // new tech
int n, m;
vector<int> v;
vector<int> temp;
int main(){
cin >> n >> m;
for(int i = 0, value; i < m; i++){
cin >> value;
v.push_back(value); temp.push_back(value);
}
// Coordinate Compression
sort(temp.begin(), temp.end());
temp.erase(unique(temp.begin(), temp.end()), temp.end());
vector<int> coor(temp.size());
st.insert(coor);
bool flag = 1;
string s; cin >> s;
for(int i = 0; i < m; i++){
int idx = lower_bound(temp.begin(), temp.end(), v[i]) - temp.begin();
if(s[i] == '+'){
coor[idx] += 1;
}
else{
coor[idx] -= 1;
}
if(st.count(coor)){
flag = 0; break;
}
else{
st.insert(coor);
}
}
if(flag) cout << 1;
else cout << 0;
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 14606번: 피자 (Small) (C++) (0) | 2022.06.06 |
---|---|
백준 18769번: 그리드 네트워크 (C++) (0) | 2022.06.06 |
백준 2171번: 직사각형의 개수 (C++) (0) | 2022.06.05 |
백준 18869번: 멀티버스 II (C++) (0) | 2022.06.05 |
백준 15481번: 그래프와 MST (C++) (0) | 2022.06.04 |