zoukankan      html  css  js  c++  java
  • codeforces278A

    Circle Line

     CodeForces - 278A 

    郑州地铁的圆线有n个车站。 我们知道所有邻近站点之间的距离:

    d[1]是第1站和第2站之间的距离;
    d[2]是第2站和第3站之间的距离;

    d[n - 1]是第n-1和第n个站之间的距离;
    d[n]是第n个站和第1个站之间的距离。
    列车沿着两个方向的圆线。 找到具有数字s和t的站点之间的最短距离。

    Input

    第一行包含整数n(3≤n≤100),表示圆线上的站数。 第二行包含n个整数d[1],d[2],…,d[n](1≤d[i]≤100) 表示相邻站对之间的距离。 第三行包含两个整数s和t(1≤s,t≤n) 表示站点数量,您需要在这些站点之间找到最短距离。 这些数字可以相同。

    行中的数字由单个空格分隔。

    Output

    打印一个数字 ,站号s和t之间最短路径的长度。

    Examples

    样例1

    Input:
    4
    2 3 4 9
    1 3

    Output:
    5

    样例2

    Input:
    4
    5 8 2 100
    4 1

    Output:
    15

    样例3

    Input:
    3
    1 1 1
    3 1

    Output:
    1

    样例4

    Input:
    3
    31 41 59
    1 1

    Output:
    0

    Note

    在第一个样本中,路径1→2→3的长度等于5,路径1→4→3的长度等于13。

    在第二个样本中,路径4→1的长度为100,路径4→3→2→1的长度为15。

    在第三个样本中,路径3→1的长度为1,路径3→2→1的长度为2。

    在第四个样本中,站的数量是相同的,因此最短距离等于0。

    sol:这个显然是个环,用前缀和预处理两个点之间的一个方向的距离d1,总长-d1就是另一个方向的距离了

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=105;
    int n,Qzh[N];
    int main()
    {
        int i,S,T;
        R(n);
        Qzh[1]=0; for(i=2;i<=n+1;i++) Qzh[i]=Qzh[i-1]+read();
        R(S); R(T);
        if(S>T) swap(S,T);
        int tmp=Qzh[T]-Qzh[S];
        Wl(min(tmp,Qzh[n+1]-tmp));
        return 0;
    }
    View Code
  • 相关阅读:
    03_ if 练习 _ little2big
    uva 11275 3D Triangles
    uva 12296 Pieces and Discs
    uvalive 3218 Find the Border
    uvalive 2797 Monster Trap
    uvalive 4992 Jungle Outpost
    uva 2218 Triathlon
    uvalive 3890 Most Distant Point from the Sea
    uvalive 4728 Squares
    uva 10256 The Great Divide
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10575079.html
Copyright © 2011-2022 走看看