Annoying Present CodeForces - 1009C

Annoying Present

题目链接:codeForces - 1009C

题意:有一长度为n的数列,每次选一个下标j,将所有数都增加x+d*|j-i|(i是下标);求m个操作后,平均每个数最多增加的值是多少;

思路:可知,每次必有增加n*x,对于d,当d>=0时,\sum_{i=1}^{n}|j-i|*d最大时j取1或n;当d<0时,\sum_{i=1}^{n}|j-i|*d最大时j取(1+n)/2;

所有值用long long,最后/n;

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
long long n, m;
int main(){
	while(cin >> n >> m){
		long long k=(n-1)*n/2;
		long long p=(n+1)/2;
		p=(p-1)*p/2+(n-p)*(1+n-p)/2;
		long long sum=0;
		for(int i=0; i<m; i++){
			long long x, d;
			scanf("%lld%lld", &x, &d);
			if(d>=0)
				sum+=x*n+d*k;
			else
				sum+=x*n+d*p;
		}
		double ans=(double)sum/(n*1.0);
		printf("%.10f\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sirius_han/article/details/81075612
今日推荐