알고리즘 문제
[C/C++ 백준 1041번] 주사위 (Silver 1)
새파란 공대생
2020. 6. 6. 21:47
https://www.acmicpc.net/problem/1041
1041번: 주사위
첫째 줄에 N이 주어진다. 둘째 줄에 주사위에 쓰여 있는 수가 주어진다. 위의 그림에서 A, B, C, D, E, F에 쓰여 있는 수가 차례대로 주어진다. N은 1,000,000보다 작거나 같은 자연수이고, 쓰여 있는 수�
www.acmicpc.net
핵심 아이디어는, 마주보는 두 면들중 작은 면들을 골라 만든다는 것이다. 문제를 푸려면 6개면 중 최소 숫자의 면, 6개면 중 2개 면의 숫자합 중 최소, 3개 면의 숫자합 중 최소를 찾아야한다. 이때 마주보는 두 면들끼리 비교해서 작을 것들끼리 조합해 만들면 쉽게 풀 수 있다. 자료형을 long long int 로 잘 맞추자,,
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main(void){
long long int N, Dice[6], ans=0;
scanf("%lld", &N);
long long int Max=-1, Min=51, Maxcnt, Mincnt;
long long twoMin=98765, threeMin=98765, twoMincnt;
for(int i=0; i<6; i++){
scanf("%lld",&Dice[i]);
if(Max<Dice[i]){
Max=Dice[i];
Maxcnt=i;
}
if(Min>Dice[i]){
Min=Dice[i];
Mincnt=i;
}
}
if(N==1){
for(int i=0; i<6; i++){
ans += Dice[i];
}
ans -= Max;
printf("%lld", ans);
}
else{
int a, b, c;
a=min(Dice[0], Dice[5]);
b=min(Dice[1], Dice[4]);
c=min(Dice[2], Dice[3]);
twoMin = min (min(a+b, b+c), c+a);
threeMin = a + b + c;
printf("%lld",(5*N*N-16*N+12)*Min+(8*N-12)*twoMin+(4*threeMin));
}
}