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);
    	}
    }

  • 相关阅读:
    Yii2框架之旅(三)
    Yii2框架之旅(二)
    Redis本地集群搭建(5版本以上)
    Redis入门笔记
    Java如何使用elasticsearch进行模糊查询--转载
    springboot集成elasticsearch7.6.1,常用api调用,创建,查找,删除索引,crud,高亮。。。--转载
    SpringBoot整合Elasticsearch7.2.0的实现方法-转载
    Spring Webflux 入门 -转载
    java 视频流截屏,形成缩略图
    记录一下 spring boot 线程处理返回数据
  • 原文地址:https://www.cnblogs.com/smuzoey/p/11787457.html
Copyright © 2011-2022 走看看