zoukankan      html  css  js  c++  java
  • xtu summer individual 5 A

    To Add or Not to Add

    Time Limit: 2000ms
    Memory Limit: 262144KB
    This problem will be judged on CodeForces. Original ID: 231C
    64-bit integer IO format: %I64d      Java class name: (Any)
     

    A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.

    However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).

    Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.

     

    Input

    The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.

    The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.

     

    Output

    In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.

     

    Sample Input

    Input
    5 3
    6 3 4 0 2
    Output
    3 4
    Input
    3 4
    5 5 5
    Output
    3 5
    Input
    5 3
    3 1 2 2 1
    Output
    4 2

    Source

     
    解题:二分,排序后二分,这也可以二分啊?尼玛,还真可以二分!好吧,二分就二分吧
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f3f
    11 using namespace std;
    12 const int maxn = 100010;
    13 int d[maxn],n,k,temp;
    14 LL sum[maxn];
    15 int check(int p){
    16     for(int i = p; i <= n; i++){
    17         if((LL)d[i]*p - sum[i]+sum[i-p] <= k)
    18             return i;
    19     }
    20     return  -1;
    21 }
    22 int main(){
    23     int i,lt,rt,mid,num,ans;
    24     while(~scanf("%d%d",&n,&k)){
    25         for(i = 1; i <= n; i++)
    26             scanf("%d",d+i);
    27         sort(d+1,d+n+1);
    28         sum[0] = 0;
    29         for(i = 1; i <= n; i++)
    30             sum[i] = sum[i-1]+d[i];
    31         lt = 1;
    32         rt = n;
    33         while(lt <= rt){
    34             mid = (lt+rt)>>1;
    35             temp = check(mid);
    36             if(temp == -1) rt = mid-1;
    37             else{
    38                 ans = temp;
    39                 num = mid;
    40                 lt = mid+1;
    41             }
    42         }
    43         printf("%d %d
    ",num,d[ans]);
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    钢镚儿冲刺一周期第七天
    代码大全(第二版)阅读笔记01
    组队开发——地铁路线查询
    学习进度——第五周
    学习进度——第四周
    返回一个整数数组中最大子数组的和(文件)
    学习进度——第三周
    返回一个整数数组中最大子数组的和
    自我介绍
    学习进度——第二周
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3895722.html
Copyright © 2011-2022 走看看