百度2017春招<度度熊回家问题>

题目:

一个数轴上共有N个点,第一个点的坐标是度度熊现在位置,第N-1个点是度度熊的家。现在他需要依次的从0号坐标走到N-1号坐标。
但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个点,并直接将这个点忽略掉,问度度熊回家至少走多少距离?

#include<iostream>  
#include<algorithm>    
#include<cmath>  
using namespace std;  
int main(){  
    int n;cin>>n;  
    int result=0;  
    vector<int>temp(n,0);  
    cin>>temp[0];  
    for(int i=1;i<n;++i){  
        cin>>temp[i];  
        result+=abs(temp[i]-temp[i-1]);  //在没有去除点时,总路程
    }  
    int maxx=0;  
    for(int i=1;i<temp.size()-1;++i){  
        maxx=max(maxx,abs(temp[i]-temp[i-1])+abs(temp[i]-temp[i+1])-abs(temp[i+1]-temp[i-1]));   //计算去掉中间点后  路程减少最多的距离
    }  
   
    cout<<result-maxx<<endl;  //总路程- 减少路程最多的即可
}  
原文地址:https://www.cnblogs.com/strongYaYa/p/6781171.html