• 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;
    }


  • 相关阅读:
    xen虚拟机管理命令
    ipmi
    http://classworlds.codehaus.org/apiusage.html
    maven编译问题之 -The POM for XXX is invalid, transitive dependencies (if any) will not be available
    SSM项目web.xml等配置文件中如何查找类的全路径名?
    shiro安全框架学习-1
    ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
    ht-7 treeSet特性
    ht-6 hashSet特性
    ht-5 treemap特性
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5399253.html
Copyright © 2020-2023  润新知