https://www.acmicpc.net/problem/2036
2036번: 수열의 점수
n개의 정수로 이루어진 수열이 있다. 이 수열에서 한 정수를 제거하거나, 또는 두 정수를 제거할 수 있다. 한 정수를 제거하는 경우에는 그 정수가 점수가 되고, 두 정수를 제거하는 경우에는 두
www.acmicpc.net
도서관 문제와 매우 비슷한 문제. 양수에서의 1과, 음수에서 남는 수들을 0과 조합해야하는 것을 조심하면 쉽게 풀 수 있다.
#include <cstdio>
#include <algorithm>
using namespace std;
bool comp(long long int a, long long int b){
return a>b;
}
int main(void){
long long int n, ans=0, num, plus[100001]={}, minus[100001]={}, pcnt=0, mcnt=0;
scanf("%lld", &n);
for(int i=0; i<n; i++){
scanf("%lld", &num);
if(num>0)
plus[pcnt++]=num;
else
minus[mcnt++]=num*(-1);
}
sort(plus, plus+pcnt, comp);
sort(minus, minus+mcnt, comp);
if(mcnt!=0){
for(int i=0; i<mcnt; i+=2){
if(i==mcnt-1)
ans -= minus[i];
else
ans += minus[i]*minus[i+1];
}
}
if(pcnt!=0){
for(int i=0; i<pcnt; i+=2){
if(i==pcnt-1)
ans += plus[i];
else if(plus[i]<=1 ||plus[i+1]<=1)
ans += (plus[i]+plus[i+1]);
else
ans += plus[i]*plus[i+1];
}
}
printf("%lld",ans);
}
'알고리즘 문제' 카테고리의 다른 글
[C/C++ 백준 2609번] 최대공약수와 최소공배수 (Silver 5) (0) | 2020.08.09 |
---|---|
[C/C++ 백준 2164번] 카드2 (Silver 4) (0) | 2020.08.09 |
[C/C++ 백준 1461번] 도서관 (Silver 1) (0) | 2020.08.04 |
[C/C++ 백준 1946번] 신입 사원 (Silver 1) (0) | 2020.08.04 |
[C/C++ 백준 2529번] 부등호 (Silver 2) (0) | 2020.08.03 |