题目链接:点击打开链接

题意:给定n个数的序列(能够排序)

操作一次能够使得某个数++或--。

问最少操作几次使得序列变成一个等差序列

输出:

第一行输出最少操作的次数

第二行输出等差数列里的最小项 和 公差的绝对值。

思路:枚举公差,公差范围一定是0到 2Max.

先排个序。

我们使得首项不变。形成一个等差数列。

然后让整个数列位移至 操作后的数组的差值 最小值 == 0。这样这个数列的操作次数= 最大的差值/2.

done.

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e3+10;
const int inf = 1e9; int n, a[N], b[N], ans;
pii an;
int go(int x){
b[1] = 0;
int mi = 0;
for (int i = 2, now = a[1] + x; i <= n; i++, now += x)
{
b[i] = a[i] - now;
mi = min(mi, b[i]);
}
int ma = 0;
for (int i = 1; i <= n; i++) {
b[i] -= mi;
ma = max(ma, b[i]);
}
b[1] -= (ma + 1) >> 1;
return (ma + 1) >> 1;
}
int main(){
rd(n);
for (int i = 1; i <= n; i++)rd(a[i]);
sort(a + 1, a + 1 + n);
ans = 1e9;
for (int step = 0;step <= 30000; step++){
int tmp = go(step);
if (ans > tmp){ ans = tmp; an.first = a[1] - b[1]; an.second = step; }
else if (ans == tmp){ an = min(an, pii(a[1] - b[1], step)); }
}
cout << ans << endl;
cout << an.first << " " << an.second << endl;
return 0;
}

Codeforces 394D Physical Education and Buns 胡搞的更多相关文章

  1. Codeforces 915E Physical Education Lessons

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

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

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

  3. cf D. Physical Education and Buns

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

  4. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  5. Codeforces 669D Little Artem and Dance (胡搞 + 脑洞)

    题目链接: Codeforces 669D Little Artem and Dance 题目描述: 给一个从1到n的连续序列,有两种操作: 1:序列整体向后移动x个位置, 2:序列中相邻的奇偶位置互 ...

  6. codeforces 915E - Physical Education Lessons 动态开点线段树

    题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...

  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】915 E. Physical Education Lessons 线段树

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

随机推荐

  1. Chrome插件 postman的使用方法详解!最全面的教程

    一 简介 Postman 是一款功能超级强大的用于发送 HTTP 请求的 Chrome插件 .做web页面开发和测试的人员应该是无人不晓无人不用!其主要特点 特点: 创建 + 测试:创建和发送任何的H ...

  2. 对象克隆及属性转换-JavaScript

    在某些项目中,需要将一些返回信息进行其他语言的翻译,可以为不同语言用户提供不同的语言版本.下面是一个实现: /** * @class Translate * @description 查询字典,翻译成 ...

  3. java Comparator和Comparable(比较器)

    Comparable: 一个类实现了Camparable接口则表明这个类的对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort方法排序,sort方法调用compareTo()方法里定义 ...

  4. asserts文件存到外部SD卡里

    package com.example.wang.testapp3; import android.content.res.AssetManager; import android.graphics. ...

  5. pycharm 终端命令行中文乱码解决

    ssh连接服务器,运行脚本发现终端显示的中文乱码.百度了很多方法,都没解决.后来换了个putty连接上去运行,输出屏幕的中文没乱码.可以确定是pycharm设置问题. 设置方法:  File -- s ...

  6. HTML5 标签语法变化和使用概念

    1.H5与H4的区别 概念的变化: H5更注重内容与结构,不再只专注于表现. 声明与标签: 新的声明背简化: <!DOCTYPE html> <meta charset=utf-8& ...

  7. [java] DOS编译 .java 文件得到 .class 文件 并执行 以及使用外部 .jar包 时的命令

    当写一个java文件后,在DOS中进行编译与执行时,如果没有引入外来的包,那情况很简单 例如: public class hello_world { public static void main(S ...

  8. P1102 A-B数对

    P1102 A-B数对用map过掉,可以当高效的桶排用,map<long long,int>m;意思是m[long long]==int; #include<iostream> ...

  9. java json和对象互相装换

    java json和对象互相装换 1.com.alibaba.fastjson.JSON 2.com.fasterxml.jackson.databind.ObjectMapper

  10. MySQL Binlog 介绍

    Binlog 简介 MySQL中一般有以下几种日志: 日志类型 写入日志的信息 错误日志 记录在启动,运行或停止mysqld时遇到的问题 通用查询日志 记录建立的客户端连接和执行的语句 二进制日志 记 ...