XidianOJ 1176 ship

题目描述

The members of XDU-ACM group went camp this summer holiday. They came across a river one day. There was a ship which only can carry at most two people at the same time. The ship would move only if there is at least one person in the ship to drive it. Everyone had different cost of time to pass the river, and the time of pass the river by ship depended on the longer time of the two passengers. You should tell them the minimum total time that all of the members should spend to arrive the next band.

输入

The input contains multiple test cases.
The first line of each case contains one integer n (1≤n≤100000). Then next n lines contains n positive integers a[i](1≤a[i]≤10000)-the ith person spend a[i] time to pass the river.

输出

For each case ,print the minimum total time they should spend in the only line.

--正文

首先按时间长短排好,则a[1]是最快的,a[2]次快,a[n]最慢,a[n-1]次慢

  若 (2*a[2]+a[1]+a[n] > 2*a[1]+a[n-1]+a[n])

  则每次先a[1],a[n]坐船,a[1]回来和a[n-1]坐,a[1]回来

  否则就每次a[1],a[2]坐船,a[1]回来,a[n-1]和a[n]坐,a[2]回来

  这样每次都少掉2个人,直到n<4为止

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n;
int a[100001];
long long res = 0;

void solve(int n){
    if (n <= 3){
        if (n == 3){
            res += a[1] + a[2] + a[3];
            return;
        }
        if (n == 2){
            res += a[2];
            return;
        }
        if (n == 1){
            res += a[1];
            return;
        }
    }
    res += min(2*a[2]+a[1]+a[n],2*a[1]+a[n-1]+a[n]);
    solve(n-2);
}

int main(){
    while (scanf("%d",&n) != EOF){
        int i;
        res = 0;
        for (i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n);
//        for (i=1;i<=n;i++){
//            printf("%d ",a[i]);
//        }
        solve(n);
        printf("%lld\n",res);
    }
} 

 

转载于:https://www.cnblogs.com/ToTOrz/p/6159579.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值