zoukankan      html  css  js  c++  java
  • 1046. Shortest Distance

    1046. Shortest Distance (20)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

    Output Specification:

    For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

    Sample Input:
    5 1 2 4 14 9
    3
    1 3
    2 5
    4 1
    
    Sample Output:
    3
    10
    7
     1 #include<stdio.h>
     2 #include<math.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 int dis[100010];
     6 int main()
     7 {
     8     int n, m, i, j, d, sum = 0;
     9     scanf("%d", &n);
    10     for(i = 1; i <= n; i++)
    11     {
    12         scanf("%d", &d);
    13         sum += d;
    14         dis[i] = sum;
    15     }
    16     scanf("%d", &m);
    17     for(i = 0; i < m; i++)
    18     {
    19         int x, y;
    20         scanf("%d%d", &x, &y);
    21         if(x > y)
    22         {
    23             int temp = y;
    24             y = x;
    25             x = temp;
    26         }
    27         int a1 = dis[y - 1] - dis[x - 1], a2 = sum - a1;
    28         if(a1 > a2)
    29         {
    30             int temp2 = a1;
    31             a1 = a2;
    32             a2 = temp2;
    33         }
    34         printf("%d
    ", a1);
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    第十二周作业
    第十一周作业
    第十周作业
    第九周作业*
    #**第八周作业+预习作业**
    第七周作业
    Linux 日志查看常用命令
    Linux tar命令
    Java 数组
    设计模式 观察者模式(Observer)
  • 原文地址:https://www.cnblogs.com/yomman/p/4267756.html
Copyright © 2011-2022 走看看