Highways
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2730   Accepted: 1008   Special Judge

Description

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 X i 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 <= 109 ).

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 .

Sample Input

4
3 5 10

Sample Output

12
3 1 4 2

题意是有N个城市在一条快速公路上,这条快速公路是一条直线。可是这条快速公路是单向的,人们去的时候能够走快速公路。回来的时候又仅仅能走country road了。

所以新上任的市长打算在回去的方向上建立两条路(事实上一条整个回去的路就足够了。可是市长任性,就两条)。满足路上的城市都能来回都走上快速公路,问修这两条路的最短距离是多少。

要在回去的方向上修两条可以在全部城市都能互相来回,说明这两条路必定反复了一段距离。那既然必定要反复一段距离的话,又要修这两条路的距离最短,自然要覆盖那个两个城市之间最短距离的那个了。

这个题 题意比题都难理解。另外尽管是水题。仅仅须要找这些线段中的最短线段。但我自己还是WA了几次,原因是要考虑到第一条和最后一条线段是不能被选择为反复的。原因题目中说了: Each highway would connect two different cities.

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int dis[50002]; int main()
{
int n,i,min,min_x;
cin>>n; dis[0]=0;
min=1000000000; cin>>dis[1]; for(i=2;i<n-1;i++)//得考虑到不是第一条线段和最后一条线段的情况
{
cin>>dis[i];
if(dis[i]-dis[i-1]<min)
{
min=dis[i]-dis[i-1];
min_x=i;
}
}
cin>>dis[n-1]; if(n>=4)
{
cout<<min+dis[n-1]<<endl;
cout<<min_x+1<<" "<<1<<" "<<n<<" "<<min_x<<endl;
}
else
{
cout<<0<<endl;
}
return 0;
}

POJ 2128:Highways的更多相关文章

  1. POJ 2485:Highways(最小生成树&amp;&amp;prim)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21628   Accepted: 9970 Descrip ...

  2. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool ...

  3. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  4. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  5. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  8. POJ 3281:Dining(最大流)

    http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...

  9. POJ 3580:SuperMemo(Splay)

    http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...

随机推荐

  1. PHP PDO fetch() 详解

    环境:(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0) PDOStatement::fetch — 从结果集中获取下一行 说明 PDOStatement ...

  2. python中 in, any 和 all用法

    in if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') any if x or y o ...

  3. 软工实践第八次作业——UML设计

    本次作业博客 团队组成 临时组长:何裕捷 组员:蔡子阳,陈德斌,胡青元,李麒,高裕翔,王焕仁,黄培鑫 UML 用例图 描述的部分: 1 这里是用户个人管理系统的用例图 面临的问题: 1 面临用户登录注 ...

  4. mapserver+QGIS+openlayers的安装和配置

    1.下载MS4W,不要怀疑MS4W就是mapserver,只是里面集成了一些其他的工具和库,下载地址:http://www.maptools.org/ms4w/index.phtml?page=dow ...

  5. cf 853 B Jury Meeting [前缀和]

    题面: 传送门 思路: 看完题目以后,首先有一个结论:每个人都是先去到首都,等待开会,开会结束以后再一个个走掉 而且这道题只有去首都和离开首都的机场 因此考虑计算去首都的飞机的前缀最小花费,以及离开首 ...

  6. hdu 4096 判断路径

    思路:将每个关系当成一条有向边,查询时就判断之间存在路径. #include<iostream> #include<cstdio> #include<cstring> ...

  7. BZOJ5306 [HAOI2018]染色 【组合数 + 容斥 + NTT】

    题目 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度为 \(N\) 的序列, 每个位置都可以被染成 \(M\) 种颜色中的某一种. 然而小 C 只 ...

  8. html table 表格

    前面的话 在CSS出现之前,table元素常常用来布局.这种做法在HTML4之后不再推荐使用.而现在有些矫枉过正,使用table展示数据都可能会被说不规范.本文将详细介绍HTML表格table tab ...

  9. 品酒大会(uoj 131)

    一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加. 在大会的晚餐上,调酒师 Rainbow 调制了 ...

  10. 回文串(bzoj 3676)

    Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...