这道题写的比较久额 。。 自己的方法比较笨。 因为出口形成的是一个圈,先是顺时针算一遍距离 ,再逆时针算一遍距离 ,然后再找出小的。
题目描述
输入
输出
样例输入
5 1 2 4 14 9
3
1 3
2 5
4 1
样例输出
3
10
7
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
// TODO: 在此处引用程序需要的其他头文件
源文件
// ProbblemE.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int main()
{
int N = 0, M = 0;
int n[10000] = { 0 }, m1[1000] = { 0 }, m2[1000] = { 0 }, result[1000]={ 0 }, min[1000] = {0};
cin >> N;
for (int i = 0; i < N; i++) {
cin >> n[i];
}
cin >> M;
for (int i = 0; i < M; i++) {
cin >> m1[i] >> m2[i];
}
//顺着检测 m1--->m2
for (int i = 0; i < M; i++) {
result[1000] = { 0 };
if (m2[i] > m1[i]) {
int count = m2[i] - m1[i]; //个数
int start = m1[i] - 1; //从m1[i]开始
for (int j = 0; j < count; j++) {
result[i] += n[start++];
}
}
else {
//分成两段
int start1 = m1[i] - 1;
int end1 = N;
int count = end1 - start1;
for (int j = 0; j < count; j++) {
result[i] += n[start1++];
}
//第二段
if (m2[i] > 1) {
int count2 = m2[i] - 1;
for (int j = 0; j < count2; j++) {
result[i] += n[j];
}
}
}
min[i] = result[i];
}
//反着检测
for (int i = 0; i < M; i++) {
result[i] = { 0 };
if (m2[i] < m1[i]) {
int count = m1[i] - m2[i];
int start = m1[i] - 2;
for (int j = 0; j < count; j++) {
result[i] += n[start--];
}
}
else {//分两段
int count1 = m1[i] - 1;
for (int j = 0; j < count1; j++) {
result[i] += n[j];
}
int count2 = N - m2[i]+1;
for (int j = 0; j < count2; j++) {
int temp = N;
result[i] += n[--temp];
}
}
}
for (int i = 0; i < M; i++) {
if (min[i] > result[i]) {
min[i] = result[i];
}
cout << min[i] << endl;
}
system("pause");
return 0;
}
结果