zoukankan      html  css  js  c++  java
  • Leetcode-1005 Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)

     1 #define pb push_back
     2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
     3 const int maxn = 50003;
     4 
     5 class Solution
     6 {
     7     public:
     8         int largestSumAfterKNegations(vector<int>& A, int K)
     9         {
    10             int hash[202] {0};
    11             int fnum = 0;
    12             _for(i,0,A.size())
    13             {
    14                 if(A[i]<0)
    15                     fnum ++;
    16                 hash[A[i]+100] ++;
    17             } 
    18             if(fnum<=K)
    19             {
    20                 
    21                 _for(i,0,100)
    22                 {
    23                     hash[200-i] += hash[i];
    24                     hash[i] = 0; 
    25                 }
    26                 K -= fnum;
    27                 
    28                 if(K&0x1)
    29                 {
    30                     _for(i,100,201)
    31                     {
    32                         if(hash[i])
    33                         {
    34                             hash[200-i]++;
    35                             hash[i] --;
    36                             break;
    37                         }
    38                     }
    39                 }
    40             }
    41             else
    42             {
    43                 int index = 0;
    44                 while(K --)
    45                 {
    46                     while(hash[index]==0)
    47                         index ++;
    48                     hash[index] --;
    49                     hash[200-index] ++; 
    50                 }
    51             }
    52 
    53             int rnt = 0;
    54             _for(i,0,201)
    55             {
    56                 rnt += hash[i] * (i-100);
    57             }
    58             return rnt;
    59         }
    60 };

    分类讨论,随便乱搞过了

  • 相关阅读:
    我要变牛逼
    java web
    导师选择
    2.1进程
    来到博客园写东西的第一天
    简单的页面布局
    html5
    第一个servlet程序
    java2D
    字节流 文件字节流 缓冲字节流
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10504849.html
Copyright © 2011-2022 走看看