zoj 2433 Highways(英语是硬伤啊)

简介:
Highways

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

In a distant country Lineland there are N cities and they are all located along the highway. The highway is a straight line; it starts from the first city and runs through the second, third city and so on, ending in the N-th city. The i-th city is located at the distance of Xi miles from the first one.

The highway is wide and smooth, so it is a pleasure for all people to drive along it. But there is one problem - all roads in Lineland, including the highway, are one-way. So people are only allowed to drive along the highway from the city with smaller number to the city with greater number and they have to use country roads to get back, and that is not such a great pleasure indeed.

After the new president Mr. Pathwayson was elected in Lineland, he has decided that he would like to make it easier for people to get from one town to another. But he does not dare to change the traditions, and make the highway two-way. Therefore he has decided to build new highways to connect the cities, so that it would be possible to get from any city to any other one by highways. Traditionally, the new highways must be one-way.

Of course, Mr. Pathwayson is a great president, and he wants people to remember him in years. After a thought he has decided that building just one highway would not be enough for that. Therefore he has decided that he must build two new highways. Each highway would connect two different cities. Since people are anxious about their health, and cars running along the highway produce dangerous wastes, each new highway must not pass through any cities, except the cities it connects. Also building two new highways in one city would disturb people too much, so all the cities that would be the ends of the new highways must be different.

You are the assistant of the minister of transportation of Lineland, so you are asked to choose the cities to be connected by the new highways. Since the cost of building a highway is proportional to its length, the total length of the highways must be minimal possible. Write a program to solve this problem. You may assume that the distance between two cities along the new highway is equal to the distance between those cities along the main highway.


Input

The first line of the input contains N (2 <= N <= 50,000).

Next line contains N-1 integer numbers: X2, X3, ..., XN (1 <= X2 < X3 < ...< XN <= 10^9).


Output

If it is impossible to build the highways satisfying all requirements, print number 0 on the first line of the output.

In the other case on the first line of the output file print the minimal possible total length of the highways to be built. On the second line print S1, E1, S2 and E2 - the numbers of the cities to connect by the first and the second highway, respectively. Note that highways are one-way and must run from S1 to E1 and from S2 to E2.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

1

4
3 5 10


Sample Output

12
3 1 4 2

分析:

(1)题意:有一条单行道,要你再建两条路,这两条路的首尾不能重合且覆盖全部旅程。其实题目的意思就是要找出N个数中相邻间距最小的两个数。

(2)注意输出格式

复制代码
#include <stdio.h>
#define MIN_NUM 1000000001

int main()
{
    int N,n,i,min,pre,cur,tem,index;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d",&n);
        min = MIN_NUM;
        pre = 0;
        for(i=1;i<n;i++)
        {
            scanf("%d",&cur);
            tem = cur - pre;
            pre = cur;
            if(i!=1&&i!=n-1&&tem < min)
            {
                min = tem;
                index = i;
            }
        }
        if(min==MIN_NUM)printf("0\n");
        else
        {
            printf("%d\n",cur+min);
            printf("%d 1 %d %d\n",index+1,n,index);
        }
        if(N)printf("\n");
    }
    return 0;
}
复制代码
本文转自NewPanderKing51CTO博客,原文链接:  http://www.cnblogs.com/newpanderking/archive/2012/10/05/2712239.html ,如需转载请自行联系原作者
相关文章
|
7月前
【每日一题Day334】LC2591将钱分给最多的儿童 | 贪心
【每日一题Day334】LC2591将钱分给最多的儿童 | 贪心
48 0
|
7月前
【每日一题Day126】LC1140石子游戏Ⅱ | 博弈dp 记忆化搜索
【每日一题Day126】LC1140石子游戏Ⅱ | 博弈dp 记忆化搜索
56 0
|
7月前
【每日一题Day338】LC2582递枕头 | 模拟+数学
【每日一题Day338】LC2582递枕头 | 模拟+数学
34 0
|
存储 AI芯片
百度松果菁英班--oj赛(第五次)
百度松果菁英班--oj赛(第五次)
82 0
|
算法 Go C语言
02【C语言 & 趣味算法】借书方案问题:小明有5本新书,要借给A、B、C三位小朋友,若每人每次只能借1本,则可以有多少种不同的借法?
02【C语言 & 趣味算法】借书方案问题:小明有5本新书,要借给A、B、C三位小朋友,若每人每次只能借1本,则可以有多少种不同的借法?
02【C语言 & 趣味算法】借书方案问题:小明有5本新书,要借给A、B、C三位小朋友,若每人每次只能借1本,则可以有多少种不同的借法?
|
机器学习/深度学习 算法
【第十五届蓝桥杯备赛(bushi,写文凑个数)】蓝桥OJ---长草
文章目录 一、题目 1、原题链接 2、题目描述 二、解题报告 1、思路分析 2、时间复杂度 3、代码详解 三、知识风暴 BFS Flood Fill算法
186 0
|
测试技术
(dfs)(枚举)第十四届蓝桥杯第三次模拟赛:9.最大滑雪长度
(dfs)(枚举)第十四届蓝桥杯第三次模拟赛:9.最大滑雪长度
134 0
【每日一题Day1】LC1700.无法吃午餐的学生数量
给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i 个三明治的类型(i = 0 是栈的顶部), students[j] 是初始队列里第 j 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。
75 0
|
算法
算法题每日一练---第1天:猴子分香蕉
5 只猴子是好朋友,在海边的椰子树上睡着了。这期间,有商船把一大堆香蕉忘记在沙滩上离去。
485 0
算法题每日一练---第1天:猴子分香蕉
|
人工智能 算法
爆表!猜猜这个大会的IQ总值有多高?
零售、工业、金融、城市、航空、自动驾驶…… 世界远比我们想象的更复杂,也正走向我们希望的简单。
3215 0