cf D. Physical Education and Buns
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的更多相关文章
- Codeforces 394D Physical Education and Buns 胡搞
题目链接:点击打开链接 题意:给定n个数的序列(能够排序) 操作一次能够使得某个数++或--. 问最少操作几次使得序列变成一个等差序列 输出: 第一行输出最少操作的次数 第二行输出等差数列里的最小项 ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
- 【题解】Luogu CF915E Physical Education Lessons
原题传送门:CF915E Physical Education Lessons 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 这道题很简单啊 每个操作就是区间赋值,顺带把总和修 ...
- CF915E Physical Education Lessons
题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还 ...
- 【CodeForces】915 E. Physical Education Lessons 线段树
[题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...
- Codeforces 915 E Physical Education Lessons
题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- Codeforces 915E. Physical Education Lessons(动态开点线段树)
E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...
随机推荐
- 海思android4.4 SDK编译Latin输入法
原来的HiSTBAndroidV500R001C01SPC020\device\hisilicon\bigfish\packages\apps\HiLatinIME\Android.mk内容例如以下: ...
- [置顶] JQuery插件学习教程
这是JQuery其它常用插件的视频教程,包括validate插件,.comet插件等.同时有大量实例项目,如果你是喜欢JQuery的童鞋千万不要错过. 教程的内容有: 1_validate插件(1) ...
- iOS 高仿:花田小憩3.0.1
前言 断断续续的已经学习Swift一年多了, 从1.2到现在的2.2, 一直在语法之间徘徊, 学一段时间, 工作一忙, 再捡起来隔段时间又忘了.思来想去, 趁着这两个月加班不是特别多, 就决定用swi ...
- 剑指offer: 38 数字在排序数组中出现的次数
题目描述 统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5} 和数字3,输出4. 思路如下 1. 预估时间复杂度,最复杂情况是,顺序扫描,统计K出现的次数,时间复杂 ...
- js兼容各个浏览器的复制功能
看似简单的复制功能,用js做起来竟然遇到各种情况.刚开始在网上搜索到复制功能的几种实现方法,但是都不兼容.最后还是用的插件代码如下 html模板 <tr> <td>1</ ...
- (转)[开发笔记]-js判断用户的浏览设备是移动设备还是PC
<script type="text/javascript"> function browserRedirect() { var sUserAgent = naviga ...
- PHP上传原理及应用
概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...
- Linux 自动更新时间
1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...
- requirejs+anjularjs+express框架
1.目录 2.首页login.html如下: <!DOCTYPE html><html> <head> <title>登录界面</title> ...
- 用layer添加UIView的动画
项目有时会遇到用UIView 添加动画的情况,这里我觉得在layer上添加动画比较好,因为可以详细地设定动画属性,方便理解 下面是一个旋转动画: -(void)roundBtnAction:(id)s ...