거의 알고리즘 일기장
백준 4796, 1449, 17509 _ 그리디 알고리즘 본문
4796번
https://www.acmicpc.net/problem/4796
4796번: 캠핑
문제 등산가 김강산은 가족들과 함께 캠핑을 떠났다. 하지만, 캠핑장에는 다음과 같은 경고문이 쓰여 있었다. 캠핑장은 연속하는 20일 중 10일동안만 사용할 수 있습니다. 강산이는 이제 막 28일 휴가를 시작했다. 이번 휴가 기간 동안 강산이는 캠핑장을 며칠동안 사용할 수 있을까? 강산이는 조금 더 일반화해서 문제를 풀려고 한다. 캠핑장을 연속하는 P일 중, L일동안만 사용할 수 있다. 강산이는 이제 막 V일짜리 휴가를 시작했다. 강산이가 캠핑장을 최대
www.acmicpc.net
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int L, P, V;
int main()
{
int cnt = 1;
while (1)
{
cin >> L >> P >> V;
if (L == 0 && P ==0 && V == 0)
break;
int ans = (V / P) * L + min(V % P, L);
cout << "Case " + to_string(cnt) << ": " << ans << '\n';
cnt++;
}
return 0;
}
1449번
https://www.acmicpc.net/problem/1449
1449번: 수리공 항승
첫째 줄에 물이 새는 곳의 개수 N과 테이프의 길이 L이 주어진다. 둘째 줄에는 물이 새는 곳의 위치가 주어진다. N과 L은 1,000보다 작거나 같은 자연수이고, 물이 새는 곳의 위치는 1,000보다 작거나
www.acmicpc.net
스티커가 붙어있는지 확인 (+0.5), 만약에 없어서 붙일때는 (-0.5)해서 붙임
입력데이터가 정렬되서 들어오지 않으므로 입력받으면서 동작 수행할 생각하지 말것.ㅎ 내가 그래서 몇번 틀림.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
int N, L;
int cnt = 0;
cin >> N >> L;
double coverable = -1;
vector<double>leakPoints;
double leakPoint;
for (int i = 0; i < N; i++)
{
cin >> leakPoint;
leakPoints.push_back(leakPoint);
}
sort(begin(leakPoints), end(leakPoints));
for (auto& ele : leakPoints)
{
if (ele + 0.5 > coverable)
{
coverable = ele - 0.5 + (double)L;
cnt++;
}
}
cout << cnt;
return 0;
}
17509
https://www.acmicpc.net/problem/17509
17509번: And the Winner Is... Ourselves!
11 lines are given as the input. The $i$-th line contains two space-separated integers, $D_i$ and $V_i$, where $D_i$ is the amount of minutes required to solve the $i$-th problem, and $V_i$ is the number of incorrect verdicts on the $i$-th problem. For eac
www.acmicpc.net
해석을 잘못해서 좀.. 그랬다. v는 단발성인걸 생각하면 걍 time만 오름차순 정렬하여 풀면됨.
#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool comp(const pair<int, int>& a, const pair<int, int>& b)
{
return a.first < b.first;
}
int main()
{
vector<pair<int, int>>problems;
int t, v;
for (int i = 0; i < 11; i++)
{
cin >> t >> v;
problems.push_back({ t, v });
}
sort(begin(problems), end(problems), comp);
int time = 0;
int V = 0;
int total = 0;
for (int i = 0; i < 11; i++)
{
time += problems[i].first;
V = problems[i].second;
total += (time + V * 20);
}
cout << total;
return 0;
}
후기
쉬는시간에 인터넷 서칭하다가 발견한 블로그에서 여러 알고리즘을 잘 설명하여 보다가 추천문제도 있어 풀어보았다. 한동안은 이 블로그에 있는 추천문제들을 풀듯하다.
https://m.blog.naver.com/kks227/220775134486
탐욕적 기법(Greedy Algorithm) (수정: 2019-11-23)
먼저 가장 유명하고 기초적이지만, 입문만 쉽고 마스터는 어려운 그런 녀석들부터 강의하는 것이 좋겠죠.탐...
blog.naver.com
'알고리즘 문제풀이' 카테고리의 다른 글
백준 1700번 _ 멀티탭 스케줄링 (0) | 2020.05.07 |
---|---|
백준 11000번 _ 강의실 배정 (0) | 2020.05.07 |
codeforces _ 189A _ A. Cut Ribbon (0) | 2020.05.06 |
codeforces_ 492B _ B. Vanya and Lanterns (0) | 2020.05.06 |
백준 2098번 _ 외판원 순회 (0) | 2020.05.06 |