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次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...
随机推荐
- 开启@EnableRedisHttpSession
sessionId=569662ce-c6d5-42a9-a94b-c9df826df716 1800秒后失效 sessionId=23913542-9b5f-4699-8a87-1023b57f5f ...
- #CSS margin-top父元素下落
[我的解决方法] 给该父元素添加如下代码 border-top: 1px solid rgba(0,0,0,0); box-sizing:border-box; [原因] css2.1盒模型中规定的内 ...
- Java第三阶段学习(八:网络通信协议、UDP与TCP协议)
一.网络通信协议 1.概念: 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,在计算机网络中,这些连接和通信的规则被称为网络通信协议,它对数据的传 ...
- 棋盘游戏 HDU1281
一开始毫无思路 看了题解才发现是二分图的最大匹配问题 行为n 列为m 行列匹配 (一行只能与一列匹配 这点和象棋的车的意义一样) 再去掉点看看最大匹配会不会少 如果少了说明为关键点 其中 ...
- Linux下部署tomcat及tomcat war包应用程序
1, 通过winscp将tomcat包(6和7版本都是一样的安装方法)和jdk-6u27-linux-x64.bin安装文件传送到linux 系统/opt里面.(这里没有固定要传送到/opt/hn,可 ...
- 数据库相关--在mac OX10.11.6上安装MySQL
一.之前失败情况 官网下载dmg文件安装.源码安装,下过5.6 5.7 8.0 版本,都可以安装成功,但是在电脑设置界面无法启动,每次点启动输入密码后,均闪一下绿色然后变红色,既然不能界面启动,那 ...
- ARP协议详解之ARP动态与静态条目的生命周期
ARP协议详解之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自动添加和删除. q 每个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉.所以,生 ...
- 以OPC PowerTool 连接iFix与KEPWARE
1.安装完iFix后,再安装KEPWARE软件,然后必须再安装所需要的IO驱动才能进行device的通讯连接.这里安装iFix本身提供的OPC PowerTool V7.34a. 2.在安装完iFix ...
- luogu P2757 [国家集训队]等差子序列
题目链接 luogu P2757 [国家集训队]等差子序列 题解 线段树好题 我选择暴力 代码 // luogu-judger-enable-o2 #include<cstdio> inl ...
- BZOJ.2142.礼物(扩展Lucas)
题目链接 答案就是C(n,m1) * C(n-m1,m2) * C(n-m1-m2,m3)...(mod p) 使用扩展Lucas求解. 一个很简单的优化就是把pi,pi^ki次方存下来,因为每次分解 ...