hdu_5805_NanoApe Loves Sequence(xjb搞)

题目链接:hdu_5805_NanoApe Loves Sequence

题意:

给你n个数,现在要删一个数,删每个数的概率是一样的,现在问你删一个值后的相邻数绝对值最大差的期望是多少,因为担心精度误差,让你答案乘n

题解:

先算出不删数的绝对值最大的差ma并记录位置,如果要删的数不是刚才求出来的位置,那么ans+=max(abs(a[i-1]-a[i+1],ma)。如果是,那么重新求一下最大值就行了,因为最多重求两次,所以总复杂度还是O(n)。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define F(i,a,b) for(int i=a;i<=b;++i)
 4 using namespace std;
 5 typedef long long ll;
 6 const int N=1e5+7;
 7 ll a[N];
 8 int t,n;
 9
10 ll getnew()
11 {
12     ll an=0;
13     F(i,2,n)an=max(an,abs(a[i]-a[i-1]));
14     return an;
15 }
16 int main(){
17
18     scanf("%d",&t);
19     while(t--)
20     {
21         scanf("%d",&n);
22         F(i,1,n)scanf("%I64d",a+i);
23         ll pos,ma=0;
24         F(i,2,n){
25             int tp=abs(a[i]-a[i-1]);
26             if(tp>ma)ma=tp,pos=i;
27         }
28         ll ans=0;
29         F(i,1,n)
30         {
31             if(i==pos||i==pos-1)
32             {
33                 if(i==1)
34                 {
35                     ll tp=a[i];
36                     a[i]=a[i+1];
37                     ans+=getnew();
38                     a[i]=tp;
39                 }else
40                 {
41                     ll tp=a[i];
42                     a[i]=a[i-1];
43                     ans+=getnew();
44                     a[i]=tp;
45                 }
46             }else
47             {
48                 if(i==1||i==n)ans+=ma;
49                 else{
50                     ans+=max(ma,abs(a[i+1]-a[i-1]));
51                 }
52             }
53
54         }
55         printf("%I64d\n",ans);
56     }
57     return 0;
58 }

时间: 2024-12-06 18:27:12

hdu_5805_NanoApe Loves Sequence(xjb搞)的相关文章

hdu-5806 NanoApe Loves Sequence Ⅱ(尺取法)

题目链接: NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examinatio

Best Coder #86 1002 NanoApe Loves Sequence

NanoApe Loves Sequence Accepts: 531 Submissions: 2481 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entr

hdu_5806_NanoApe Loves Sequence Ⅱ(双指针)

题目链接:hdu_5806_NanoApe Loves Sequence Ⅱ 题意: 给你一段数,问你有多少个区间满足第K大的数不小于m 题解: 直接双指针加一下区间就行 1 #include<cstdio> 2 #include<algorithm> 3 #define F(i,a,b) for(int i=a;i<=b;i++) 4 using namespace std; 5 typedef long long ll; 6 const int N=2e5+7; 7 in

5806 NanoApe Loves Sequence Ⅱ(尺取法)

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 1585    Accepted Submission(s): 688 Description NanoApe, the Retired Dog, has returned back to prepare for for the

5805 NanoApe Loves Sequence

传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 1323    Accepted Submission(s): 521 Description NanoApe, the Retired Dog, has returned back to prepare for the Natio

NanoApe Loves Sequence Ⅱ(尺取法)

题目链接:NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 339    Accepted Submission(s): 165 Problem Description NanoApe, the Retired Dog, has returned back to prepare for

hdu-5805 NanoApe Loves Sequence(线段树+概率期望)

题目链接: NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination! In

hdu 5273 Dylans loves sequence 逆序数简单递推

Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Description Dylans得到了N个数a[1]...a[N].有Q个问题,每个问题形如(L,R)他需要求出L−R这些数中的逆序对个数.更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y] Input 第一行有两个数N和Q

BC#86 1003NanoApe Loves Sequence Ⅱ[two-pointer]

NanoApe Loves Sequence Ⅱ Accepts: 374 Submissions: 946 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) 问题描述 退役狗 NanoApe 滚回去学文化课啦! 在数学课上,NanoApe 心痒痒又玩起了数列.他在纸上随便写了一个长度为 nn 的数列,他又根据心情写下了一个数 mm. 他想知道这个数列中有多少个区间里的第 kk 大