Codeforces 394D Physical Education and Buns 胡搞
题目链接:点击打开链接
题意:给定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 胡搞的更多相关文章
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- Codeforces 915E. Physical Education Lessons(动态开点线段树)
E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...
- cf D. Physical Education and Buns
http://codeforces.com/contest/394/problem/D 题意:给你n个数,然后通过操作使得这n个数变为一个等差数列,操作是可以经过小于等于k次加1或减去1,要使得k尽量 ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
- Codeforces 669D Little Artem and Dance (胡搞 + 脑洞)
题目链接: Codeforces 669D Little Artem and Dance 题目描述: 给一个从1到n的连续序列,有两种操作: 1:序列整体向后移动x个位置, 2:序列中相邻的奇偶位置互 ...
- codeforces 915E - Physical Education Lessons 动态开点线段树
题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^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 ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- 【CodeForces】915 E. Physical Education Lessons 线段树
[题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...
随机推荐
- IIS7配置HTTPS+默认访问https路径
一.下载证书(这里我使用的是阿里云免费的证书) 文件说明: 1. 1532858285913.key(证书私钥文件).1532858285913.pem(证书文件).1532858285913.pfx ...
- h5手势密码开发(使用jq)
直接上代码: 目录结构: 本次开用到的技术jq,以及引入的jq插件jquery.gesture.password.min.js index.html <!DOCTYPE html> < ...
- Java编程的逻辑 (45) - 神奇的堆
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- html屏幕旋转事件监听
近期做微信服务号开发,在做图片展示的时候需要横竖屏的检测实现图片大小不同的展示. 添加屏幕旋转事件侦听,可随时发现屏幕旋转状态(左旋.右旋还是没旋). 摘自:http://bbs.phonegap10 ...
- shell学习(一)
一.免密交互 --stdin 1.从标准输入读取字符串 如:passwd --stdin heruguo246 [root@localhost mnt]# ---输入了修改heruigu ...
- NET定时任务组件Hangfire
开源的.NET定时任务组件Hangfire解析 项目慢慢就要开工了,很多园友都在问这个事情,看来大伙对这事很上心啊,事情需要一步步的来,尽量写出一个我们都满意的项目.以前每次在博客前面都会扯淡一下,不 ...
- CSS------当内容超出div宽度后自动换行和限制文字不超出div宽度和高度
如图: 1.自动换行 </div> 2.限制宽高度 </div> (注意:如果div放在li中还需要加上display:inline-block属性)
- Git公钥/私钥生成方式
打开git bash命令行工具,依次执行以下命令,直接下一步: 私钥生成:ssh-keygen -t rsa -b 2048 -f private.key 公钥生成:openssl rsa -in p ...
- [转]Splay算法
首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角. 先看一道题目: skydec有n个数,每次他都会把一些数放进一些盒子里,由于skydec太傻×,所以他不能判断数的大小,现在 ...
- IP基础知识与分配实现
一.IP寻址 1.划分网络ID和主机ID的最初方案是使用地址分类. 2.A类:0.0.0.0-127.255.255.255 B类:128.0.0.0-191.255.255.255 C类:192.0 ...