hdu 5805 NanoApe Loves Sequence (水题)
来源:互联网 发布:流动人口大数据 编辑:程序博客网 时间:2024/06/06 02:25
题意:n个数,把每个数都取出一遍,问相临两数之差绝对值的期望(结果要乘上n,那就是求所有差的绝对值的和)
思路:找原序列3个最大的差的绝对值出来,对每个拿出的数左右之差进行比较,取大者相加。(这个方法有些冗杂,官方题解简洁。)
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;struct node{ int dx; int i1,i2;}p[100100];int a[100100];bool cmp(const node &x ,const node &y ){ return x.dx > y.dx;}int myabs(int x){ if(x < 0) x = -x; return x;}int main(){ int t; scanf("%d",&t); while(t--){ int n,m=0; scanf("%d",&n); if(n==3){ for(int i = 1;i<=n;i++) scanf("%d",&a[i]); long long sum = myabs(a[3] - a[2])+myabs(a[2]-a[1])+myabs(a[3]-a[1]); printf("%I64d\n",sum); } else{ scanf("%d",&a[1]); for(int i =2;i<=n;i++){ scanf("%d",&a[i]); p[m].dx = myabs(a[i] - a[i-1]); p[m].i2 = i;p[m++].i1 = i-1; } sort(p,p+m,cmp); long long ans = 0; if(p[0].i1!=1) ans += p[0].dx; else ans+=p[1].dx; for(int i = 2;i<=n-1;i++){ long long c = 0; if(i==p[0].i1 || i==p[0].i2){ if(i == p[1].i1 || i==p[1].i2){ c = (long long)max(p[2].dx,myabs(a[i+1]-a[i-1])); } else{ c = (long long)max(p[1].dx,myabs(a[i+1]-a[i-1])); } } else{ c = (long long)max(p[0].dx,myabs(a[i+1]-a[i-1])); } ans+=c; } if(p[n].i2!=n) ans += p[0].dx; else ans+=p[1].dx; printf("%I64d\n",ans); } } return 0;}
0 0
- 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
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence [Ad Hoc]
- Hdu 5805 NanoApe Loves Sequence【思维题】
- HDU 5805 NanoApe Loves Sequence(思维)
- 【HDU】5805 - NanoApe Loves Sequence(模拟)
- HDU 5805 NanoApe Loves Sequence(水~)
- NSIS System Plug-in
- epoll的优点
- EventBus源码阅读(1)-ThreadMode
- VS 宏目录配置(ICE项目属性配置)
- hadoop HDFS存储原理
- hdu 5805 NanoApe Loves Sequence (水题)
- lanchimage不显示
- CAS单点登录(二)---非SSL协议 CAS服务端部署及客户端配置
- 从零开学FastDFS之二(配置篇)
- 加油站
- 点对点 与 端对端 的区别
- boost::asio::async_write() versus boost::asio::write()
- toj 4606 Homework Checker
- 选择排序基本思想和实现代码