ZOJ 1877 Bridge

7 篇文章 0 订阅
Bridge

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

n people wish to cross a bridge at night. A group of at most two people may cross at any time, and each group must have a flashlight. Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross. 

Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member. Your job is to determine a strategy that gets all n people across the bridge in the minimum time.


Input

The first line of input contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1000 people and nobody takes more than 100 seconds to cross the bridge.

Input contains multiple test cases. Process to the end of file.


Output

The first line of output must contain the total number of seconds required for all n people to cross the bridge. The following lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross. (Each person is indicated by the crossing time specified in the input. Although many people may have the same crossing time the ambiguity is of no consequence.) Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross. If more than one strategy yields the minimal time, any one will do.


Sample Input

4
1
2
5
10


Sample Output

17
1 2
1
5 10
2
1 2


贪心问题,有两种最优方式,一种是最小次小走与最大次大走轮流的搭配,另一种是最小最大一块走的搭配,需要一定的判定条件 ~
在此给出两种不同情况的数据
第一种(样例):

4
1
2
5
10
第二种:
4
1
3
4
5

代码如下:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

int cmp(const void *a, const void *b)
{
    return *(int*)a > *(int*)b ? 1 : -1;
}
int a[1002], c[10000];
int main()
{
#ifdef test
    freopen("in.txt", "r", stdin);
#endif
    int num;
    while(scanf("%d", &num) != EOF)
    {
        int cnt = 0;
        int sum = 0;
        for(int i = 0; i < num; i++)
            scanf("%d", &a[i]);
        if(num != 1)
        {
            qsort(a, num, sizeof(a[0]), cmp);
            for(int j = num - 1; j > 1;)
            {
                if(a[j - 1] + a[0] > a[1] * 2) // 判定条件:如果次大和最小的和大于次小的两倍的话,走第一种情况
                {
                    c[cnt++] = a[0];
                    c[cnt++] = a[1];
                    sum += a[1];
                    c[cnt++] = a[0];
                    sum += a[0];
                    c[cnt++] = a[j - 1];
                    c[cnt++] = a[j];
                    sum += a[j] + a[1];
                    c[cnt++] = a[1];
                    j -= 2;
                }
                else              // 否则走第二种
                {
                    c[cnt++] = a[0];
                    c[cnt++] = a[j--];
                    sum += a[j + 1] + a[0];
                    c[cnt++] = a[0];
                }
            }
            c[cnt++] = a[0];
            c[cnt++] = a[1];
            sum += a[1];
            printf("%d\n",sum);
            for(int i = 0, j = 0; i < cnt; j++)
                if(j % 2)
                    printf("%d\n", c[i++]);
                else
                {
                    printf("%d %d\n", c[i], c[i + 1]);
                    i += 2;
                }
        }
        else // 注意只有一个人过桥的情况要特殊处理,因为这个WA了两遍还耽误了很多时间,感觉特别亏
            printf("%d\n%d\n", a[0], a[0]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值