zoukankan      html  css  js  c++  java
  • 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
    ", h);
    	}
    }

  • 相关阅读:
    .NET对象克隆的深究(转)
    25条哈佛成功金言 (转)
    __doPostBack()方法研究
    一个好的人事博客
    (转)IIS的inetinfo.exe进程占用CPU达100%解决办法
    .net开发随笔
    visual studio.net已检测到web服务器运行的不是asp.net1.1版"故障的排除
    Infragistics NetAdvantage控件的使用:该关系不是此 DataView 指向的表的父关系
    新的一年开始了
    六种方法,做一名更好的开发者
  • 原文地址:https://www.cnblogs.com/smuzoey/p/11787457.html
Copyright © 2011-2022 走看看