首页 > 其他分享 >Educational Codeforces Round 47 (Rated for Div. 2) C. Annoying Present 数学

Educational Codeforces Round 47 (Rated for Div. 2) C. Annoying Present 数学

时间:2023-04-24 22:42:17浏览次数:29  
标签:Educational Rated 47 ll Alice long ans Bob array



Alice got an array of length
n
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
m
changes of the following form. For some integer numbers
x
and
d
, he chooses an arbitrary position
i
(
1

i

n
) and for every
j

[
1
,
n
]
adds
x
+
d

d
i
s
t
(
i
,
j
)
to the value of the
j
-th cell.
d
i
s
t
(
i
,
j
)
is the distance between positions
i
and
j
(i.e.
d
i
s
t
(
i
,
j

)

|
i

j
|
, where
|
x
|
is an absolute value of
x
).

For example, if Alice currently has an array
[
2
,
1
,
2
,
2
]
and Bob chooses position
3
for

x


1
and

d

2
then the array will become
[
2

1
+
2

2
,

1

1
+
2

1
,

2

1
+
2

0
,

2

1
+
2

1
]
=
[
5
,
2
,
1
,
3
]
. Note that Bob can’t choose position
i
outside of the array (that is, smaller than
1
or greater than
n
).

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?

Input
The first line contains two integers
n
and
m
(
1

n
,
m

10
5
) — the number of elements of the array and the number of changes.

Each of the next
m
lines contains two integers
x
i
and
d
i
(

10
3

x
i
,
d
i

10
3
) — the parameters for the
i
-th change.

Output
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

6
.

Examples
inputCopy
2 3
-1 3
0 0
-1 -4
outputCopy
-2.500000000000000
inputCopy
3 2
0 2
5 0
outputCopy
7.000000000000000

排版问题就不管了)
刚开始感觉难以下手,不知道怎么处理,听完同学一说,才发现就是数学的推导:
每次都会给你 x,d 对序列每个数加上 x+d* dict( i,j ),这里的 i 是任意的;
求最后的算术平均值 max ;
化简一下:
对于这 n 个数的 sum= x*n+d*Σdict( i,j )
考虑 d 的正负问题:
①: d>=0;
那么就是 n*x+ d * ( 0+…n-1 )= n*x + d*n*(n-1)/2
②:d<0;
自然后面的越小越好;
(1)n为奇数:
中间位置为 ( n+1 )/2;
以中间位置为 i , 求和即可;
(2)n为偶数:
以 n/2 为基准,同理求和;
注: 建议使用 long long , 可能会爆 int 以及 精度问题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define maxn 200005
const int mod=1e9+7;
#define eps 1e-5
#define pi acos(-1.0)

ll quickpow(ll a,ll b)
{
    ll ans=1;
    while(b){
        if(b&1){
            ans=ans*a;
        }
        a=a*a;
        b>>=1;
    }
    return ans;
}
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}




int main()
{
    //ios::sync_with_stdio(false);
     ll n,m;
     cin>>n>>m;
     ll ans=0;
     for(int i=0;i<m;i++){
        ll x,d;
        cin>>x>>d;
        if(d>=0){
            ans+=n*x+d*n*(n-1)/2;
        }
        else {
            if(n%2){
                ll t=(n+1)/2;
                ans+=n*x+2*d*t*(t-1)/2;
            }
            else {
                ll t=n/2;
                ans+=n*x+n*n*d/4;
            }
        }
     }
     printf("%.7lf\n",ans*1.0/n);
    return 0;
}


标签:Educational,Rated,47,ll,Alice,long,ans,Bob,array
From: https://blog.51cto.com/u_15657999/6221918

相关文章

  • Navicat连接Oracle报错:ORA-28547...
    使用Navicat连接正常的oracle数据库时,提示 可能是因为Navicat本地的OCI版本与Oracle数据库版本不符造成的,可以下载对应的OCI版本在Navicat中使用。1.下载OCI搜索oracleinstantclient找到相关下载地址OracleInstantClientDownloads根据实际oracle数据库版本选择对应in......
  • Educational Codeforces Round 147
    A题意思路有前导零结果直接为0,出现在第一位的?贡献为9,其他地方的?贡献为10。代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;chars[10];intmain(){intT;scanf("%d",&T);while(T--){scanf("%s",s);......
  • 【Oracle】year must be between -4713 and +9999,and not be 0
    【Oracle】yearmustbebetween-4713and+9999,andnotbe0yearmustbebetween-4713and+9999,andnotbe0出现问题的时候一般是to_date的地方有问题,很有可能是有字符串或者空格在数据中过滤掉就行......
  • Educational Codeforces Round 147 (Rated for Div. 2)
    题目链接B核心思路真的需要加强了,看到这个最大值和最小值我们其实也需要往对应的最大值和最小值的相关操作去想,不如前缀最小值/前缀最大值或者是后缀最小值/后缀最大值。这里一个比较直接的想法就是想找到不同的地方,然后看不可以扩展。那么怎么看是否可以扩展呢,如果是左边的话......
  • Educational Codeforces Round 127 (Rated for Div. 2)
    题目链接D核心思路首先挖掘下操作的性质吧:x>1&&x+3<10:1xx+1x+2x+310我们可以发现这样子好像对于我们的结果是没有影响的,答案还是9.所以这个性质就挖掘出来了啊,只要我们把一段连续的插入到对应的区间里面就好了。也就是只要这个区间的左端点小于插入连续的数的最小值,......
  • Educational Codeforces Round 147 (A-D)
    A.Matching橘子熊:这题太简单了我不想写题面Description给定给一个带问号的字符串,求有多少种可能的数字Input多次询问,一次一个字符串Output对于每次询问,输出可能的数字的总数数据范围与约定2e5次询问,单词询问不超过5个字符思路主要思路签到题大部分情况下,一个......
  • Educational Codeforces Round 39 (Rated for Div. 2) -- D. Timetable (DP)
    写得很折磨人,每次dp都写个一个多小时,写出来明明觉得不难.题目大意:可以进行K次操作,把删除1,进行k次操作后每行第一个1和最后一个1的位置相减的绝对值加1得到的结果最小。做法:每次肯定是要从左删或者从右边删,然后顺着这个思路,先把每行的进行小于等于k次操作时,每行最小......
  • CF ER147 div.2
    A 简单计数题,判断前导零。#include<bits/stdc++.h>usingnamespacestd;intT;intmain(){ cin>>T; while(T--){ chars[10]; cin>>s; intn=strlen(s); intans=1; for(inti=0;i<n;i++){ if(s[i]=='?'&&a......
  • Educational Codeforces Round 147 (Rated for Div. 2)
    EducationalCodeforcesRound147(RatedforDiv.2)链接EducationalCodeforcesRound147(RatedforDiv.2)A题如果第一位数是0,直接打印0如果第一位数是'?',有9个数可以选择,如果其他位数是'?',有10中情况选择,相乘就可以了#include<iostream>#include<algo......
  • Thinkpad-t470电脑 Hackintosh 黑苹果efi引导文件
    原文来源于黑果魏叔官网,转载需注明出处。(下载请直接百度黑果魏叔)硬件型号驱动情况主板Thinkpad-t470处理器[email protected]/3.4GhzTurbo已驱动内存16GBDDR42666Mhz(SKHynix)已驱动硬盘IntelSSDPro7600P512GBNVME已驱动显卡IntelHDGraphics520已驱动声......