zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    linux openssh 升级
    局域网从另一台电脑copy文件(Linux系统下)
    单例模式
    6、android传递数据之剪切板传递数据
    5、android使用意图传递数据之全局变量传递
    4、android生命周期的介绍
    3、android搭建开发环境介绍
    2、android系统框架的介绍
    1、安卓学习线路图
    7、开发有状态bean
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5399253.html
Copyright © 2011-2022 走看看