Gym - 100989G

There are K hours left before Agent Mahone leaves Amman! Hammouri doesn‘t like how things are going in the mission and he doesn‘t want to fail again. Some places have too many students covering them, while other places have only few students.

Whenever Hammouri commands a student to change his place, it takes the student exactly one hour to reach the new place. Hammouri waits until he is sure that the student has arrived to the new place before he issues a new command. Therefore, only one student can change his place in each hour.

Hammouri wants to command the students to change their places such that after K hours, the maximum number of students covering the same place is minimized.

Given the number of students covering each place, your task is to find the maximum number of students covering the same place after K hours, assuming that Hammouri correctly minimizes this number.

Input

The first line of input contains two integers M (2?≤?M?≤?105) and K (1?≤?K?≤?109), where M is the number of places and K is the number of hours left before Agent Mahone leaves Amman.

The second line contains M non-negative integers, each represents the number of students covering one of the places. Each place is covered by at most 109 students.

Output

Print the maximum number of students covering a place after K hours, assuming that Hammouri minimized this number as much as possible in the last K hours.

Examples

Input

5 43 4 1 4 9

Output

5

Input

2 10000000001000000000 4

Output

500000002

Input

5 32 2 2 2 1

Output

2---------------------------------------二分,注意边界刚开始,以平均值和最大值为边界,计算可移动的数目小于题目所给值即可
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <iomanip>
 6 #include <string>
 7 #include <set>
 8 #include <queue>
 9 #include <stack>
10 #include <map>
11 #include <algorithm>
12
13 using namespace std;
14
15 typedef long long LL;
16 const int INF = 0x3f3f3f3f;
17 const int MAXN = 100005;
18 const int MOD = 1e9+7;
19
20 int main()
21 {
22     int n, i;
23     LL k, num[MAXN], mx = 0, sum = 0;
24     cin >> n >> k;
25     for(i = 0;i < n;++i)
26     {
27         cin >> num[i];
28         mx = max(mx, num[i]);
29         sum += num[i];
30     }
31 //    int ave = mx / m;
32 //    if(mx % m)
33 //        ave++;
34 //    等同于下行码
35     LL ave = (sum + n - 1) / n;
36     LL l = ave, r = mx, mid, ans;
37     while(l <= r)
38     {
39         mid = (l + r) >> 1;
40         sum = 0;
41         for(i = 0;i < n;++i)
42             if(num[i] > mid)
43             sum += (num[i] - mid);
44         if(sum <= k)
45         {
46             ans = mid;
47             r = mid - 1;
48         }
49         else
50             l = mid + 1;
51 //        cout << sum << endl;
52     }
53     cout << ans << endl;
54     return 0;
55 }
 

原文地址:https://www.cnblogs.com/shuizhidao/p/9280022.html

时间: 2024-10-12 19:56:42

Gym - 100989G的相关文章

CodeForces Gym - 100989G(二分)

题目链接:https://vjudge.net/contest/236677#problem/G 题目意思:有m个地方,有k小时,每个地方有a[i]个人.你要进行操作(一个小时可以移动一个人到另一个地方,或者不移动),最后使得m个地方的人数最多值变成最小情况.(就是移动人使每个地方的人接近平均值,就是统计学中方差就小) 思路:求出平均值,因为平均值是不变的,答案一定在平均值和最大值之间,二分就是要找到这个区间,左边临界值就是平均值,右边临界值就是最大值.在与当前最大值maxx二分得平局值mid,

Gym - 100989G (二分法)

There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going in the mission and he doesn't want to fail again. Some places have too many students covering them, while other places have only few students. Wheneve

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

B - Average Gym - 101161B 组合数学

http://codeforces.com/gym/101161/attachments 今天被卡常了,其实是自己对组合数技巧研究的不够. 如果是n, m <= 1e5的,然后取模是质数,那么可以用费马小定理. 如果n, m都比较小,那么其实是直接杨辉三角.不用逆元那些. 这题的思路是,枚举每一一个ave,然后总和就是n * ave 相当于方程  x1 + x2 + .... + xn = n * ave中,在0 <= x[i] <= full的情况下,不同解的个数中,使得x[i] ==

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

CodeForces Gym 101063C 二进制压缩

http://codeforces.com/gym/101063/problem/C 给n个人,m样物品,每个人可以从物品中选择几样.两人选择物品的交集元素个数比上并集元素个数如果大于某个比例即可将两人配对.求配对数. n的范围是1e5,直接比较所有人的选择会TLE,应该将所有选择物品的情况用二进制压缩,m最大是10,情况数目小于2048,可以接受.注意配对总数范围应为long long. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> i

Gym 101246H ``North-East&#39;&#39;(LIS)

http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的点.输出它可能经过的点和一定会经过的点. 思路: 分析一下第一个案例,在坐标图上画出来,可以发现,他最多可以经过4个点,有两种方法可以走. 观察一下,就可以发现这道题目就是要我们求一个LIS. 首先,对输入数据排一下顺序,x小的排前,相等时则将y大的优先排前面. 用二分法求LIS,这样在d数组中就可

Gym 100712I Bahosain and Digits(开关翻转问题)

http://codeforces.com/gym/100712/attachments 题意: 给出一串数字,每次选择连续的k个数字加上任意数(超过10就取余),最后要使得所有数字都相等,求最大的k. 思路: 开关翻转问题. 算法具体可以参考<挑战程序竞赛>常用技巧篇. 这道题目就是在枚举k的同时再枚举一下最后要转换成的数字即可. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring>

很好的脑洞题:dfs+暴力 Gym - 101128A Promotions

http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次性取出来的个数为a(或b).当然,取出来的种类可能有很多种(即一个集合),问,这个集合中有多少个数字是相同的. 第一个输出集合长度为a的,第二个输出集合长度为b的,第三个输出无论如何都无法被取出的个数. 思路:建立正向图和反向图. 定义pair<int, int> interval[i] 表示第i