zoukankan      html  css  js  c++  java
  • codeforces 1201 c

    C. Maximum Median

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given an array aa of nn integers, where nn is odd. You can make the following operation with it:

    Choose one of the elements of the array (for example aiai) and increase it by 11 (that is, replace it with ai+1ai+1).
    You want to make the median of the array the largest possible using at most kk operations.

    The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array [1,5,2,3,5][1,5,2,3,5] is 33.

    Input

    The first line contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, nn is odd, 1≤k≤1091≤k≤109) — the number of elements in the array and the largest number of operations you can make.

    The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

    Output

    Print a single integer — the maximum possible median after the operations.

    Examples

    input

    Copy

    3 2
    1 3 5
    output

    Copy

    5
    input

    Copy

    5 5
    1 2 1 1 1
    output

    Copy

    3
    input

    Copy

    7 7
    4 1 2 4 3 4 4
    output

    Copy

    5
    Note

    In the first example, you can increase the second element twice. Than array will be [1,5,5][1,5,5] and it's median is 55.

    In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is 33.

    In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be [5,1,2,5,3,5,5][5,1,2,5,3,5,5] and the median will be 55.

    题意主要是说:给一个数组,每次选择给一个数加一,执行b此操作之后,问中位数最大是多少;

    显然,中位数是数组排序之后的中位数,先给数组排序,前一半不做任何处理,对后一半进行操作。之后就类似于填坑,只要当前和后边齐平就可以。

    二分去找那个数。

    要注意开ll;

     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<queue>
     6 #include<vector>
     7 #include<map>
     8 #include<math.h>
     9 using namespace std;
    10 typedef long long ll;
    11 const int mod=1e9+7;
    12 const int PI=acos(-1.0);
    13 const int maxn=200010;
    14 const int inf=1e7+5;
    15 ll n,k;
    16 ll a[maxn];
    17 ll check(ll m)
    18 {
    19     ll sum=0;
    20     int t=n/2+1;//找中位数
    21     for(int i=n;i>=t;i--){
    22         if(m>a[i]){
    23           sum+=m-a[i];//填到mid所需要的代价
    24         }
    25     }
    26    if(sum<=k){//填坑操作次数如果<=k说明操作可以实现
    27         return 1;
    28       }
    29    return 0;
    30 }
    31 int main()
    32 {
    33     cin>>n>>k;
    34     ll ans=0;
    35     for(int i=1;i<=n;i++)
    36      cin>>a[i];
    37     sort(a+1,a+1+n);
    38      ll l=1,r=2e9;
    39      while(l<=r)
    40      {
    41          int mid=(l+r)/2;
    42          if(check(mid))
    43          {
    44              ans=mid;
    45              l=mid+1;
    46          }
    47         else
    48         r=mid-1;
    49      }
    50      cout<<ans;
    51 }

     

  • 相关阅读:
    C++ 运行时类型识别 知道实例父类类型,显示出子类类型
    C++里面方便的打印日志到文件
    vs2015上配置Armadillo+openBlas
    opencl 在vs2015上遇见的问题
    Lucene子项目------------------Solr遇到的问题
    [LeetCode]Course Schedule
    [LeetCode]Minimum Size Subarray Sum
    [LeetCode]Reverse Linked List
    [LeetCode]Isomorphic Strings
    [LeetCode]Ugly Number
  • 原文地址:https://www.cnblogs.com/sweetlittlebaby/p/12435179.html
Copyright © 2011-2022 走看看