Priceless
[백준] 2246번: 콘도 선정 (C++) 본문

입력 횟수 만큼 거리와 비용을 입력 받은 후
가장 적은 거리거나 가장 적은 비용일 때의 경우의 수를 구한다
처음에는 단순히 합을 구한 후 최소 값이 몇 개인지 확인하면 될 줄 알았다
그 결과 바로 실패..
사실 아직도 왜 틀렸는지 완전히 이해하진 못했다
아마 예시에서는 100의 배수로만 되어 있어서 단순하지 않은 조건이 있는 듯하다
int main(){
init();
int n ;
cin >> n;
int c, d;
int min = 20000;
int count = 0;
for(int i = 0; i < n; i++){
cin >> c >> d;
if(min > c + d){
min = c + d;
count = 1;
}
else if (min == c + d){
count++;
}
}
cout << count;
return 0;
}
각종 사이트의 도움을 받아 문제를 풀었지만
문제에 대한 이해도 필요하고
vector와 pair에 대한 이해가 더욱 필요할 것 같다
// baekjoon 14646
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
// codes for fast I/O
void init(){
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
}
bool cmp(pair<int, int> a, pair<int, int> b){
if (a.first == b.first) {
return a.second < b.second;
}
return a.first < b.first;
}
int main(){
init();
int n ;
cin >> n;
int c, d;
vector<pair<int, int>> v;
for(int i = 0; i < n; i++){
cin >> c >> d;
v.push_back({d,c});
}
sort(v.begin(), v.end(),cmp);
int count = 1;
d = v[0].first;
c = v[0].second;
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
if (v[j].first > d && v[j].second < c) {
++count;
d = v[j].first;
c = v[j].second;
break;
}
}
}
cout << count;
return 0;
}
'Algorithm & Test > 백준' 카테고리의 다른 글
[백준] 7562번: 나이트의 이동 (C++) (0) | 2023.09.07 |
---|---|
[백준] 1012번: 유기농 배추 (C++) (0) | 2023.09.06 |
[백준] 2204번: 도비의 난독증 테스트 (C++) (0) | 2023.09.02 |
[백준] 4307번: 개미 (C++) (0) | 2023.09.01 |
[백준] 15903번: 카드 합체 놀이 (C++) (0) | 2023.08.31 |