본문 바로가기

Algorithm/Greedy

(C++) - 백준(BOJ) 17509 : And the Winner Is... Ourselves!

반응형

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

greedy문제였습니다.

📕 풀이방법

📔 입력 및 초기화

정답을 출력할 penalty, 현 시간을 저장할 curTime, (문제 푸는시간, 틀린 현황)이 pair로 저장되는 vector v를 선언한 후 정보를 입력받습니다.

📔 풀이과정

문제풀 때 걸리는 시간이 문제를 틀린 현황과는 독립적이므로 단순히 빨리 풀리는 문제를 먼저 풀게되는 경우만 고려하면 됩니다.따라서 v.first에 대해서만 sort를 한 후 penalty를 계산해 출력합니다.penalty는 현 시간 + 문제풀때 걸린 시간 + 20 * 현 문제 틀린 횟수 가 됩니다.

📔 정답출력

penalty를 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;

int penalty, curTime;
vector <pii> v(11);

int main(){
  for(int i = 0; i < 11; i++)
    cin >> v[i].first >> v[i].second;
  sort(v.begin(), v.end());

  for(int i = 0; i < v.size(); i++){
    penalty += (curTime + v[i].first + 20 * v[i].second);
    curTime += v[i].first;
  }
  cout << penalty;
}