※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 24855번 문제인 Natives이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/24855 

 

24855번: Natives

Captain Cook and his team were captured by island natives. Not to be eaten, the adventurers must give some treasures to natives. It turned out that the captain has $n$ treasures. Chieftain of the natives agrees to let the captain and his team go, if they g

www.acmicpc.net

각 선물들을 가치순으로 정렬해, 가치가 낮은 순으로 N이 홀수라면 N/2+1개를, 짝수라면 N/2개를 원주민에게 주고 남은 선물을 가지고 돌아가는 것으로 문제를 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <algorithm>
using namespace std;

int N;
int arr[1000];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N;
	for (int i = 0; i < N; i++) cin >> arr[i];
	sort(arr, arr + N);

	int ans = 0;
	for (int i = (N & 1) ? N / 2 + 1 : N / 2; i < N; i++) {
		ans += arr[i];
	}

	cout << ans;
}
728x90

+ Recent posts