NC24445. [USACO 2014 Dec B]Marathon
描述
输入描述
The first line gives the value of N.
The next N lines each contain two space-separated integers, x and y,
representing a checkpoint (-1000 <= x <= 1000, -1000 <= y <= 1000).
The checkpoints are given in the order that they must be visited.
Note that the course might cross over itself several times, with
several checkpoints occurring at the same physical location. When
Bessie skips such a checkpoint, she only skips one instance of the
checkpoint -- she does not skip every checkpoint occurring at the same
location.
输出描述
Output the minimum distance that Bessie can run by skipping up to one
checkpoint. Don't forget to end your output with a newline. In the
sample case shown here, skipping the checkpoint at (8, 3) leads to the
minimum total distance of 14.
示例1
输入:
4 0 0 8 3 11 -1 10 0
输出:
14
C++14(g++5.4) 解法, 执行用时: 44ms, 内存消耗: 1156K, 提交时间: 2020-08-23 11:14:38
#include<iostream> #include<algorithm> #include<cmath> using namespace std; int n,x[100005],y[100005]; int judge(int i,int j){ return abs(x[i]-x[j])+abs(y[i]-y[j]); } int main(){ cin>>n; int sum=0; for(int i=1;i<=n;i++){ cin>>x[i]>>y[i]; } for(int i=2;i<=n;i++){ sum+=judge(i-1,i); } int maxx=-1; for(int i=2;i<n;i++){ maxx=max(maxx,judge(i-1,i)+judge(i,i+1)-judge(i-1,i+1)); } cout<<sum-maxx<<endl; return 0; }
Python3(3.5.2) 解法, 执行用时: 722ms, 内存消耗: 28720K, 提交时间: 2020-04-26 14:07:51
N=int(input()) arr=[list(map(int,input().split())) for i in range(N)] skip_dis=[0 for i in range(N)] sumdis=0 max_skip=0 for i in range(1,N) : sumdis+=abs(arr[i][0]-arr[i-1][0])+abs(arr[i][1]-arr[i-1][1]) if i<=N-2 : tmp1=abs(arr[i][0]-arr[i-1][0])+abs(arr[i][1]-arr[i-1][1])+abs(arr[i][0]-arr[i+1][0])+abs(arr[i][1]-arr[i+1][1]) tmp2=abs(arr[i+1][0]-arr[i-1][0])+abs(arr[i+1][1]-arr[i-1][1]) max_skip=max(tmp1-tmp2,max_skip) print(sumdis-max_skip)
C++11(clang++ 3.9) 解法, 执行用时: 74ms, 内存消耗: 1368K, 提交时间: 2020-04-28 22:28:42
#include<iostream> #include<algorithm> #include<cmath> using namespace std; int n,x[100005],y[100005]; int judge(int i,int j){ return abs(x[i]-x[j])+abs(y[i]-y[j]); } int main(){ cin>>n; int sum=0; for(int i=1;i<=n;i++){ cin>>x[i]>>y[i]; } for(int i=2;i<=n;i++){ sum+=judge(i-1,i); } int maxx=-1; for(int i=2;i<n;i++){ maxx=max(maxx,judge(i-1,i)+judge(i,i+1)-judge(i-1,i+1)); } cout<<sum-maxx<<endl; return 0; }