본문 바로가기

Algorithm_BOJ(백준)/그리디(Greedy Algorithm)

[백준 12845 c++] 모두의 마블

728x90
반응형

문제 링크

www.acmicpc.net/problem/12845

 

 

문제 접근

// 접근: 그리디는 눈치챈 다음 최대를 구하는 것이므로 최대값이 가장 많이 중복이 되어야 한다고 생각

 





 

문제 풀이

// 풀이: 결국 더하는 과정의 결과값은 처음에 최대값에서 시작하면 최댓값은 n-2번 더하고 나머지 총 합을 더하면 최대값

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

주의

 

 

 

 

개념

 

 

개념리스트 입력하면서 2차원 리스트 만들기 lst = [list(map(int,input().split())) for _ in range(n)]

 

 

 

소스코드

 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio> // c 문법 헤더파일
#include<string> // c++ 문자열 클래스
#include<vector> // 동적배열 라이브러리
#include<stack>
#include<queue>
#include<algorithm>  // sort와 unique 사용
#include<cmath> // 제곱이나 루트함수 사용
#include<cstring> // memset 함수
#include <set>
#include <map> // map구조체
#include <numeric> //accumulate(v.begin(), v.end(), 0);

// [백준 12845 c++] 모두의 마블
// 접근: 그리디는 눈치챈 다음 최대를 구하는 것이므로 최대값이 가장 많이 중복이 되어야 한다고 생각
// 풀이: 결국 더하는 과정의 결과값은 처음에 최대값에서 시작하면 최댓값은 n-2번 더하고 나머지 총 합을 더하면 최대값


using namespace std; // cin,cout 편하게 사용 라이브러리

int n;
vector<int> card;

int main()
{
	// IO 속도 향상
	//ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n;
	int sum = 0;
	int maxnum=0;
	for (int i = 0; i < n; i++)
	{
		int tp;
		cin >> tp;
		sum += tp;
		maxnum = max(tp, maxnum);
		card.push_back(tp);
	}

	int result = (n - 2) * maxnum + sum;
	cout << result;

	return 0;
}

 

반응형