문제 링크입니다: https://www.acmicpc.net/problem/2456
2456번: 나는 학급회장이다
첫째 줄에는 반의 학생들의 수 N (3<=N<=1,000)이 주어진다. 다음 N개의 각 줄에는 각 학생이 제출한 회장후보 3명에 대한 선호 점수가 주어지는 데, 첫 번째 점수는 후보 1번에 대한 점수이고 두 번째 점수는 후보 2번에 대한 점수이고 세 번째 점수는 후보 3번에 대한 점수이다. 이 세 점수는 서로 다르며, 1, 2, 3이 정확히 한 번씩 나타난다.
www.acmicpc.net
정렬 조건을 잘 짜야하는 문제였습니다.
동일한 점수일 경우 3점, 2점 투표가 더 많은 순으로 당선이 되므로,
weightedSum이라는 별도 변수를 만들어 투표 점수가 높은 순으로 더 가중치를 준 값을 더해나갔습니다.
이후 후보자들을 점수가 높은순으로 정렬하였고 점수가 동일하다면 weightedSum이 높은순으로 정렬했습니다.
* 문제를 잘 읽어야 합니다. 동일한 점수일 경우 0과 함께 최고 점수도 출력해야합니다.
This file contains 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 = 3; | |
typedef struct | |
{ | |
int idx; | |
int sum; | |
int weightedSum; | |
}Vote; | |
bool cmp(Vote a, Vote b) | |
{ | |
if (a.sum > b.sum) | |
{ | |
return true; | |
} | |
if (a.sum == b.sum) | |
{ | |
if (a.weightedSum > b.weightedSum) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int N; | |
cin >> N; | |
vector<Vote> candidates(3); | |
// 인덱스 부여 | |
for (int i = 0; i < 3; i++) | |
{ | |
candidates[i].idx = i + 1; | |
} | |
for (int i = 0; i < N; i++) | |
{ | |
for (int j=0; j<3; j++) | |
{ | |
int weight; | |
cin >> weight; | |
candidates[j].sum += weight; | |
switch (weight) | |
{ | |
case 2: | |
weight *= 10; | |
break; | |
case 3: | |
weight *= 100; | |
break; | |
} | |
candidates[j].weightedSum += weight; | |
} | |
} | |
sort(candidates.begin(), candidates.end(), cmp); | |
if (candidates[0].weightedSum == candidates[1].weightedSum) | |
{ | |
cout << 0 << " " << candidates[0].sum << "\n"; | |
return 0; | |
} | |
cout << candidates[0].idx << " " << candidates[0].sum << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 17779번 게리맨더링 2 (2) | 2020.04.30 |
---|---|
백준 17825번 주사위 윷놀이 (0) | 2020.04.30 |
백준 2531번 회전초밥 (0) | 2020.04.26 |
백준 2530번 인공지능 시계 (0) | 2020.04.26 |
백준 2526번 싸이클 (0) | 2020.04.26 |