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 胡搞

相关文章:

你感兴趣的文章:

标签云: