题目链接:点击打开链接

题意:给定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. hdu 2545 求当前结点到根节点的距离

    求当前结点到根节点的距离 Sample Input 2 1 //n m 1 2 1 2 //询问 5 2 1 2 1 3 3 4 3 5 4 2 //询问 4 5 0 0 Sample Output ...

  2. RGBA颜色与兼容性的半透明背景色

    所谓RGBA颜色,顾名思意就是R+G+B+A的颜色,再具体点就是RED+GREEN+BLUE+ALPHA的颜色,小写一下就是red+green+blue+alpha的颜色,翻译一下就是红+绿+蓝+Al ...

  3. day--14前端(HTML、CSS)

    浏览器相当于客户端,浏览器访问服务端,收到消息之后里面断开,一次请求,一次响应,一次断开.     Web框架本质    http://www.cnblogs.com/wupeiqi/articles ...

  4. 【AtCoder】ARC098题解

    C - Attention 枚举,计算前缀和即可 代码 #include <bits/stdc++.h> #define fi first #define se second #defin ...

  5. Codeforces 908F New Year and Rainbow Roads

    New Year and Rainbow Roads 思路:我们考虑两个绿点之间的红点和蓝点, 首先把这些红点和蓝点接到绿点上面绝对不会超过绿点距离的两倍. 然后我们先把两个绿点连上, 再把绿点经过蓝 ...

  6. CSS------让ul中高度不同的li底部对齐

    如图: 代码:(需要将li中vertical-align属性设置为bottom) <ul style="margin-top:50px"> <li style=& ...

  7. “漂亮的”排序算法 Stooge Sort 如何完成排序

    Stooge Sort 是一种低效的递归排序算法,甚至慢于冒泡排序.在<算法导论>第二版第7章(快速排序)的思考题中被提到,是由Howard.Fine等教授提出的所谓“漂亮的”排序算法. ...

  8. PocketMoney

    PocketMoney( Money.pas/cpp/c)Description学校为了表彰tsoi的优异成绩, m个领导每人都决定给tsoi的一些人发一些小红包.于是n个Tsoier排成一排,等待着 ...

  9. java 线程池(ExecutorService与Spring配置threadPoolTaskExecutor)

    一.java ExecutorService实现 创建ExecutorService变量private ExecutorService executor = null 2.执行对应任务时,首先生成线程 ...

  10. 求任意多边形面积 python实现

    数学解决方法: 多边形外选取一点,连接各点构成三角形,计算求和......  详细链接  http://blog.csdn.net/hemmingway/article/details/7814494 ...