zoukankan      html  css  js  c++  java
  • 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分)
     

    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]), followed by Ninteger distances D1​​ D2​​ ⋯ DN​​, where Di​​ is the distance between the i-th and the (-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(≤), 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 1.

    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 <bits/stdc++.h>
     2 using namespace std;
     3 int n,m;
     4 int an[100005];
     5 int main(){
     6     cin >> n;
     7     int sum = 0;
     8     for(int i = 1; i <= n ; i ++){
     9         cin >> an[i];
    10         sum += an[i];
    11         an[i] += an[i-1];
    12     }
    13     cin >> m;
    14     int xx, yy, x, y;
    15     for(int i = 0; i < m; i ++){
    16         cin >> xx >> yy;
    17         y = max(xx,yy);
    18         x = min(xx,yy);
    19         int cnt = an[y-1]-an[x-1];
    20         int cnt1 = sum - cnt;
    21         int ans = min(cnt, cnt1);
    22         cout << ans << endl;
    23     }
    24     return 0;
    25 }


  • 相关阅读:
    C#快速开发平台(C/S架构+Winform+DevExpress+FastReport)
    C# Winform C/S系统快速开发框架企业版V4.5已发布,欢迎下载试用
    CSS3 Animation
    CSS3 Transition
    CSS3 Transform
    jQuery 屏蔽鼠标快速经过
    JQ插件jquery.fn.extend与jquery.extend
    font-size单位换算
    js获取客户端操作系统
    js数组的操作
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11182671.html
Copyright © 2011-2022 走看看