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,10​5​​]), followed by N integer distances D​1​​ D​2​​ ⋯ D​N​​, where D​i​​ is the distance between the i-th and the (i+1)-st exits, and D​N​​ 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 (≤10​4​​), 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 10​7​​.

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

本来对每个要求的case分别计算顺时针走和逆时针走分别所需的路径长度,再把比较小的那个放在数组里最后统一输出,但是提交到pat上后最后一个测试用例超时了,代码如下

#include <iostream>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    int N,M;
    cin>>N;
    int arr[99999];
    for(int i=0;i<N;i++)
    {
        cin>>arr[i];
    }
    cin>>M;
    int resultArr[10000];
    for(int i=0;i<M;i++)
    {
        int tempStart,tempEnd;
        cin>>tempStart>>tempEnd;
        int temp;
        if(tempStart>tempEnd)
        {
            temp=tempEnd;
            tempEnd=tempStart;
            tempStart=temp;
        }
        int count1=0,count2=0;
        for(int j=tempStart-1;j<tempEnd-1;j++)
        {
            count1+=arr[j];
        }
        for(int j=tempEnd-1;j<=N-1;j++)
        {
            count2+=arr[j];
        }
        for(int j=0;j<=tempStart-2;j++)
        {
            count2+=arr[j];
        }
        if(count1<=count2)
        {
            resultArr[i]=count1;
        }
        else
        {
            resultArr[i]=count2;
        }
    }
    for(int i=0;i<M;i++)
    {
        cout<<resultArr[i];
        if(i!=M-1)
        {
            cout<<endl;
        }
    }
    return 0;
}

然后去看了其他博主的博客,就是把思路让数组里的每个元素存储到下一个节点的路径长度转化成数组里的每个元素存储从第一个节点出发到后面各节点的顺时针长度 。然后就通过了。代码如下:

#include <iostream>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    int N,M;
    cin>>N;
    int arr[99999];
    for(int i=0;i<=N;i++)
    {
        if(i==0)
        {
            arr[i]=0;
        }
        else
        {
            int temp;
            cin>>temp;
            arr[i]=arr[i-1]+temp;
        }
    }
    cin>>M;
    int resultArr[10000];
    for(int i=0;i<M;i++)
    {
        int count1=0,count2=0;
        int temp1,temp2;
        cin>>temp1>>temp2;
        if(temp1>temp2)
        {
            int temp=temp1;
            temp1=temp2;
            temp2=temp;
        }
        count1=arr[temp2-1]-arr[temp1-1];
        count2=arr[N]-arr[temp2-1]+arr[temp1-1];
        if(count1<count2)
        {
            resultArr[i]=count1;
        }
        else
        {
            resultArr[i]=count2;
        }
    }
    for(int i=0;i<M;i++)
    {
        cout<<resultArr[i];
        if(i!=M-1)
        {
            cout<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38279908/article/details/87861195
今日推荐