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. Hadoop 本地模式安装

    0. 说明 本地模式:使用的存储系统,是Linux系统 提前安装好 JDK 参考 CentOS7 安装 JDK 1. 将 Hadoop 的安装包通过 Xftp 发送到centos 用户的 home 目 ...

  2. MySQL日常运维操作---持续更新

    1.查看当前连接数: 这些参数都是什么意思呢? Threads_cached ##mysql管理的线程池中还有多少可以被复用的资源 Threads_connected ##打开的连接数 Threads ...

  3. 高斯消去、追赶法 matlab

    1. 分别用Gauss消去法.列主元Gauss消去法.三角分解方法求解方程组 程序: (1)Guess消去法: function x=GaussXQByOrder(A,b) %Gauss消去法 N = ...

  4. Alpha冲刺报告(9/12)(麻瓜制造者)

    今日已完成 邓弘立: 正在进行主页逻辑的编写 符天愉: 部署商品发布和物品需求的接口 江郑: 尝试完善接口文档,进行进一步测试 刘双玉: 编写接口说明 肖小强: 进行逻辑模块的编写 李佳铭: 修改了U ...

  5. Django商城项目笔记No.13用户部分-用户中心个人信息

    首先处理个人信息的显示 邮箱绑定: 首先给用户的模型类里添加一个字段来说明用户的邮箱是否激活 然后数据库迁移 python manage.py makemigrations python manage ...

  6. 4.Dubbo2.5.3集群容错和负载均衡

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.集群容错和负载均衡原理 各节点关系: 这里的Invoker是Provider的一个可调用Service的抽 ...

  7. 10.Solr4.10.3数据导入(DIH全量增量同步Mysql数据)

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.创建MySQL数据 create database solr; use solr; DROP TABLE ...

  8. [BeiJing2006]狼抓兔子

    题面 一眼看就是最小割板子题,建图也很直观,注意每一条边建双向边其实不用建4条边,只要反向边的容量和正边相同就行.然后直接跑最大流板子就行.不过此题拿vector存图会MLE……而拿链前存图就能卡过去 ...

  9. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  10. JavaScript 删除数组中的对象

    1.获得对象在数组中的下标 function (_arr,_obj) { var len = _arr.length; for(var i = 0; i < len; i++){ if(_arr ...