Codeforces 1009C: Annoying Present
2 seconds
256 megabytes
standard input
standard output
Alice got an array of length nn as a birthday present once again! This is the third year in a row!
And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.
Bob has chosen mm changes of the following form. For some integer numbers xx and dd, he chooses an arbitrary position ii (1≤i≤n1≤i≤n) and for every j∈[1,n]j∈[1,n] adds x+d⋅dist(i,j)x+d⋅dist(i,j) to the value of the jj-th cell. dist(i,j)dist(i,j) is the distance between positions ii and jj (i.e. dist(i,j)=|i−j|dist(i,j)=|i−j|, where |x||x| is an absolute value of xx).
For example, if Alice currently has an array [2,1,2,2][2,1,2,2] and Bob chooses position 33 for x=−1x=−1 and d=2d=2 then the array will become [2−1+2⋅2, 1−1+2⋅1, 2−1+2⋅0, 2−1+2⋅1][2−1+2⋅2, 1−1+2⋅1, 2−1+2⋅0, 2−1+2⋅1] = [5,2,1,3][5,2,1,3]. Note that Bob can't choose position ii outside of the array (that is, smaller than 11 or greater than nn).
Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.
What is the maximum arithmetic mean value Bob can achieve?
The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) — the number of elements of the array and the number of changes.
Each of the next mm lines contains two integers xixi and didi (−103≤xi,di≤103−103≤xi,di≤103) — the parameters for the ii-th change.
Print the maximal average arithmetic mean of the elements Bob can achieve.
Your answer is considered correct if its absolute or relative error doesn't exceed 10−610−6.
2 3
-1 3
0 0
-1 -4
-2.500000000000000
3 2
0 2
5 0
7.000000000000000
题意:一个数组有n个数,每个数初始值为0。对该数组进行m次操作,每一次操作将数组中的a[i]=a[i]+x+d*abs(i-j),求m次操作后该数组所有元素和的最大的平均值是多少(看了好久的题,最后看了猛神的博客才把题意弄懂(;´д`)ゞ)。
实现:
因为数组的输出值全是0,所以用ans代表当前数组的元素和(ans初始值为0)。因为每次操作,每个元素都要先加上x,
所以ans=ans+x*n
接下来求该操作中的d*abs(i-j),因为i是一直变化的,每次操作是j是在随意的位置,但是一旦确定就不改变,所以可以看做是求数轴上1~n每个数到某一定点的距离。
因为d的正负不确定,还要使d*∑abs(i-j)的值最大,所以要对j的位置进行讨论:
如果d是正数,距离要尽可能大,所以j应该放在端点处;如果是负数,要使距离尽可能小,即需要放在中间位置
PS:最后注意精度问题,好像double会卡39组数据,int第五组都过不去,要用long long,最后再将long long转换成浮点型
AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	ll n,m;
	ll x,d;
	cin>>n>>m;
	ll ans=0;
	while(m--)
	{
		cin>>x>>d;
		ans+=n*x;//求数组里元素的总和
		if(d>0)//距离应该最大,j取到两端点处,距离和为:(0+(n-1)*n)/2=n*(n-1)/2
		{
			ans+=d*(n*(n-1)/2);
		}
		else//此时j应在中间,分元素个数为奇数,偶数两种情况
		{
			if(n%2)//如果是奇数,放在正中间
			{
				 ll mid=(n+1)/2;
				 ll x=mid*(mid-1);
				 ans+=d*x;
			}
			else//偶数的话中间两个位置都可以
			{
				ll mid=n/2;
				ll x=(1+mid)*mid/2;
				ans+=d*(2*x-mid);
			}
		}
	}
	printf("%.15lf\n",ans*1.0/n);
	return 0;
}												
											Codeforces 1009C: Annoying Present的更多相关文章
- 【Codeforces 1009C】Annoying Present
		
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实就是让你最后这n个数字的和最大. 加上的x没有关系.因为肯定都是加上n个x 所以直接加上就可以了 主要在于如何选取j 显然我们要找到一个位 ...
 - C. Annoying Present  SB题
		
C. Annoying Present time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 - Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)
		
题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...
 - Codeforces ~ 1009C ~ Annoying Present (贪心)
		
题意 一个长度为n的数组(初始全为0),进行m次操作. 操作:给你x,d,你任意挑选一个 i (1~n),每个数字加上 x+|i-j|*d( j 表示对应数字的下标) 问m次操作后的最大算术平均值为多 ...
 - Codeforces 731C:Socks(并查集)
		
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
 - Codeforces 747D:Winter Is Coming(贪心)
		
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
 - Codeforces 747C:Servers(模拟)
		
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
 - Codeforces 749D:Leaving Auction(set+二分)
		
http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...
 - Codeforces 749B:Parallelogram is Back(计算几何)
		
http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...
 
随机推荐
- java对象,引用的区别
			
一,其实 对象 就是一个类的实例 在Java中有一句比较流行的话,叫做“万物皆对象”,这是Java语言设计之初的理念之一.要理解什么是对象,需要跟类一起结合起来理解.下面这段话引自<Java编程 ...
 - 编程当道,学点Python技术好傍身
			
为了填满AI时代的人才缺口,编程语言教育都从娃娃抓起了!如果你还不懂Python是什么将来怎么给孩子辅导作业呢? Python新手入门教程 近期,浙江省信息技术课程改革方案出台,Python言语现已断 ...
 - ng-深度学习-课程笔记-11: 卷积神经网络(Week1)
			
1 边缘检测( edage detection ) 下图是垂直边缘检测的例子,实际上就是用一个卷积核进行卷积的过程. 这个例子告诉我们,卷积可以完成垂直方向的边缘检测.同理卷积也可以完成水平方向的边缘 ...
 - 892. Surface Area of 3D Shapes
			
问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...
 - Android 自定义View-字母索引表(一)
			
在有些Android应用中,为了方便快速定位,经常会看到屏幕右侧有一个字母索引表,今天尝试使用自定义View的方式实现了索引表的基本布局. 字母索引表的样式如下面的示意图所示, 此时我们至少需要知道以 ...
 - VS+QT中文乱码问题
			
1.使用QStringLiteral把所有中文包起来 2.#pragma execution_character_set("utf-8")
 - struts1和struts2比较
 - Git-时光穿梭【转】
			
本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 时光穿梭机 我们已经成 ...
 - UVa 10791 最小公倍数的最小和(唯一分解定理)
			
https://vjudge.net/problem/UVA-10791 题意: 输入整数n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小. 思路: 首先对n进行质因数分解,举个例子 ...
 - pycharm同时使用python2.7版本和python3.6版本
			
最近在看爬虫的专题,很多爬虫的教程是python2的,电脑上装的是3.6版本,而且python不向下兼容,这就很麻烦,最简单的print要加括号啊,等等.于是分享一个在windows环境下pychar ...