[CF#592 E] [二分答案] Minimizing Difference
链接: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的更多相关文章
- CF 1042A Benches——二分答案(水题)
题目:http://codeforces.com/problemset/problem/1042/A #include<iostream> #include<cstdio> # ...
- CF 371C-Hamburgers[二分答案]
C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Cf Round #403 B. The Meeting Place Cannot Be Changed(二分答案)
The Meeting Place Cannot Be Changed 我发现我最近越来越zz了,md 连调程序都不会了,首先要有想法,之后输出如果和期望的不一样就从输入开始一步一步地调啊,tmd现在 ...
- CF 1100E Andrew and Taxi(二分答案)
E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 洛谷P2402 奶牛隐藏(网络流,二分答案,Floyd)
洛谷题目传送门 了解网络流和dinic算法请点这里(感谢SYCstudio) 题目 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛 ...
- 洛谷CF1071E Rain Protection(计算几何,闵可夫斯基和,凸包,二分答案)
洛谷题目传送门 CF题目传送门 对于这题,我无力吐槽. 虽然式子还是不难想,做法也随便口胡,但是一些鬼畜边界情况就是判不对. 首先显然二分答案. 对于每一个雨滴,它出现的时刻我们的绳子必须落在它上面. ...
- 【CF981F】Round Marriage(二分答案,二分图匹配,Hall定理)
[CF981F]Round Marriage(二分答案,二分图匹配,Hall定理) 题面 CF 洛谷 题解 很明显需要二分. 二分之后考虑如果判定是否存在完备匹配,考虑\(Hall\)定理. 那么如果 ...
- 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 ...
- [CodeForces954G]Castle Defense(二分答案+差分)
Description 题目链接 Solution 二分答案,套一个差分标记即可 每次放弓箭手显然越右边越优 Code #include <cstdio> #include <alg ...
随机推荐
- MySQL 5.7 中文全文检索
MySQL 5.7 中文全文检索 在 MySQL 5.7.6 之前,全文索引只支持英文全文索引,不支持中文全文索引,需要利用分词器把中文段落预处理拆分成单词,然后存入数据库.从 MySQL 5.7.6 ...
- RESTful 介绍
什么是RESTful?一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务端交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. ...
- ELK 日志平台构建
elastic中文社区 https://elasticsearch.cn/ 完整参考 ELK实时日志分析平台环境部署--完整记录 https://www.cnblogs.com/kevingrace/ ...
- C基础知识(2):变量&常量的定义和声明
变量定义和声明 (1) 使用int,char等类型符定义变量 使用int,char等类型符定义变量时,不管有没有指定初始值,都就已经建立了存储空间(开辟内存).内存寻址由大到小,优先分配内存地址比较大 ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- 【VUE】vue在vue-cli3环境下基于axios解决跨域问题
网上的绝大部分教程解决vue+axios跨域问题都不能直接适用vue-cli3.这是因为vue-cli3不一样的配置方式导致的. 如果是使用vue-cli3构建的项目,那么默认是没有config.js ...
- POP与OOP编程模式对比
面向过程:(procedure oriented programming 即:POP) 代表:C/C++ 向过程程序设计,它是以功能为中心来进行思考和组织的一种编程方式,强调的是系统的数据被加工和处理 ...
- PTA(Basic Level)1014.福尔摩斯的约会 && PTA(Advanced Level)1061.Dating
大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm.大侦探很快就明白了,字条上奇 ...
- AssertionError [ERR_ASSERTION]: Task function must be specified,gulp版本不一致
报错信息: vue项目打包报错 > innovate-admin-vue@ build /home/soldier/SOLDIER/IDE_project/webStorm_project/in ...
- jpa报错 Unable to acquire a connection from driver [null], user [null] and URL [null]
jpa报错 Unable to acquire a connection from driver [null], user [null] and URL [null] 为啥报错 因为你在persist ...