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次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...
随机推荐
- 转:vue2.0 keep-alive最佳实践
转载至:https://segmentfault.com/a/1190000008123035 1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性 ...
- jQuery库冲突
jQuery库冲突解决办法 一次面试中面试官问到jQuery和别的库冲突怎么解决?虽然以前看过,但是我已经不记得了. 我的思路就是如果让我来设计,那我就用一个默认值$,不传参数,那就用$,最后就挂 ...
- 【LOJ】#2109. 「JLOI2015」骗我呢
题解 我深思熟虑许久才算是明白个大概的计数问题吧 先是转化成一个矩形,列一条直线y = x,y = x - (m + 1)我们从(0,0)走到(n + m + 1,m + 1)就是答案 因为我们起始相 ...
- SpringBoot的Controller使用
一: 1.注解 2.control注解 3.效果 4.RespomseBody package com.caojun.springboot; import org.springframework.be ...
- Exception in thread "main" java.net.SocketTimeoutException: connect timed ou错误处理
今天做了一个Jsoup解析网站的项目,使用Jsoup.connect(url).get()连接某网站时偶尔会出现java.net.SocketTimeoutException:Read timed o ...
- 【转】tair与redis比较总结
1. Tair总述 1.1 系统架构 一个Tair集群主要包括3个必选模块:configserver.dataserver和client,一个可选模块:invalidserver.通常情况下,一个集群 ...
- Android项目无法运行在x86的虚拟机上
Android项目无法运行在x86的虚拟机上 解决办法来自 https://blog.csdn.net/qq_33495943/article/details/70255942 运行程序的时候报错如下 ...
- 22.python中的面向对象和类的基本语法
当我发现要写python的面向对象的时候,我是踌躇满面,坐立不安呀.我一直在想:这个坑应该怎么爬?因为python中关于面向对象的内容很多,如果要讲透,最好是用面向对象的思想重新学一遍前面的内容.这个 ...
- P4812 D’s problem(d)
P4812 D’s problem(d)From: admin 时间: 1000ms / 空间: 65536KiB / Java类名: Main 背景 NOIP春季系列课程 描述 小D是一名魔法师,它 ...
- BZOJ.2595.[WC2008]游览计划(DP 斯坦纳树)
题目链接 f[i][s]表示以i为根节点,当前关键点的连通状态为s(每个点是否已与i连通)时的最优解.i是枚举得到的根节点,有了根节点就容易DP了. 那么i为根节点时,其状态s的更新为 \(f[i][ ...