链接: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. 第一个简单APP设计图

    以下是我画出来的最简单的手机UI设计图哟,以后慢慢积累吧.... 其实使用很简单,很多控件都有,直接使用就好....还是多动手吧,相信自己之后能很好的掌握这个的使用哟!!!!!!

  2. Docker入门(转载)

    Docker入门 一.Docker 1.什么是容器? 容器就是将软件打包成标准化单元,用于开发.交付和部署.容器是轻量的.可执行的独立软件包 ,包含软件运行所需的所有内容:代码.运行时环境.系统工具. ...

  3. Day03:日期操作 / 集合框架(上)

    日期操作 Java中的时间 · Java中的时间使用标准类库的Date类表示,是用距离一个固定时间点的毫秒数(可正可负,long类型)表达一个特定的时间点: · 固定的时间点叫纪元(epoch),是U ...

  4. FTP简单搭建(二)

    六.配套设置 1.基于用户名的上传和下载 创建用户 useradd alex echo redhat |passwd --stdin alex 指定用户登录的路径 可不设置,不设置则为用户家目录 mk ...

  5. Laravel-Admin图片上传时的问题

    关于laravel-admin进入有图片上传页面时,会提示未配置存储路径等信息.此时需要去配置. 查看config/admin.php里面有 'upload' => [ // Disk in ` ...

  6. @Transient注解的使用(不被序列化和作为临时变量存储)

    转自:https://blog.csdn.net/sinat_29581293/article/details/51810805 java 的transient关键字的作用是需要实现Serilizab ...

  7. [Python3] 038 函数式编程 偏函数

    目录 函数式编程 之 偏函数 1. 关于强制类型转换 int 的补充 2. 利用 int 新建函数 3. functools.partial 函数式编程 之 偏函数 1. 关于强制类型转换 int 的 ...

  8. Spring_four

    Spring_four 基于XML的AOP实现事务控制 坐标xml ; //2.6更新转入账户 accountDao.updateAccount(target); } } 注意:方法级别的事务会覆盖类 ...

  9. spark教程(三)-RDD认知与创建

    RDD 介绍 spark 最重要的一个概念叫 RDD,Resilient Distributed Dataset,弹性分布式数据集,它是 spark 的最基本的数据(也是计算)抽象. 代码中是一个抽象 ...

  10. redis存储数据

    redis存储结构--5种 RedisTemplate访问Redis数据结构(一)--String https://blog.csdn.net/qq_25135655/article/details/ ...