Topic Link: Click to open the link
Test instructions: Sequence of given n number (can be sorted)
A single operation can cause a certain number of + + or--。 Ask the least number of actions to make the sequence into a linear sequence
Output:
The number of times the first line outputs the least action
The second line outputs the smallest item in the arithmetic progression and the absolute value of the tolerance.
Idea: To enumerate tolerances, the tolerance range must be 0 to 2Max.
First Order.
We make the first item unchanged, forming a arithmetic progression.
Then the entire sequence is shifted to the value of the operation of the array of the minimum value = = 0, so that the number of operations of this sequence = Maximum difference/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 * + (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);p Utchar (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 <= 300 00; 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 394D Physical Education and buns screwing