HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)
2016-08-06 22:05
495 查看
先找相邻差值的最大,第二大,第三大
删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可
删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可
#include <iostream> #include <cstdio> #include <cmath> using namespace std; #define LL long long const int N = 100005; int t, n, p1, p2, p3; LL a ; LL s1 , s2 ; LL sum; int main() { scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%I64d", &a[i]); p1 = p2 = p3 = 0; s1[0] = -1; for(int i = 1; i < n; i++) { s1[i] = abs(a[i + 1] - a[i]);//相邻 if(s1[p1] < s1[i] ) p1 = i;//最大 } for(int i = 1; i < n; i++) { if(i == p1) continue; else if(s1[p2] < s1[i]) p2 = i;//第二大 } for(int i = 1; i < n; i++) { if(i == p1 || i == p2) continue;//第三大 else if(s1[p3] < s1[i]) p3 = i; } for(int i = 2; i < n; i++) s2[i] = abs(a[i+1] - a[i-1]);//去掉 i 点新增值 sum = 0; if(p1 == 1) sum += s1[p2]; else sum += s1[p1]; for(int i = 2; i < n; i++) { if(p1 == i-1 || p1 == i) { if(p2 == i-1 || p2 == i) sum += max(s1[p3], s2[i]); else sum += max(s1[p2], s2[i]); } else sum += max(s1[p1], s2[i]); } if(p1 == n - 1) sum += s1[p2]; else sum += s1[p1]; printf("%I64d\n",sum); } }
相关文章推荐
- HDU-5805-NanoApe Loves Sequence(思维题)
- hdu_5805_NanoApe Loves Sequence(xjb搞)
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence (模拟)
- hdu:5805:前缀和后缀NanoApe Loves Sequence
- HDU 5805 - NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence(简单思路题)
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002
- HDU 5805 NanoApe Loves Sequence(ST 表 or 预处理)
- HDU 5805 NanoApe Loves Sequence 线段树
- hdu 5805 NanoApe Loves Sequence (水题)
- hdu-5805 NanoApe Loves Sequence(线段树+概率期望)
- HDU 5805 NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence(水~)
- HDU 5805:NanoApe Loves Sequence
- 【HDU】5805 - NanoApe Loves Sequence(模拟)
- HDU 5805 NanoApe Loves Sequence