Codeforces Round 231 D 两次二分
来源:互联网 发布:玖富微理财网络借贷 编辑:程序博客网 时间:2024/03/28 23:30
The Physical education teacher at SESC is a sort of mathematician too. His most favorite topic in mathematics is progressions. That is why the teacher wants the students lined up in non-decreasing height form an arithmetic progression.
To achieve the goal, the gym teacher ordered a lot of magical buns from the dining room. The magic buns come in two types: when a student eats one magic bun of the first type, his height increases by one, when the student eats one magical bun of the second type, his height decreases by one. The physical education teacher, as expected, cares about the health of his students, so he does not want them to eat a lot of buns. More precisely, he wants the maximum number of buns eaten by some student to be minimum.
Help the teacher, get the maximum number of buns that some pupils will have to eat to achieve the goal of the teacher. Also, get one of the possible ways for achieving the objective, namely, the height of the lowest student in the end and the step of the resulting progression.
The single line contains integer n (2 ≤ n ≤ 103) — the number of students. The second line contains n space-separated integers — the heights of all students. The height of one student is an integer which absolute value doesn't exceed 104.
In the first line print the maximum number of buns eaten by some student to achieve the teacher's aim. In the second line, print two space-separated integers — the height of the lowest student in the end and the step of the progression. Please, pay attention that the step should be non-negative.
If there are multiple possible answers, you can print any of them.
5-3 -4 -2 -3 3
2-3 1
52 -3 -1 -4 3
1-4 2
Lets look at the first sample. We can proceed in the following manner:
- don't feed the 1-st student, his height will stay equal to -3;
- give two buns of the first type to the 2-nd student, his height become equal to -2;
- give two buns of the first type to the 3-rd student, his height become equal to 0;
- give two buns of the first type to the 4-th student, his height become equal to -1;
- give two buns of the second type to the 5-th student, his height become equal to 1.
To sum it up, when the students line up in non-decreasing height it will be an arithmetic progression: -3, -2, -1, 0, 1. The height of the lowest student is equal to -3, the step of the progression is equal to 1. The maximum number of buns eaten by one student is equal to 2.
这题并不难。。。我以为不能调位置呢。。。
首先二分最大值,然后二分斜率(把人的位置和高度画于坐标轴上,注意是整数,而且大于0 )
就OK了。
#include <stdio.h>#include <queue>#include <algorithm>#include <iostream>#include <fstream>#include <string.h>using namespace std;#define INF 0x3f3f3f3f#define MAX 1111#define P pair<int,int>#define fst first#define sec second#define ll long long int num[MAX];int n;int check(int p,int x,int &S,int &R)//1 small 2 big 0 right{ int top=num[0]+x,down=num[0]-x; for(int i=1;i<n;i++) { int itop=num[i]+x-p*i; int idown=num[i]-x-p*i; if(down>itop) return 2; if(top<idown) return 1; top=min(top,itop); down=max(down,idown); } R=p; S=down; return 0;}bool C(int x,int &S,int &R){ int lb=0,ub=num[1]+x-(num[0]-x)+1; while(ub-lb>1) { int m=(lb+ub)>>1; int res=check(m,x,S,R); if(res==1) lb=m; else if(res==2) ub=m; else return true; } return !check(lb,x,S,R);} int main(){// freopen("acm.in","r",stdin); cin>>n; for(int i=0;i<n;i++) cin>>num[i]; sort(num,num+n); int lb=-1,ub=10001; int S,R; while(ub-lb>1) { int m=(lb+ub)>>1; if(C(m,S,R)) ub=m; else lb=m; } cout<<ub<<endl; cout<<S<<" "<<R<<endl; return 0;}
- Codeforces Round 231 D 两次二分
- Codeforces Round #251 (Div. 2) D 二分
- Codeforces Round #352 (Div. 2) D 二分
- Exams Codeforces Round#377-D(贪心+二分)
- Codeforces Round #377 (Div. 2) D 二分
- Codeforces Round #377 D. Exams (二分)
- Educational Codeforces Round 21 D ( 二分 )
- Codeforces Round #256 (Div. 2) D. Multiplication Table 二分
- Codeforces Round #274 (Div. 2) D. Long Jumps(二分)
- Codeforces Round #220 (Div. 2) D 树状数组 && 二分
- Codeforces Round #309 (Div. 1)D. Nudist Beach 二分+bfs
- Codeforces Round #309 (Div. 1) D. Nudist Beach 二分 贪心
- Codeforces Round #298 (Div. 2) D. Handshakes 二分
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs_clock,二分
- Codeforces Round #256 (Div. 2) D Multiplication Table (二分)
- Codeforces Round #345 (Div. 2) D. Image Preview【二分】
- Codeforces Round #316 (Div. 2) D 【dfs序+二分】
- Codeforces Round #352 (Div. 2) D 二分贪心两头 + 细节
- jquery+CSS3 简易无限级纵向(上下)菜单插件
- Linux用户及用户组管理
- Linux chkconfig命令使用详解
- js获得当前时区夏令时发生和终止的时间
- Linux crontab计划任务应用
- Codeforces Round 231 D 两次二分
- [leet code] Edit Distance
- BMP图像加载实例(C语言)
- 关于我为什么要写博文?
- Spring的Quartz定时机制
- Maximum Depth of Binary Tree -- LeetCode
- Minimum Depth of Binary Tree -- LeetCode
- 互斥与同步(一)
- ubuntu系统 调整屏幕分辨率