Highways(Zoj 2433)

题目链接

                                                                             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

题意:

   在一个很遥远的国家,有N个城市都坐落在公路旁边。公路是直线的,它从第一个城市开始,通过第二个,第三个,直到第N个。第i个城市在距离第一个Xi米处。 但是公路是单向的,人们只能从小号码城市开车到大号码城市(按照编号顺序)。 有个人人决定新建2条公路,使得每个城市可以相互走。每条新公路不能通过第三个城市。同时每个城市至多是一条公路的尽头。 
要求写出来一个程序,满足所有的要求,同时要使得成本最低。
 

思路:

首先城市数不能少于4,如果少于4,输出0。第一座城市和最后一座城市必须相连,那么就从中间找出最短的间隔长度加上第一座城市到最后一座城市的全长就行了。

 

温馨提示:注意输出的格式哦。

 

样例解释:

1是下边一组样例

4是有四个城市

3 5 10 分别是第二个、第三个、第四个城市到第一个城市的距离。

 

我们现在建的两条路是4------>2    和  3  --------> 1,4------>2   的距离是7  , 3  --------> 1 的距离是5,所以先输出的为12

然后我们输出两条路的编号。

 

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#define maxn 1000000001
using namespace std;
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        int n , mi , p , c , t , i;
        cin >> n;
        mi = maxn;
        p = 0 ;
        for(int j = 1 ; j < n ; j ++)
        {
            cin >> c;
            t = c - p;
            p = c ;
            if(j != 1 && j != n - 1 && t < mi)
            {
                mi = t;
                i = j;
            }
        }
        if(mi == maxn)
            cout<<"0"<<endl;
        else
        {
            cout<<c + mi << endl;
            cout<<i + 1 << " 1 " << n << " "<< i<<endl;
        }
        if(T)
            cout<<endl;
    }
    return 0 ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值