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. MongoDB基础之 用户和数据库基于角色的访问控制

    mongod 关键字参数:--auth 默认值是不需要验证,即 --noauth,该参数启用用户访问权限控制:当mongod 使用该参数启动时,MongoDB会验证客户端连接的账户和密码,以确定其是否 ...

  2. October 28th, 2017 Week 43rd Saturday

    All I ever wanted was a single thing worth fighting for. 我想要的只不过是一件能让我奋不顾身的事业. Stop complaining the ...

  3. [Python2]介绍关于Uiautomator的watcher使用场景及使用方法

    [官方的介绍]: Watcher You can register watcher to perform some actions when a selector can not find a mat ...

  4. 1.Redis安装(Linux环境)

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.Redis安装 使用的最新版本为 3.2.9,下载并安装: wget http://download.re ...

  5. 详解Web请求中的DNS域名解析

    当我们打开浏览器,输入一个URL去请求我们需要的资源,但是URL是需要解析成对应的IP地址才能与远程主机建立连接,如何将URL解析成IP就是DNS的工作范畴,即使作为开发人员,这个过程我们也感觉不到, ...

  6. BZOJ3879:SvT(后缀数组,单调栈,ST表)

    Description (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始 ...

  7. rebase合并commit步骤详解

    网上关于rebase合并commit有很多文章,但大部分中间一些步骤没有写明 第一步:在终端输入 git rebase -i [startPoint] [endPoint] 并回车 第二步:编辑指令, ...

  8. 在ROS中使用QT界面

    在终端可以直接用catkin_create_qt_pkg命令创建带Qt界面的ROS package,再按照前面说的方法导入到Qt即可 这里参考的是qt_createTutorialsQt App Te ...

  9. Android-ProgressDialog点击对话框外部是不让其消失

    1)ProgressDialog.setCanceledOnTouchOutside(false); 调用这个方法时,按对话框以外的地方不起作用.按返回键还起作用 2)ProgressDialog.s ...

  10. Ueditor使用笔记

            富文本编辑器在javaweb项目中还是比较常见的,如:ckeditor.kindeditor.ueditor等.今天主要叙述的对象为ueditor,它属于百度的.闲话不多说,下面开始介 ...