B. Parity Alternated Deletions
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp has an array a consisting of n integers.
He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n−1 elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-… or odd-even-odd-even-…) of the removed elements. Polycarp stops if he can’t make a move.
Formally:
If it is the first move, he chooses any element and deletes it;
If it is the second or any next move:
if the last deleted element was odd, Polycarp chooses any even element and deletes it;
if the last deleted element was even, Polycarp chooses any odd element and deletes it.
If after some move Polycarp cannot make a move, the game ends.
Polycarp’s goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.
Help Polycarp find this value.
Input
The first line of the input contains one integer n (1≤n≤2000) — the number of elements of a.
The second line of the input contains n integers a1,a2,…,an (0≤ai≤106), where ai is the i-th element of a.
Output
Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.
Examples
inputCopy
5
1 5 7 8 2
outputCopy
0
inputCopy
6
5 1 2 4 6 3
outputCopy
0
inputCopy
2
1000000 1000000
outputCopy
1000000
题意 给你包含n个元素的数组 你能先删一个数 这个数如果是偶 你下个就要删奇 如果这个数是奇数 你下个就要删偶数
问你能删完所有的满足剩下的最小值是多少
做法 我们只要按题意模拟删两个元素的过程 用两个优先队列贪心的去删即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <ctype.h>//isalpha,isdight,toupper
#include <map>
#include <sstream>
typedef long long ll;
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=1010;
int n;
priority_queue<int>q,q_,Q,Q_;
int a[2005];
int sum,sum_;
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
sum+=a[i];
sum_+=a[i];
if(a[i]&1==1)q.push(a[i]),Q.push(a[i]);
else q_.push(a[i]),Q_.push(a[i]);
}
while(!q.empty()){
int now=q.top();
q.pop();
sum-=now;
if(q_.empty())break;
now=q_.top();
q_.pop();
sum-=now;
}
while(!Q_.empty()){
int now=Q_.top();
Q_.pop();
sum_-=now;
if(Q.empty())break;
now=Q.top();
Q.pop();
sum_-=now;
}
printf("%d\n",min(sum,sum_));
return 0;
}