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

  • 相关阅读:
    5.线性回归算法
    作业14 15 手写数字识别-小数据集
    作业13 14 深度学习-卷积
    作业12 13-垃圾邮件分类2
    作业11 12.朴素贝叶斯-垃圾邮件分类
    作业10:11.分类与监督学习,朴素贝叶斯分类算法
    作业9、主成分分析
    作业8、特征选择
    作业7.逻辑回归实践
    作业6.逻辑归回
  • 原文地址:https://www.cnblogs.com/smuzoey/p/11787457.html
Copyright © 2011-2022 走看看