Codeforces1144B(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.
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.
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,因为肯定可以选完。否则根据偶数比奇数多的个数或奇数比偶数多的个数从排序后数组中输出相应个数。
Codeforces1144B(B题)Parity Alternated Deletions的更多相关文章
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- CF550 DIV3
A - Diverse Strings CodeForces - 1144A A string is called diverse if it contains consecutive (adjace ...
- UVA.11464 Even Parity (思维题 开关问题)
UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...
- CodeForces 297A Parity Game (脑补题)
题意 一个01串,可以有两种操作:①在末尾添加parity(a):②删除开头的一个字符.其中parity(a),当串中1的个数为奇数时为1,偶数时为0.问某个01串是否可以通过若干操作变成另一个01串 ...
- 2017微软秋招A题
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 There is an integer array A1, A2 ...AN. Each round you may ch ...
- 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
1.POJ 1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5744 Accepted: ...
- 51nod1204 Parity
如果sm[j]和sm[i]奇偶性相同,那么(i+1,j)个数为偶数如果奇偶性相同看成是朋友,不同的看成是敌人,那么就跟bzoj1370的做法差不多了. 如果奇偶性相同,就将x和y合并,x+n,y+n合 ...
- POJ 1733 Parity game (并查集)
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6816 Accepted: 2636 Descr ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- Laravel:php artisan key:generate三种报错解决方案,修改默认PHP版本(宝塔面板)
为了兼容N多个网站,服务器上有3个PHP版本5.3/5.6/7.2.宝塔默认为5.3,但是laravel5.7并不支持,所以在创建线上 .env 环境配置文件,初始化应用配置时候报错了. cp .en ...
- SYN2306A型 GPS北斗双模授时板
SYN2306A型 GPS北斗双模授时板 北斗gps时钟北斗授时设备北斗时钟同步系统使用说明视频链接: http://www.syn029.com/h-pd-211-0_310_36_-1.html ...
- 一、Linux常用命令
1.ls 作用:列出文件信息,默认为当前目录下 语法: -a:列出所有的文件,包括以.开头的隐藏文件 -d:列出目录本身,并不包含目录中的文件 -h:和-l一起使用,文件大小人类易读 -l:长输出(“ ...
- Tido 习题-二叉树-树状数组求逆序对
这里给大家提供一个全新的求逆序对的方法 是通过树状数组来实现的 题目描述 样例输入 Copy 5 2 3 1 5 4 样例输出 Copy 3 提示 #include<iostream ...
- C++按格式接收输入字符(京东,滴滴,360笔试必用)
头一次起这种标题,为了对得起这个标题,我尽量多写点~ 最近还是一边实习一遍投简历--笔试--面试,然而发现了自己的好多问题. 在答了京东笔试(滴滴,360也是这样的)的题后,发现与腾讯,阿里等公司的不 ...
- 基于Google Earth Engine的全国地表温度反演
国内研究landsat8温度反演的人员很多,但是现有算法一般都是一景为例子,进行开展. 这有一个局限性,当研究的尺度很大时,就需要比较大的运算量了,例如全省温度,全国温度,全球温度,当然大家可能会说, ...
- kubernetes client-go解析
注:本次使用的client-go版本为:client-go 11.0,主要参考CSDN上的深入浅出kubernetes之client-go系列,建议看本文前先参考该文档.本文档为CSDN文档的深挖和补 ...
- wireshark和nmap
原创博客,转载请注出处! 关于Wireshark和Nmap实验报告 Wireshark部分 一.捕获三次握手报并分析 如上图所示,简述三次握手流程: Step1.客户端由56780号端口向服务器443 ...
- S7-300CPU存储器介绍及存储卡使用
1. S7 300存储区概述 S7-300 PLC的存储区可以划分为四个区域:装载存储器(Load Memory).工作存储器(Work Memory). 系统存储器(System Memory)和保 ...
- 一台服务器通过nginx配置多个域名(80端口)
1. 问题描述 多个域名对应一个服务器,为了避免域名后增加端口号,两个域名都需要占用80端口号,使用nginx来进行配置. 2. 解决方案 目前项目中,线上正在使用(100%可用)多域名对应一个服务器 ...