https://www.acmicpc.net/problem/2002
2002번: 추월
입력은 총 2N+1개의 줄로 이루어져 있다. 첫 줄에는 차의 대수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 대근이가 적은 차량 번호 목록이 주어지고, N+2째 줄부터 N개의 줄에는 영식이
www.acmicpc.net
[난이도] Gold4
[유형] 구현
[풀이]
터널 통과전 각 자동차에 대해 자신보다 앞에 가고 있는 자동차를 적절한 자료구조에 기록해놓고
터널 통과후 앞에 이전에 있던 차가 없으면 그 차는 추월을 한것이다.
string 자체로 기록하기 위해 map,set 등을 이용하였는데 index로 변환하여 풀면
메모리 및 시간을 줄일 수 있다.
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold4/2002.cpp
#include <iostream> #include <string> #include <vector> #include <map> #include <set> using namespace std; string a[1001]; int main(){ ios_base::sync_with_stdio(); cin.tie(); map<string,vector<string>> start; map<string,set<string>> end; int N; cin>>N; for(int i=N-1;i>=0;i--) cin>>a[i]; for(int i=0;i<N-1;i++) for(int j=i+1;j<N;j++) start[a[i]].push_back(a[j]); for(int i=N-1;i>=0;i--) cin>>a[i]; for(int i=0;i<N-1;i++) for(int j=i+1;j<N;j++) end[a[i]].insert(a[j]); int ans = 0; for(auto k : start){ string n = k.first; for(auto e : k.second){ if(end[n].find(e) == end[n].end()) { ans++; break; } } } cout << ans; }
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <map> | |
#include <set> | |
using namespace std; | |
string a[1001]; | |
int main(){ | |
ios_base::sync_with_stdio(); | |
cin.tie(); | |
map<string,vector<string>> start; | |
map<string,set<string>> end; | |
int N; | |
cin>>N; | |
for(int i=N-1;i>=0;i--) cin>>a[i]; | |
for(int i=0;i<N-1;i++) | |
for(int j=i+1;j<N;j++) start[a[i]].push_back(a[j]); | |
for(int i=N-1;i>=0;i--) cin>>a[i]; | |
for(int i=0;i<N-1;i++) | |
for(int j=i+1;j<N;j++) end[a[i]].insert(a[j]); | |
int ans = 0; | |
for(auto k : start){ | |
string n = k.first; | |
for(auto e : k.second){ | |
if(end[n].find(e) == end[n].end()) { | |
ans++; | |
break; | |
} | |
} | |
} | |
cout << ans; | |
} |
'Problem-Solving > BOJ' 카테고리의 다른 글
[BOJ/백준][Gold4] 16398 : 행성 연결 (C++) (0) | 2021.01.17 |
---|---|
[BOJ/백준][Gold4] 16120 : PPAP (C++) (0) | 2021.01.17 |
[BOJ/백준][Gold4] 4803 : 트리 (C++) (0) | 2021.01.17 |
[BOJ/백준][Gold4] 14923 : 미로 탈출 (C++) (0) | 2021.01.13 |
[BOJ/백준][Gold4] 2655 : 가장높은탑쌓기 (C++) (0) | 2021.01.13 |