1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
Input Specification:
Each input file contains one test case. For each case, the first line contains an integer N (in [3]), followed by N integer distances D1 D2 ⋯ DN, where Di is the distance between the i-th and the (-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (≤), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 1.
Output Specification:
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
Sample Input:
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output:
3题目分析:刚开始的直接暴力做 第三个点没过 看了别人的博客 认识了前缀数组
10
7
错误代码
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Dist[];
int N, M;
int Sum;
void Ans(int i, int j)
{
int sum = ;
if (i > j)Ans(j, i);
else
{
for (int k = i; k < j; k++)
sum += Dist[k];
sum = (sum < (Sum - sum)) ? sum : Sum - sum;
cout << sum << endl;
}
}
int main()
{ cin >> N;
for (int i = ; i <= N; i++)
{
cin >> Dist[i];
Sum += Dist[i];
}
cin >> M;
int v1, v2;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
Ans(v1, v2);
}
}
ac代码
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
vector<int>Dist;
int sum;
int main()
{
int N;
cin >> N;
Dist.resize(N + );
for (int i = ; i <=N; i++)
{
int temp;
cin >> temp;
sum += temp;
Dist[i] = sum;
}
int M;
cin >> M;
int v1, v2;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
if (v1 > v2)
swap(v1,v2);
int s1 = Dist[v2 - ] - Dist[v1 - ];
int s2 = sum - s1;
if (s1 > s2)
cout << s2 << endl;
else
cout << s1 << endl;
}
}
1046 Shortest Distance (20分)的更多相关文章
- 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: ...
- PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- 【PAT甲级】1046 Shortest Distance (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- 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 ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
- 1046 Shortest Distance (20)
#include<stdio.h> int main() { int n,m,a,b,tem,pre,p; int i,j; ]; while(scanf("%d",& ...
- PAT (Advanced Level) 1046. Shortest Distance (20)
处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
随机推荐
- 01-初识InfluxDB
初识InfluxDB 1. InfluxDB介绍 时间序列数据库,简称时序数据库,Time Series Database,一个全新的领域,最大的特点就是每个条数据都带有Time列. 时序数据库到底能 ...
- Redis03——Redis是如何删除你的数据的
众所周知Redis针对每一个key都能单独设置过期时间,那么Redis是怎么处理这些key的过期时间的呢?当同一时间有大量Key同时到期时,Redis又是怎么处理的呢?会不会影响到我的线上业务呢?如果 ...
- Leetcode 1160: 拼写单词
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌 ...
- css中:overflow:hidden清除浮动的原理
要想彻底清除浮动的影响,适合的属性不是 clear 而是 overflow. 一般使用 overflow:hidden,利用 BFC 的“结界”特性彻底解决浮动对外部或兄弟元素的影响. 1. 前言: ...
- CSS的SVG学习
SVG 意为可缩放矢量图形(Scalable Vector Graphics). HTML三种方法导入svg文件: <html xmlns:svg="http://www.w3.org ...
- python中那些让开发事半功倍的模块
1. Map Map会将一个函数映射到一个输入列表的所有元素上 ex: 有一个列表: [1,2,3,4,5,6], 现在要求把列表每个元素乘以10 如果你还不知道Map,那你可能会这样做: list1 ...
- Java第一节课动手动脑
在第一节课的动手动脑中,主要解决四则运算问题. 首先第一个是出30道四则运算题目,在100以内.这个问题需要控制随机数生成的范围和结果的范围在100以内就可以. 第一次改进是3点:一为避免重复,二为定 ...
- Spring生命周期详解
导读 Spring中Bean的生命周期从容器的启动到停止,涉及到的源码主要是在org.springframework.context.support.AbstractApplicationContex ...
- Pyinstaller通过spec文件打包py程序(多个py脚本)
Pyinstaller pyinstaller是python的一个第三方模块,使用它可以将python程序打包为可执行文件,实现打包后的程序在没有python环境的机器上也可以运行.pyinstall ...
- Css五种定位之间的区别
##CSS 定位机制## CSS 有三种基本的定位机制:普通流.浮动流和定位流. 除非专门指定,否则所有框都在普通流中定位.也就是说,普通流中的元素的位置由元素在 (X)HTML 中的位置决定. 块级 ...