链接:http://codeforces.com/contest/1244/problem/E

题意:

给定包含$n$个数的数组,你可以执行最多k次操作,使得数组的一个数加1或者减1。

问合理的操作,使得数组中最大的数和最小的数差值最小。

思路:

二分答案,重点是检查的时候需要跑两遍。

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize(4)
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /**********showtime************/ const int maxn = 1e5+;
int a[maxn];
ll sum[maxn];
int n;
ll k;
bool check(int dif) {
int le = ;
for(int i=; i<=n; i++) {
while(le < i && a[i] - a[le] > dif) le++;
ll zuo = 1ll * (le-) * (a[i] - dif) - sum[le-];
ll you = sum[n] - sum[i] - 1ll * (n - i) * a[i];
if(zuo + you <= k) return true;
}
int ri = ;
for(int i=; i<=n; i++) {
while(ri + <= n && a[ri+] - a[i] <= dif) ri++; ll zuo = 1ll * (i-) * a[i] - sum[i-];
ll you = sum[n] - sum[ri] - 1ll * (n - ri) * (a[i] + dif); if(zuo + you <= k) return true;
} return false;
}
int main(){
scanf("%d%lld", &n, &k);
for(int i=; i<=n; i++) {
scanf("%d", &a[i]);
}
sort(a+, a++n);
for(int i=; i<=n; i++) sum[i] = sum[i-] + a[i];
int le = , ri = mod;
int res;
while(le <= ri) {
int mid = (le + ri) >> ;
if(check(mid)) res = mid, ri = mid-;
else le = mid+;
}
printf("%d\n", res);
return ;
}

[CF#592 E] [二分答案] Minimizing Difference的更多相关文章

  1. CF 1042A Benches——二分答案(水题)

    题目:http://codeforces.com/problemset/problem/1042/A #include<iostream> #include<cstdio> # ...

  2. CF 371C-Hamburgers[二分答案]

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Cf Round #403 B. The Meeting Place Cannot Be Changed(二分答案)

    The Meeting Place Cannot Be Changed 我发现我最近越来越zz了,md 连调程序都不会了,首先要有想法,之后输出如果和期望的不一样就从输入开始一步一步地调啊,tmd现在 ...

  4. CF 1100E Andrew and Taxi(二分答案)

    E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 洛谷P2402 奶牛隐藏(网络流,二分答案,Floyd)

    洛谷题目传送门 了解网络流和dinic算法请点这里(感谢SYCstudio) 题目 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛 ...

  6. 洛谷CF1071E Rain Protection(计算几何,闵可夫斯基和,凸包,二分答案)

    洛谷题目传送门 CF题目传送门 对于这题,我无力吐槽. 虽然式子还是不难想,做法也随便口胡,但是一些鬼畜边界情况就是判不对. 首先显然二分答案. 对于每一个雨滴,它出现的时刻我们的绳子必须落在它上面. ...

  7. 【CF981F】Round Marriage(二分答案,二分图匹配,Hall定理)

    [CF981F]Round Marriage(二分答案,二分图匹配,Hall定理) 题面 CF 洛谷 题解 很明显需要二分. 二分之后考虑如果判定是否存在完备匹配,考虑\(Hall\)定理. 那么如果 ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  9. [CodeForces954G]Castle Defense(二分答案+差分)

    Description 题目链接 Solution 二分答案,套一个差分标记即可 每次放弓箭手显然越右边越优 Code #include <cstdio> #include <alg ...

随机推荐

  1. Spring学习之==>入门知识

    一.Spring是什么? Spring 是一种轻量级的.非侵入式的 Java/JavaEE 应用框架.Spring 使用的是基本的 JavaBean 来完成以前只可能由EJB完成的事情.然而,Spri ...

  2. [转] Maven 从命令行获取项目的版本号

    [From]https://blog.soebes.de/blog/2018/06/09/help-plugin/ I bet you have been faced with the situati ...

  3. js中的堆内存和栈内存

    我们常常会听说什么栈内存.堆内存,那么他们到底有什么区别呢,在js中又是如何区分他们的呢,今天我们来看一下. 一.栈内存和堆内存的区分 一般来说,栈内存主要用于存储各种基本类型的变量,包括Boolea ...

  4. python copy与deepcopy (拷贝与深拷贝)

    copy与deepcopy python 中的copy与deepcopy是内存数据的操作,但是两个函数有一定的区别. 1.copy import copy list = [1, [4, 5, 6], ...

  5. 西安邀请赛-L(打表找规律)

    题目链接:https://nanti.jisuanke.com/t/39279 题意:给定n个不同的数表示的序列,定义两种操作:1. 交换前一半和后一半(如果有奇数个,则中间的不管).2. 交换每个偶 ...

  6. 6.maven的安装

    JAVA配置 JAVA_HOME=安装目录 PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin CLASSPATH=%JAVA_HOME%\lib\dt.jar;%JAV ...

  7. [转帖]docker清理日志

    docker清理日志 2017年05月03日 10:37:27 不想当码农的程序员 阅读数 12827    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn. ...

  8. 设计模式:模板方法(Template method)

    首先我们先来看两个例子:冲咖啡和泡茶.冲咖啡和泡茶的基本流程如下: 所以用代码来创建如下: 咖啡:Caffee.java public class Coffee { void prepareRecip ...

  9. Python 条件判断 和循环

    使用条件判断 if else # 条件派单 if else print('条件派单 if else') # s = input('请输入生日年号:') # birth = int(s) birth = ...

  10. 使用Redis實現秒殺功能

    <?php $id = 1; $pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","r ...