HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

先找相邻差值的最大,第二大,第三大

删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5 #define LL long long
 6 const int N = 100005;
 7 int t, n, p1, p2, p3;
 8 LL a[N];
 9 LL s1[N], s2[N];
10 LL sum;
11 int main()
12 {
13     scanf("%d", &t);
14     while(t--)
15     {
16         scanf("%d", &n);
17         for(int i = 1; i <= n; i++)
18             scanf("%I64d", &a[i]);
19         p1 = p2 = p3 = 0;
20         s1[0] = -1;
21         for(int i = 1; i < n; i++)
22         {
23             s1[i] = abs(a[i + 1] - a[i]);//相邻
24             if(s1[p1] < s1[i] ) p1 = i;//最大
25         }
26         for(int i = 1; i < n; i++)
27         {
28             if(i == p1) continue;
29             else if(s1[p2] < s1[i]) p2 = i;//第二大
30         }
31         for(int i = 1; i < n; i++)
32         {
33             if(i == p1 || i == p2) continue;//第三大
34             else if(s1[p3] < s1[i]) p3 = i;
35         }
36         for(int i = 2; i < n; i++)
37             s2[i] = abs(a[i+1] - a[i-1]);//去掉 i 点新增值
38
39         sum = 0;
40         if(p1 == 1) sum += s1[p2];
41         else sum += s1[p1];
42         for(int i = 2; i < n; i++)
43         {
44             if(p1 == i-1 || p1 == i)
45             {
46                 if(p2 == i-1 || p2 == i) sum += max(s1[p3], s2[i]);
47                 else sum += max(s1[p2], s2[i]);
48             }
49             else sum += max(s1[p1], s2[i]);
50         }
51         if(p1 == n - 1) sum += s1[p2];
52         else sum += s1[p1];
53         printf("%I64d\n",sum);
54     }
55 } 
时间: 2024-10-11 05:57:15

HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)的相关文章

HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans += n - j + 1 . i++ 的时候看看需不需要size-- 就可以更新了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define

HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003

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

HDU 5063 Operation the Sequence(BestCoder Round #13)

Problem Description: You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types:Type1: O 1 call fun1();Type2: O 2 call fun2();Type3:

hdu 5204 Rikka with sequence(BestCoder Round #37)

Rikka with sequence                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 378    Accepted Submission(s): 75 Problem Description As we know, Rikk

HDU 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le

HDU 5805 NanoApe Loves Sequence(思维)

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

HDU 4908 BestCoder Sequence(BestCoder Round #3)

Problem Description: Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence. As the best coder, M

HDU 5806 NanoApe Loves Sequence Ⅱ ——(尺取法)

题意:给出一个序列,问能找出多少个连续的子序列,使得这个子序列中第k大的数字不小于m. 分析:这个子序列中只要大于等于m的个数大于等于k个即可.那么,我们可以用尺取法写,代码不难写,但是有些小细节需要注意(见代码注释).我觉得,<挑战程序设计>里的尺取法的内容需要好好的再回顾一下= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namesp

hdu5504 GT and sequence(BestCoder Round #60 )

GT and sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 334 Accepted Submission(s): 85 Problem Description You are given a sequence of N integers. You should choose some numbers(at least o