https://www.acmicpc.net/problem/1713
1713번: 후보 추천하기
첫째 줄에는 사진틀의 개수 N이 주어진다. (1≤N≤20) 둘째 줄에는 전체 학생의 총 추천 횟수가 주어지고, 셋째 줄에는 추천받은 학생을 나타내는 번호가 빈 칸을 사이에 두고 추천받은 순서대로 �
www.acmicpc.net
구현 알고리즘이라 그런지 진짜 구현만 하면 되지만, 조건이 굉장히 많아 까다로웠다. 따져야 할 조건이 추천수와 카운터도 있으므로 2차원 배열로 만들어주고, 조건에 따라 차근차근 해결해 주면 된다.
코드는 다음과 같다.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(void){
int N, T, number, fullcnt=0, deletestudent;
int minchu, mintime, answerarray[21];
bool exist;
scanf("%d %d",&N,&T);
int Picture[N][3]={};
memset(Picture, -1, N*3*sizeof(int));
for(int i=0; i<T; i++){
exist = false;
scanf("%d",&number);
//있는지 찾는다.
for(int j=0; j<N; j++){
if(Picture[j][0] == number){//있으면
Picture[j][1]++;
exist = true;
}
}
if(!exist){//존재하지 않는경우
if(fullcnt==N){//다 찼을경우
deletestudent = -1;
minchu = 1001;//최소 추천 카운터
mintime = 1001; //최소 시간 카운터
for(int k=0; k<N; k++){
if(minchu>Picture[k][1]){
minchu = Picture[k][1];
mintime = Picture[k][2];
deletestudent = k;
}
else if(minchu == Picture[k][1]){
if(mintime < Picture[k][2]){
minchu = Picture[k][1];
mintime = Picture[k][2];
deletestudent = k;
}
}
}
Picture[deletestudent][0] = number;
Picture[deletestudent][1] = 1;
Picture[deletestudent][2] = 0;
}
else{//다 차지 않았을 경우
Picture[fullcnt][0] = number;
Picture[fullcnt][1] = 1;
Picture[fullcnt][2] = 0;
fullcnt++;
}
}
for(int q=0; q<fullcnt; q++){
Picture[q][2]++;//시간증
}
}
for(int i=0; i<N; i++){
answerarray[i]=Picture[i][0];
}
sort(answerarray, answerarray + N);
for(int i=0; i<N; i++){
printf("%d ",answerarray[i]);
}
}
'알고리즘 문제' 카테고리의 다른 글
[C/C++ 백준 2920번] 음계 (Bronze 2) (Class 1) (0) | 2020.07.26 |
---|---|
[C/C++ 백준 1008번] A/B (Bronze IV) (Class 1) (0) | 2020.07.26 |
[C/C++ 백준 2563번] 색종이 (Silver 5) (0) | 2020.07.24 |
[C/C++ 백준 1748번] 수 이어 쓰기 1 (Silver 3) (0) | 2020.07.22 |
[C/C++ 백준 2960번] 에라토스테네스의 체 (Silver 4) (0) | 2020.07.22 |