문제 링크입니다: https://www.acmicpc.net/problem/13904
13904번: 과제
예제에서 다섯 번째, 네 번째, 두 번째, 첫 번째, 일곱 번째 과제 순으로 수행하고, 세 번째, 여섯 번째 과제를 포기하면 185점을 얻을 수 있다.
www.acmicpc.net
오랜만에 그리디 문제였습니다.
알고리즘은 아래와 같습니다.
1. 입력값들을 점수를 기준으로 내림차순 정렬을 하고 점수가 같다면 마감일이 빠른 순대로 정렬을 진행합니다.
2. 이제 [0, N) 까지 정렬된 배열을 돌면서 v[i].dueDate 날 수행할 과제 점수를 dayScore 배열 v[i].dueDate 번째 인덱스에 넣어줍니다.
2.1 while문은 (v[i].dueDate - 1) 번째 날부터 첫 날까지 순회하며 과제를 수행하는 날을 찾기 위한 반복문입니다.
2.2 과제를 수행할 수 있는 날이라면 즉, day가 0 이상이라면 dayScore 배열에 과제 점수를 저장해줍니다.
3. 마감일이 [0, 1000) 이므로 0부터 N이 아닌 0부터 MAX 까지 반복문을 돌리며 과제 점수를 모두 더해주고 출력해줍니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
const int MAX = 1000; | |
typedef struct | |
{ | |
int dueDate; | |
int score; | |
}Assignment; | |
int dayScore[MAX]; | |
bool cmp(Assignment a, Assignment b) | |
{ | |
if (a.score > b.score) | |
{ | |
return true; | |
} | |
if (a.score == b.score) | |
{ | |
if (a.dueDate < b.dueDate) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int N; | |
cin >> N; | |
vector<Assignment> v(N); | |
for (int i = 0; i < N; i++) | |
{ | |
cin >> v[i].dueDate >> v[i].score; | |
} | |
sort(v.begin(), v.end(), cmp); | |
for (int i = 0; i < N; i++) | |
{ | |
int day = v[i].dueDate - 1; | |
bool flag = true; | |
while (1) | |
{ | |
if (dayScore[day] == 0 || day < 0) | |
{ | |
break; | |
} | |
day--; | |
} | |
if (day >= 0) | |
{ | |
dayScore[day] = v[i].score; | |
} | |
} | |
int result = 0; | |
for (int i = 0; i < MAX; i++) | |
{ | |
result += dayScore[i]; | |
} | |
cout << result << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 16637번 괄호 추가하기 (6) | 2020.05.24 |
---|---|
백준 1019번 책 페이지 (0) | 2020.05.22 |
백준 1949번 우수 마을 (0) | 2020.05.18 |
백준 15829번 Hashing (0) | 2020.05.18 |
백준 16500번 문자열 판별 (0) | 2020.05.14 |