Robin Hood CodeForces - 672D (二分)
大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差.
二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, 否则为$max(1,R-L)$
- #include <iostream>
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <math.h>
- #include <set>
- #include <map>
- #include <queue>
- #include <string>
- #include <string.h>
- #include <bitset>
- #define REP(i,a,n) for(int i=a;i<=n;++i)
- #define PER(i,a,n) for(int i=n;i>=a;--i)
- #define hr putchar(10)
- #define pb push_back
- #define lc (o<<1)
- #define rc (lc|1)
- #define mid ((l+r)>>1)
- #define ls lc,l,mid
- #define rs rc,mid+1,r
- #define x first
- #define y second
- #define io std::ios::sync_with_stdio(false)
- #define endl '\n'
- #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
- using namespace std;
- typedef long long ll;
- typedef pair<int,int> pii;
- const int P = 1e9+7, INF = 0x3f3f3f3f;
- ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
- ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
- ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
- inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
- //head
- #ifdef ONLINE_JUDGE
- const int N = 1e6+10;
- #else
- const int N = 111;
- #endif
- int n, k, a[N];
- int main() {
- scanf("%d%d", &n, &k);
- REP(i,1,n) a[i]=rd();
- int l=0,r=1e9,L,R;
- while (l<=r) {
- ll d = 0;
- REP(i,1,n) if (a[i]<mid) d+=mid-a[i];
- if (d<=k) L=mid,l=mid+1;
- else r=mid-1;
- }
- l=0,r=1e9;
- while (l<=r) {
- ll d = 0;
- REP(i,1,n) if (a[i]>mid) d+=a[i]-mid;
- if (d<=k) R=mid,r=mid-1;
- else l=mid+1;
- }
- int ans = max(0,R-L);
- ll sum = 0;
- REP(i,1,n) sum+=a[i];
- if (sum%n) ans=max(ans,1);
- printf("%d\n", ans);
- }
Robin Hood CodeForces - 672D (二分)的更多相关文章
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 672D D. Robin Hood(二分)
题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...
- Codeforces Round #352 (Div. 2) D. Robin Hood 二分
D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills a ...
- CF 672D Robin Hood(二分答案)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 671B/Round #352(div.2) D.Robin Hood 二分
D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...
- Codeforces Round #352 (Div. 1) B. Robin Hood (二分)
B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【15.93%】【codeforces 672D】Robin Hood
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【CodeForces】671 B. Robin Hood
[题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...
随机推荐
- ubuntu16.04下g++安装及使用
1)首先在虚拟机中安装Ubuntu16.04,网络模式设置为NAT模式,安装完成后在虚拟机中测试是否能够上网. 2)进入Ubuntu,按Ctrl+alt+T,调出终端,输入sudo su,输入密码切换 ...
- luogu P1003 铺地毯
水题 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; t ...
- Spring boot 源码分析(前言)
开坑达人 & 断更达人的我又回来了 翻译的坑还没填完,这次再开个新坑= = 嗯,spring boot的源码分析 本坑不打算教你怎么用spring boot = = 也不打算跟你讲这玩意多方便 ...
- php配置可被设定范围
PHP中的每个指令都有其所属的模式,这些模式决定这一个PHP指定在何时何地.是否能被设定.例如有些指令可以在 PHP 脚本中用 ini_set() 来设定,而有些则只能在php.ini 或 httpd ...
- rabbitMQ Management http://localhost:15672/ 打不开
C:\RabbitMQ Server\rabbitmq_server-3.7.7\sbin>rabbitmq-plugins enable rabbitmq_management 安装rabbi ...
- ssm框架搭建的基本配置(一站式教会你搭建)
首先是需要的jar包: <dependency> <groupId>org.springframework</groupId> <artifactId> ...
- shell编辑器vi的常用命令
一:翻页 ctrl+u向上翻半页 ctrl+f向上翻一页 ctrl+d 向下翻半页 ctrl+b 向下翻一页 二:移动光标指令 0: 光标移至当前行首 $: 光标移至当前行尾 三:常用插入.删除指令 ...
- 豆瓣上关于<<一万小时天才理论>>一书的一个评论
原帖地址:http://book.douban.com/review/3707543/ 这本书和其他几本关于一万小时的书一样,都是基于埃里克森和赫伯特.西蒙的“十年法则”的标志性理论,但这本书通过新的 ...
- js中创建对象的4种方法
1.直接创建,不可复用式创建var obj = new Object(); obj.name = ""; obj.id = ""; 2.使用工厂方法来创建对象, ...
- python调用远程chromedriver.exe、selenium抓包方法
本地python示例代码: from selenium import webdriver driver = webdriver.Remote(command_executor='http://192. ...