Atcoder Beginner Contest 092 —— C题

题目链接:https://abc092.contest.atcoder.jp/tasks/arc093_a

There are N sightseeing spots on the x-axis, numbered 1,2,…,N. Spot i is at the point with coordinate Ai. It costs |ab| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis. In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i. You will visit the remaining spots as planned in the order they are numbered. You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i=1,2,…,N, find the total cost of travel during the trip when the visit to Spot i is canceled.


坐标轴上,输入n个数,输进去的顺序就是固定的顺序,并不是按照数轴从左到右重新排序(并且第一个和第二个例子符合重新排序的答案)。然后依次少从第一个点开始缺少,并输出需要的路程,路程为第i个点 减去第i+1个点的绝对值,依次相加。需要从起点0点出发并且回到0点。所以啊a[0] = a[n + 1] = 0.

AC代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MaxN = 1e5 + 5;

int a[MaxN];
int b[MaxN];

int main()
{
	int n;
	long long sum = 0;
	scanf("%d", &n);
	a[0] = 0;
	a[n + 1] = 0;
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		b[i] = fabs(a[i] - a[i - 1]);
		sum = sum + b[i];
	}
	b[n + 1] = fabs(a[n + 1] - a[n]);
	sum = sum + b[n + 1];
	int h;
	for(int i = 1; i <= n; i++) {
		h = sum - b[i] - b[i + 1] + fabs(a[i+1] - a[i-1]);
		printf("%d\n", h);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40844130/article/details/79703363