17/20,部分超时。

#include<bits/stdc++.h>
using namespace std;
int N,x,pairs;
int a,b;
vector<int> dist;
void input(){
cin>>N;
for(int i=0;i<N;i++){
cin>>x;
dist.push_back(x);
}
}
void solve(){
cin>>pairs;
for(int i=0;i<pairs;i++){
cin>>a>>b;
int sum1=0;
int sum2=0;
int sum=0;
if(a==b)cout<<0<<endl;
else if(a<b){
for(int j=a-1;j<b-1;j++)sum1+=dist[j];
for(int j=b-1;j<5;j++)sum2+=dist[j];
for(int j=0;j<a-1;j++)sum2+=dist[j];
}
else{
for(int j=a-1;j<5;j++)sum1+=dist[j];
for(int j=0;j<b-1;j++)sum1+=dist[j];
for(int j=b-1;j<a-1;j++)sum2+=dist[j];
}
sum=(sum1<sum2)?sum1:sum2;
cout<<sum<<endl;
}
}
int main(){
input();
solve();
return 0;
}

改进下,顺时针与逆时针路径之和是总路径长度,所以可以简化求sum2的方式。不过依旧超时17/20

#include<bits/stdc++.h>
using namespace std;
int N,x,pairs;
int a,b;
vector<int> dist;
int sum=0;
void input(){
cin>>N;
for(int i=0;i<N;i++){
cin>>x;
dist.push_back(x);
sum+=x;
}
}
void solve(){
cin>>pairs;
for(int i=0;i<pairs;i++){
cin>>a>>b;
int sum1=0;
int sum2=0; if(a==b)cout<<0<<endl;
else if(a<b){
for(int j=a-1;j<b-1;j++)sum1+=dist[j];
sum2=sum-sum1;
}
else{
for(int j=a-1;j<5;j++)sum1+=dist[j];
for(int j=0;j<b-1;j++)sum1+=dist[j];
sum2=sum-sum1;
}
cout<<((sum1<sum2)?sum1:sum2)<<endl;
}
}
int main(){
input();
solve();
return 0;
}

以上代码会超时是因为我们每次求两个结点的最短距离,都进行一次累加计算,每次查询就要遍历数组也就是10的5次方,而又有10的4次方次操作,所有共10的9次方。

我们需要通过记忆化来对最短距离存储,但是怎么存呢?

方案1:通过二维数组,array[low][high]

方案2:通过map,map<(low,high),int>

方案3:计算出1到其他结点顺时针的距离,通过差值计算两点最短距离。

事实证明方案3更好。

#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int N,x,pairs;
int a,b;
int dist[MAXN],A[MAXN];//A用来存放i到i+1之间的距离。Dist来存放结点1到结点i顺时针下个结点的距离。
int sum=0;
void input(){
cin>>N;
for(int i=1;i<=N;i++){
cin>>x;
A[i]=x;
sum+=x;
dist[i]=sum;
}
}
void solve(){
cin>>pairs;
for(int i=0;i<pairs;i++){
cin>>a>>b;
if(a>b)swap(a,b);
int temp=dist[b-1]-dist[a-1];
cout<<min(temp,sum-temp)<<endl;
}
}
int main(){
input();
solve();
return 0;
}

A1046. Shortest Distance(20)的更多相关文章

  1. A1046 Shortest Distance (20)(20 分)

    1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...

  2. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  3. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  4. PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  5. pat1046. Shortest Distance (20)

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  6. 1046 Shortest Distance (20 分)

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

  7. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

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

  8. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  9. A1046. Shortest Distance

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

随机推荐

  1. Linux查看系统负载(CPU和MEM考虑)

    查看占用CPU最高的10个进程 [tidb@:vg_adn_tidbCkhsTest:| head mysql ? Sl Nov22 : /usr/local/mysql/bin/mysqld --b ...

  2. 复杂json的解析:jsonobject与jsonArray的使用

    String parameter = { success : 0, errorMsg : "错误消息", data : { total : "总记录数", ro ...

  3. PyQt5---firstwindow

    # -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  4. (一)SSO之CAS框架通俗原理

    版权声明:本文为博主原创文章.转载请标明出处. https://blog.csdn.net/lovesummerforever/article/details/36068249 SSO统一验证     ...

  5. Kafka学习之路 (三)Kafka的高可用

    一.高可用的由来 1.1 为何需要Replication 在Kafka在0.8以前的版本中,是没有Replication的,一旦某一个Broker宕机,则其上所有的Partition数据都不可被消费, ...

  6. [转]System.DllNotFoundException: 无法加载 DLL“*.dll”: 内存位置访问无效。 (异常来自 HRESULT:0x800703E6)

    我在使用地税发票控件进行开票的测试的时候,在xp上测试时正常的,在别人的win7系统测试也是正常,但我在我本机确不正常.我本机装的是msdn版本win7系统,这个系统比较原装. 错误信息如下: -- ...

  7. JAVA框架 Spring 引入多个配置文件

    1.如果配置文件比较长,可以分多个配置文件.有两种方式: 1)在主配置文件加标签<import/> <import resource="jd/com/other/appli ...

  8. [笔记] ubuntu下添加windows的字体

    方法如下: 第一步:将windows下喜欢的字体文件copy到一个文件夹中,例如将XP里WINDOWS/FONTS中的字体文件(本人比较贪心,把整个文件夹copy了过来……),在linux中命名为xp ...

  9. Java 中long类型转换成为int类型时可能会出错的地方

    那计算两个日期之间间隔的天数为例来说明这个问题. 下面是计算日期间隔天数的简单算法(主要出错的地方为红色标注的地方): public int getDay(String startDate, Stri ...

  10. OO课程学期末总结

    OO课程学期末总结 测试VS正确性论证 OCL vs JSF 对象约束语言(Object Constraint Language), 简称OCL, 是一种指示用户建模系统中的限制方式. 他是UML可选 ...