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 小希的迷宫 基 ...
随机推荐
- Python连载15-高阶函数&map映射
一.高阶函数 1.定义:把函数作为参数使用的函数叫做高阶函数 (1)变量可以设置为赋值 a=100 b=a (2)函数名称就是一个变量 def FunA(): print("FunA()&q ...
- 常用URL分享,实用地址
常用地址 文库文档免费下载地址1:http://www.hiwenku.com/ 文库文档免费下载下载2:http://www.20009.net/wk.html google地图拾取器:http:/ ...
- Android native进程间通信实例-binder篇之——用parcel传输数组
和之前稍微不同,这次要稍微分析一下 Parce.cpp 和 android_os_Parcel.cp p的源码,为的是能够掌握调试技巧,后续传输其它类型数据就能举一反三了! 1. 代码共享 这次 ...
- Linux系统下解锁Oracle的Scott用户
1).在Oracle用户下面输入命令:lsnrctl status查看监听是否开启,如果未开启则需要开启监听,输入命令:lsnrctl start; 2).如果没有设置监听的话需要先建立一个监听,然后 ...
- 【React】存储全局数据
参考链接:https://segmentfault.com/a/1190000012057010?utm_source=tag-newest webstorage webstorage是本地存储,存储 ...
- C#使用 SSL Socket 建立 Client 与 Server 连接
当 Client 与 Server 需要建立一个沟通的管道时可以使用 Socket 的方式建立一个信道,但是使用单纯的 Socket 联机信道可能会担心传输数据的过程中可能被截取修改因而不够安全,为了 ...
- Linux运维工程师学习成长路线
不过大家的留言都很精彩,希望大家也可以去留言区逛一逛~~ 好在这不是最后一期送书,之前已经有了好多活动,小编一定继续为大家多送些福利. 希望大家可以一如既往的关注脚本之家,支持爱你们的小编,共同进步! ...
- Nginx查看并发连接数
Nginx查看并发连接 通过界面查看 通过界面查看通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 --with-http_stub_status_module 然后 ...
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- C# 连接数据库等
SqlConnection连接池:可以通过连接字符串配置连接池.对象池技术:HttpApplication :Asp.Net生产者 消费者. 线程.应用程序跟数据连接非常耗时,而且连接使用非常频繁,使 ...