现在的位置: 首页 > 综合 > 正文

Codeforces Round #231 (Div. 2) D 难以置信的二分

2014年06月06日 ⁄ 综合 ⁄ 共 2943字 ⁄ 字号 评论关闭
D. Physical Education and Buns
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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.

Sample test(s)
input
5
-3 -4 -2 -3 3
output
2
-3 1
input
5
2 -3 -1 -4 3
output
1
-4 2
Note

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.

题解

题目意思是说,给一个序列,对序列里的数据有三种操作,增加,减少,或者不变。其中,经过处理之后要求整个序列可以排列成一个递增的等差数列。求一个数ans,使得每个数据的改变量不超过ans。

枚举公差,二分ans就可以了。

代码示例

/****
	*@PoloShen
	*Title:D
	*/
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

int arr[1005];
int n;

void solve(){
    for (int i = 0; i < n; i++){ scanf("%d", &arr[i]); }
    sort(arr, arr + n);
    int l = 0, r = 0, ans = 2000000000;
    int ar0 = 0, det = 0;
    for (int d = 0; d < 20001; d++){
        l = r = 0;
        for (int i = 1; i < n; i++){
            l = min(l, arr[0] + i * d - arr[i]);
            r = max(r, arr[0] + i * d - arr[i]);
        }
        int pos = arr[0];
        l = -l;
        if (l < r){
            int mid = (l + r) / 2 - l;
            l += mid; r -= mid; pos -= mid;
        }
        if (r < l){
            int mid = (l + r) / 2 - r;
            l -= mid; r += mid; pos += mid;
        }
        l = -l;
        while (-l < r){ l--; r--; pos--; }
        while (-l > r){ l++; r++; pos++; }
        if (ans > r){ ans = r; ar0 = pos; det = d; }
    }
    printf("%d\n", ans);
    printf("%d %d\n", ar0, det);
}

int main(){
    scanf("%d", &n); solve();
    return 0;
}

抱歉!评论已关闭.