问题链接:https://paste.ubuntu.com/p/drXGZpQzdd/
解决办法:https://paste.ubuntu.com/p/4nvdVV3Fn9/
题目描述
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.
输入
Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 … DN, where Di is the distance between the i-th and the (i+1)-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 (<=104), 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 107.
输出
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
样例输入 Copy
5 1 2 4 14 9
3
1 3
2 5
4 1
样例输出 Copy
3
10
7
正确代码:
学习:定义maxn常数,数组定义。
当数据分散时,找第一个点为定点。
数组加减比循环计算省时。函数费时。
#include<stdio.h>
#define maxn 100010
int ans[maxn];
int path_len[maxn];
int main() {
int n;
while (scanf("%d", &n) != EOF)
{
ans[0] = 0;
path_len[0] = 0;
int i, all = 0;
for (i = 1; i < n + 1; i++) {
scanf("%d", &ans[i]);
all += ans[i];//边输入边计算总路程
path_len[i] = path_len[i - 1] + ans[i]; // 这里一次计算好每个站点到第一个站点的距离
}
int m;
scanf("%d", &m);
for (i = 1; i < m + 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
int j, temp = 0;
if (a < b) {//顺时针从小到大计算单向路程
temp = path_len[b - 1] - path_len[a - 1]; // 这里不能重复遍历计算
}
else {
temp = path_len[a - 1] - path_len[b - 1]; // 这里不能重复遍历计算
}
if (temp > (all / 2)) temp = all - temp;//取最短路程
printf("%d\n", temp);
}
}
return 0;
}
我的代码:
#include<stdio.h>
/*int sp(int ans[],int all,int a,int b){
int clw=0,clr,temp;
int i;
if(a>b){
temp=a;a=b;b=temp;
}
for(i=a;i<b;i++){
clw+=ans[i];
}
clr=all-clw;
return clw<clr?clw:clr;
}*/
int main(){
int n;
scanf("%d",&n);
int ans[n+1]={};
ans[0]=0;
int i,all=0;
for(i=1;i<n+1;i++){
scanf("%d",&ans[i]);
all+=ans[i];
}
int m;
scanf("%d",&m);
//int sum[m]={};
for(i=1;i<m+1;i++){
int a,b;
scanf("%d %d",&a,&b);
int j,temp=0;
if(a<b){
for(j=a;j<b;j++) temp+=ans[j];
}else{
for(j=b;j<a;j++) temp+=ans[j];
}
if(temp>(all/2)) temp=all-temp;
printf("%d\n",temp);
//sum[i-1]=temp;
//sum[i-1]=sp(ans,all,a,b);
}
/*for(i=0;i<m;i++){
printf("%d\n",sum[i]);
}*/
return 0;
}
和我一样超时。我改进后已达到1.2s。原版都是2s。
转自memcpy0
#include <cstdio>
int RightDistance(int a[], int left, int right) {
// [left, right)
int sum = 0;
for (int i = left - 1; i < right - 1; i++) // 逻辑序号映射到物理序号
sum += a[i];
return sum;
}
int LeftDistance(int a[], int N, int rightDist) {
int sum = 0;
for (int i = 0; i < N; i++)
sum += a[i];
return sum - rightDist; // 环的性质
}
int main()
{
int N, M;
while (scanf("%d", &N) != EOF) {
int a[N];
for (int i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &M);
int left, right;
while (M--) {
scanf("%d%d", &left, &right);
int r1, r2;
if (left > right) r1 = RightDistance(a, right, left); // 改变方向
else r1 = RightDistance(a, left, right);
r2 = LeftDistance(a, N, r1);
printf("%d\n", r1 < r2 ? r1 : r2);
}
}
return 0;
}