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 ...
随机推荐
- 深入浅出Web开发——Fiddler
环境配置: 如果使用Chrome,Fiddler无法捕捉HTTP请求信息,请检查Chrome是否使用SwitchyOmega插件.
- October 13th 2017 Week 41st Friday
The shortest distance between two people is a smile. 人与人之间最短的距离是微笑. I find a smiling face can bring ...
- LeetCode 休闲计划
老年退役选手的 LeetCode 休闲之旅 前言 不知不觉两年多的大学时光悄然流逝,浑浑噩噩的状态似乎从来没有离开过自己. 这两年刷题似乎一直是常态.在退役之后的现在,深感有些东西一段时间没有接触,很 ...
- 【Android自动化】测试系统的应用程序安装与卸载性能,判断长时间反复安装对系统的整体性能影响
# -*- coding:utf-8 -*- import sys import os import time import subprocess from uiautomator import de ...
- BZOJ 1087 互不侵犯King 状态压缩DP
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1087 题目大意; 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国 ...
- PhpStorm破解版及使用教程
本文引自网络,仅供本人学习使用之用,感谢网友的分享 PhpStorm PhpStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具,旨在提高用户效率,可深刻理解用户的编码,提 ...
- ERC 725 and ERC 735 的实现及关系
https://github.com/OriginProtocol/origin-playground 通过ERC 725 and ERC 735 的实现来说明它们到底是做什么的: 看了这个例子后才大 ...
- 2018-2019-2 20165302 《网络对抗技术》Exp4 恶意代码分析
实验要求 1.监控你自己系统的运行状态,看有没有可疑的程序在运行 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systracer套 ...
- ocv & derate & crpr
OCV: on-chip-variation 是指芯片在制造工艺P.工作电压V.环境温度T 等参数的局部变化情况下导致的 cell &net delay 变化,比如假设从clk 到两个reg ...
- 【转】阿里云Ubuntu系统搭建SVN服务器
##SVN服务器相关软件安装 1.使用SSH远程服务器 (1)对于MAC OS/Liunx的用户直接打开终端输入 ssh 用户名@实例名,例如 ssh root@192.168.1.100 执行上面 ...