[백준] 13904번 과제 (C++)

2025. 11. 27. 19:40·Algorithm
728x90

https://www.acmicpc.net/problem/13904

 

 

이 문제는 N개의 과제가 주어질때 (마감일, 얻을 수 있는 점수)가 주어지며 이때 점수를 많이 받을 수 있는 최댓값을 출력해야 한다.

 

N이 최대 1000이므로 굉장히 작은 숫자이다.

 

즉 O(N^2)으로도 풀린다는 의미이므로 따로 visited라는 배열을 만들었고

 

과제를 점수순으로 정렬한 이후 순차적으로 확인하며 d가 채워지지 않은 경우 true로 채워넣고 만약 채워진 경우 마감일자에서부터 1일까지 확인하여 빈곳이 있는지 확인하여 채워 넣어 점수를 최대한으로 얻을 수 있는 로직을 구성하였다.

 

이 풀이는 시간복잡도가 최대 O(N^2)인 풀이이다.

 

 

정답코드

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"
#define MAX 1e9

struct coordinate {
    int x;
    int y;
    int r;
};

struct horse {
    int x;
    int y;
    int dir;
};

// int dx[] = {-1, 1, 0, 0, 1, -1, -1, 1};
// int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int N;
vector<pii> v;
bool visited[1001];

bool compare(pii a, pii b) {
    return a.second > b.second;

    if(a.second == b.second) {
        return a.first > b.first;
    }
}

void solve() {
    sort(v.begin(), v.end(), compare);

    int answer = 0;

    for(int i = 0; i < N; i++) {
        int day = v[i].first;
        int score = v[i].second;

        if(!visited[day]) {
            visited[day] = true;
            answer += score;
        }
        else {
            for(int j = day; j >= 1; j--) {
                if(!visited[j]) {
                    visited[j] = true;
                    answer += score;
                    break;
                }
            }
        }
    }

    cout << answer;
}

void input() {
    cin >> N;

    for(int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        v.push_back({a, b});
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    input();
    solve();
}

 

 

728x90
저작자표시 (새창열림)

'Algorithm' 카테고리의 다른 글

[백준] 7453번 합이 0인 네 정수 (C++)  (0) 2025.11.29
[백준] 17485번 진우의 달 여행 (Large) (C++)  (1) 2025.11.28
[백준] 12852번 1로 만들기 2 (C++)  (0) 2025.11.26
[백준] 17880번 새로운 게임 (C++)  (0) 2025.11.25
[백준] 18119번 단어암기 (C++)  (0) 2025.10.29
'Algorithm' 카테고리의 다른 글
  • [백준] 7453번 합이 0인 네 정수 (C++)
  • [백준] 17485번 진우의 달 여행 (Large) (C++)
  • [백준] 12852번 1로 만들기 2 (C++)
  • [백준] 17880번 새로운 게임 (C++)
쿨쿨.
쿨쿨.
  • 쿨쿨.
    All of the life
    쿨쿨.
  • 전체
    오늘
    어제
    • 분류 전체보기 (279)
      • Programming (47)
        • C, C++ (18)
        • Python (6)
        • Java (1)
        • HTML,CSS,JS (3)
        • SQL(DB) (13)
        • SpringBoot (3)
        • Android (2)
        • CI,CD (1)
      • Algorithm (173)
        • Review (4)
      • Security (14)
        • WebHacking (3)
        • Websecurity (11)
      • OS (19)
        • Linux (12)
        • Mac os (2)
      • 머신러닝 (1)
      • CS(Computer Science) (12)
        • 컴퓨터 네트워크 (3)
        • 컴퓨터 구조 (1)
        • 인공지능 (8)
      • Docker (1)
      • Dev Book Review (3)
        • Clean Code (1)
        • Effective Java (0)
        • Real MySQL (2)
      • SWM (1)
      • Review (6)
      • AWS (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    브루트포스
    구현
    우선순위 큐
    시뮬레이션
    에라토스테네스의 체
    트리
    다이나믹 프로그래밍
    BFS
    깊이우선탐색
    wargame
    그리디
    정렬
    우선순위큐
    백트래킹
    그래프 이론
    다익스트라
    코딩
    DP
    누적합
    이분탐색
    Leviathan
    linux
    비트마스킹
    Bandit
    c언어
    백준
    WebSecurity
    투포인터
    DFS
    재귀
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.6
쿨쿨.
[백준] 13904번 과제 (C++)
상단으로

티스토리툴바