zoukankan      html  css  js  c++  java
  • Codeforces1144B(B题)Parity Alternated Deletions

    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 n1n−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 (1n20001≤n≤2000) — the number of elements of aa.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai1060≤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.

    代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 int main() {
     6     int n,ji=0,ou=0,sum=0;
     7     cin>>n;
     8     int a[n+1],jisz[n],ousz[n];
     9     for(int i=0; i<n; i++) {
    10         cin>>a[i];
    11         if(a[i]%2==0) {
    12             ousz[ou++]=a[i];
    13         } else {
    14             jisz[ji++]=a[i];
    15         }
    16     }
    17     sort(ousz,ousz+ou);
    18     sort(jisz,jisz+ji);
    19     if(ji-ou<=1&&ji-ou>=-1) {
    20         cout<<0;
    21         return 0;
    22     } else {
    23         if(ou>ji+1) {
    24             for(int i=0; i<(ou-ji-1); i++) {
    25                 sum+=ousz[i];
    26             }
    27         } else if(ji>ou+1) {
    28             for(int i=0; i<(ji-ou-1); i++) {
    29                 sum+=jisz[i];
    30             }
    31         }
    32     }
    33     cout<<sum;
    34 }

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

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

  • 相关阅读:
    B/S架构大文件上传问题
    大文件上传解决方案-支持断点续传的文件上传插件(转)
    前端大文件分片上传
    B/S大文件分片上传
    Web大文件分片上传
    .NET大文件分片上传
    ASP.NET大文件分片上传
    leetCode(37):Implement Queue using Stacks
    poj 3928 Ping pong(树状数组)
    从零開始学Swift之Hello World进化版
  • 原文地址:https://www.cnblogs.com/yuanhang110/p/11255935.html
Copyright © 2011-2022 走看看