Codeforces 394D Physical Education and buns screwing

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: Sequence of given n number (ability to sort)

Operation can make a 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. form a arithmetic progression.

Then the entire sequence is shifted to the value of the array after the operation of the minimum value = = 0. The number of operations for 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;}


Codeforces 394D Physical Education and buns screwing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.