A1046. Shortest Distance(20)
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)的更多相关文章
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- 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 ...
- pat1046. Shortest Distance (20)
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- A1046. Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
随机推荐
- OpenGL超级宝典笔记——贝塞尔曲线和曲面(转)
http://my.oschina.net/sweetdark/blog/183721 参数方程表现形式 在中学的时候,我们都学习过直线的参数方程:y = kx + b;其中k表示斜率,b表示截距(即 ...
- mysql 大文件导入导出
导出:mysqldump -u用户名 -p密码 -hIP地址 数据库名 > /dump.sql示例:mysqldump -uroot -proot -h127.0.0.1 test > / ...
- Java Collection集合方法
一.简单方法 package cn.itcast.day15; import java.util.ArrayList; import java.util.Arrays; import java.uti ...
- DevExpress10、RichEditControl
1.概述 传统.NET界面也有一个RichTextBox控件,一个富文本控件,可存储图片文字,有自己的文件格式RTF. 在DevExpress控件组里面也有一个同等的控件,RichEditContro ...
- [提权]域内提权神器 MS14-068 完整EXP
可以让任何域内用户提升为域管理员 c:\python27\python.exe ms14-068.py -u k8test3@k8.local -p k8team!@# -s S-1-5-2 ...
- [python] 列表解析式的高效与简洁
方法一(列表解析式): list1 = ["abc","efg","hij"] list2 = [i[0] for i in list1] ...
- Python接口自动化--requests 1
# _*_ encoding:utf-8 _*_ import requests #请求博客园首页,无参数的get请求 r = requests.get('http://www.cnblogs.com ...
- 13.5.SolrCloud集群使用手册之数据导入
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.使用curl命令方式 SolrCloud时会根据路由规则路由到各个shard. 删除所有数据 curl h ...
- 压缩tar: Removing leading `/’ from member names
这个错误根据网上的很多说法都是谁 缺少-P参数造成的,只需要使用 -zcvfP即可解决问题,经验证并不是 网上很多文章都是互相抄写的,我引起的这个问题的原因是参数使用的方式不对 -f参数是用来制定压缩 ...
- 基于window 7安装ubuntu 18.04双系统
window7下安装ubuntu双系统 1.首先下载ubuntu镜像文件 进入ubuntu官网,http://releases.ubuntu.com/18.04/.下载最新镜像,ubuntu-18.0 ...