B. Parity Alternated Deletions

Polycarp has an array aa consisting of nn 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−1n−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 nn (1≤n≤20001≤n≤2000) — the number of elements of aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1060≤ai≤106), where aiai is the ii-th element of aa.

Output

Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.

代码:

 #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main() {
int n,ji=,ou=,sum=;
cin>>n;
int a[n+],jisz[n],ousz[n];
for(int i=; i<n; i++) {
cin>>a[i];
if(a[i]%==) {
ousz[ou++]=a[i];
} else {
jisz[ji++]=a[i];
}
}
sort(ousz,ousz+ou);
sort(jisz,jisz+ji);
if(ji-ou<=&&ji-ou>=-) {
cout<<;
return ;
} else {
if(ou>ji+) {
for(int i=; i<(ou-ji-); i++) {
sum+=ousz[i];
}
} else if(ji>ou+) {
for(int i=; i<(ji-ou-); i++) {
sum+=jisz[i];
}
}
}
cout<<sum;
}

思路分析:如果奇偶数量差在1和-1之间输出0,因为肯定可以选完。否则根据偶数比奇数多的个数或奇数比偶数多的个数从排序后数组中输出相应个数。

题目链接:https://codeforces.com/contest/1144/problem/B

Codeforces1144B(B题)Parity Alternated Deletions的更多相关文章

  1. CodeForces Round #550 Div.3

    http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...

  2. CF550 DIV3

    A - Diverse Strings CodeForces - 1144A A string is called diverse if it contains consecutive (adjace ...

  3. UVA.11464 Even Parity (思维题 开关问题)

    UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...

  4. CodeForces 297A Parity Game (脑补题)

    题意 一个01串,可以有两种操作:①在末尾添加parity(a):②删除开头的一个字符.其中parity(a),当串中1的个数为奇数时为1,偶数时为0.问某个01串是否可以通过若干操作变成另一个01串 ...

  5. 2017微软秋招A题

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 There is an integer array A1, A2 ...AN. Each round you may ch ...

  6. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  7. 51nod1204 Parity

    如果sm[j]和sm[i]奇偶性相同,那么(i+1,j)个数为偶数如果奇偶性相同看成是朋友,不同的看成是敌人,那么就跟bzoj1370的做法差不多了. 如果奇偶性相同,就将x和y合并,x+n,y+n合 ...

  8. POJ 1733 Parity game (并查集)

    Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6816   Accepted: 2636 Descr ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. python列表的内建函数

    list.append(obj) 向列表中添加一个对象obj list.count(obj) 返回一个对象obj 在列表中出现的次数 list.extend(seq)a 把序列seq 的内容添加到列表 ...

  2. 【React】react学习笔记01-概念与基本使用

      俺为啥要学这玩意:   家里的杂事好不容易处理完了,开始正式静下心来学习~博主是做后端开发的,js基础不深,之前也是用React写过许多东西了,但是基本上都是用的CV大法,别人的组 件修修改改拿来 ...

  3. 获取Spring中的Bean

    1.Utils工具类 package com.xxx.common.helper; import org.springframework.beans.BeansException; import or ...

  4. mysql-8.0-winx64安装以及修改密码

    一.下载安装包(https://dev.mysql.com/downloads/mysql/) 二.添加my.ini配置文件 打开刚刚解压的文件夹 C:\mysql-8.0.16-winx64,在该文 ...

  5. C# Redis分布式锁(基于ServiceStack.Redis)

    相关的文章其实不少,我也从中受益不少,但是还是想自己梳理一下,毕竟自己写的更走心! 首先给出一个拓展类,通过拓展方法实现加锁和解锁. 注:之所以增加拓展方法,是因为合理使用拓展类(方法),可以让程序更 ...

  6. python的is与==的区别

    is is比较的是两个变量的地址值,如果地址值正确,则返回True,否则返回False,实例如下: 如图所示,a,b列表的数值相等,但地址是不相等的,所以返回True,与值无关 == ==比较的是两个 ...

  7. Keep It Simple

    The KISS principle, or Keep It Simple, Stupid, spans many trades, industries, and professions. The m ...

  8. 寻觅Azure上的Athena和BigQuery (二):神奇的PolyBase

    前情回顾 在“数据湖”概念与理论逐渐深入人心的今天,面向云存储的交互式查询这个需求场景显得愈发重要.这是因为原生的云存储(主要指S3这样的对象存储)既能够容纳大容量的明细数据,又能在性能和成本间取得一 ...

  9. APP系统架构设计初探

    一,图片体验的优化. 在手机上显示图片,速度是一个非常重要的体验点,试想,如果您打开一个网站,发现里面的图片一直显示失败或者是x,稍微做得好一点的,可能是一个不消失的loading或者是菊花等等,但不 ...

  10. 七牛云图床和Markdown使用

    七牛云图床和Markdown使用 1.图床是什么? 图床一般是指储存图片的服务器,有国内和国外之分.国外的图床由于有空间距离等因素决定访问速度很慢影响图片显示速度.国内也分为单线空间.多线空间和cdn ...