http://codeforces.com/contest/394/problem/D

题意:给你n个数,然后通过操作使得这n个数变为一个等差数列,操作是可以经过小于等于k次加1或减去1,要使得k尽量小。

思路:通过枚举公差d,然后通过每一个减去相应的个数的d,找到首项,每一个都可以得到一个首项,在这些首项中找到最大值和最小值,我们取最大值和最小值的一半为a1,然后找到一个k,在等到的很多个k中找到最小值。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 10010
using namespace std;
const int inf=<<; int n;
int a[maxn],a1,d; int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
int ans=inf;
for(int k=; k<=; k++)
{
int max1=-inf,min1=inf;
for(int i=; i<n; i++)
{
min1=min(min1,a[i]-i*k);
max1=max(max1,a[i]-i*k);
}
int x=(min1+max1)/;
if(ans>max(x-min1,max1-x))
{
ans=max(x-min1,max1-x);
a1=x;
d=k;
}
}
printf("%d\n",ans);
printf("%d %d\n",a1,d);
}
return ;
}

cf D. Physical Education and Buns的更多相关文章

  1. Codeforces 394D Physical Education and Buns 胡搞

    题目链接:点击打开链接 题意:给定n个数的序列(能够排序) 操作一次能够使得某个数++或--. 问最少操作几次使得序列变成一个等差序列 输出: 第一行输出最少操作的次数 第二行输出等差数列里的最小项 ...

  2. Codeforces 915E Physical Education Lessons

    原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...

  3. CF915E Physical Education Lessons 动态开点线段树

    题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...

  4. 【题解】Luogu CF915E Physical Education Lessons

    原题传送门:CF915E Physical Education Lessons 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 这道题很简单啊 每个操作就是区间赋值,顺带把总和修 ...

  5. CF915E Physical Education Lessons

    题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还 ...

  6. 【CodeForces】915 E. Physical Education Lessons 线段树

    [题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...

  7. Codeforces 915 E Physical Education Lessons

    题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...

  8. Physical Education Lessons CodeForces - 915E (动态开点线段树)

    Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...

  9. Codeforces 915E. Physical Education Lessons(动态开点线段树)

    E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...

随机推荐

  1. 【机房重构】SQL之视图

    近期在重构机房收费系统,越往后就会越感觉到这里很多其它的是对之前学过知识(数据库,设计模式)的一种应用和回想.比方在登录功能中用到了抽象加反射,在学生下机中,我们能够用触发器来同一时候更新两个表.这里 ...

  2. [RxJS] Filtering operators: takeUntil, takeWhile

    take(), takeLast(), first(), last(), those opreators all take number or no param. takeUtil and takeW ...

  3. 关于text-align无法居中的问题

    昨天项目,一直出现一个无法居中的问题,最后发现竟然是text-align的问题,才发现自己对text-align的理解还是不够透彻,于是在此再举例分析下. css中的元素一共有三类:块元素.行内块和内 ...

  4. HTML5 文件域+FileReader 分段读取文件(四)

    一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...

  5. (转)asp.net分页存储过程

    Asp.Net分页存储过程 SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in  (sel ...

  6. Java 获取字符串中第N次出现的字符位置

    public static int getCharacterPosition(String string){    //这里是获取"/"符号的位置    Matcher slash ...

  7. for循环,如何结束多层for循环

    采用标签方式跳出,指定跳出位置, a:for(int i=0;i<n;i++) { b:for(int j=0;j<n;j++) { if(n=0) { break a; } } }

  8. shell脚本学习之case例子

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支.  在代码块中控制程序分支  ca ...

  9. js 中读取JSON的方法探讨

    方法一:函数构造定义法返回 var strJSON = "{name:'json name'}";  //得到的JSONvar obj = new Function("r ...

  10. javascript——归并方法

    <script type="text/javascript"> //ECMAScript5 还新增了2个归并数组的方法:reduce()和reduceRight(). ...