PAT 1046. Shortest Distance (20)

原创 2015年07月07日 17:05:35

模拟 10^4 * 10^5 会超时,通过设置一个0点,通过计算每个点到0的距离,两点之间的最短距离只需要相减就可以了。
代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
  int n;
  while(cin>>n)
  {
    int d[100005],dis;
    d[0] = 0;
    for(int i = 1; i <= n; i++)
    {
    cin>>dis;
    d[i] = d[i-1] + dis;
  }
    int m;
    cin>>m;
    while(m--)
    {
      int a,b;
      cin>>a>>b;
      if(a>b)
      {
        int temp = a;
        a = b;
        b = temp;
      }
    int min = d[b-1] - d[a-1];
    if(min < d[n] - min)
      cout<<min<<endl;
    else
      cout<<d[n] - min<<endl;
    }
  }
  return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

1046. Shortest Distance (20)-PAT甲级

题目:The task is really simple: given N exits on a highway which forms a simple cycle, you are suppose...

PAT A1046 Shortest Distance (20)

题目地址:https://www.patest.cn/contests/pat-a-practise/1046题目描述:The task is really simple: given N exits...

PAT-A 1046. Shortest Distance (20)

PAT甲级 1046 Shortest Distance

浙江大学PAT_甲级_1046. Shortest Distance (20)

C++

pat1046Shortest Distance (20)

题意分析: (1)给出一个环形公路中几个相邻出口之间的距离,然后任意给出若干对出口,求出这些出口之间最短的距离,(即从一个出口沿着两个方向到达另外一个出口的最短距离) (2)建议大家画一个示意图,分别...

PAT 1046. Shortest Distance (20)

1046. Shortest Distance (20) 浙大PAT The task is really simple: given N exits on a highway which forms...

pat 1046. Shortest Distance (20)

1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standa...

PAT 1046. Shortest Distance (20)

1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 1600...

1046. Shortest Distance (20)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t...

1046. Shortest Distance (20)

题目如下: The task is really simple: given N exits on a highway which forms a simple cycle, you a...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)