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);
    }
} 
时间: 2024-12-28 16:55:51

XidianOJ 1176 ship的相关文章

【BZOJ 1176】【Balkan 2007】Mokia

http://www.lydsy.com/JudgeOnline/problem.php?id=1176 整体二分的例题 把每个询问拆成四个询问,整体二分里x坐标递增,按x坐标扫的时候用树状数组维护y坐标前缀和. 一开始想复杂了,按cdq分治先solve左边再处理中间再solve右边,这样每次都要对x坐标排序,常数巨大,T了好几次TwT 后来参考了别人的代码,发现自己一开始就想复杂了.这道题不需要在solve完后还是保持原来的按x坐标递增的顺序,也不需要先处理出左边的信息才能更新右边的信息. 这

[2016-03-29][HDU][1176][免费馅饼]

时间:2016-03-29 09:46:34 星期二 题目编号:[2016-03-29][HDU][1176][免费馅饼] #include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxt = 100000 + 10; int dp[maxt][11]; int a[maxt][11]; int main(){ int n,x,t,maxT; whi

sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

DescriptionIn the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. T

Tyvj 1176 火焰巨魔的惆怅

Tyvj 1176 火焰巨魔的惆怅 背景 TYVJ2月月赛第一道 巨魔家族在某天受到了其他种族的屠杀,作为一个英雄,他主动担任了断后的任务,但是,在巨魔家族整体转移过后,火焰巨魔却被困住了,他出逃的方式也只有召唤小火人这一种方式,所以请你帮助他. 描述 我们把火焰巨魔所处的位置抽象成一张有向图,他的位置就是1号点位,目的就是走到第N号点位,因为小火人会裂嘛,所以我们可以看做每走一条路,小火人的数量都会加倍,而每条路上的敌人有多强,会消耗多少小火人c[i]也会给出(c[i]为负值):当然有些时候路

HDU 1176免费馅饼 DP数塔问题转化

L - 免费馅饼 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1176 Appoint description:  prayerhgq  (2015-07-28) System Crawler  (2015-11-21) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.

hdu 1176 免费馅饼 (dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1176 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int dp[100000+10][12]; int mat[100000+10][12]; int main() { int n; int i,j,k; int maxt; w

sicily 1176 two ends 动态规划解题

1176. Two Ends Constraints Time Limit: 1 secs, Memory Limit: 64 MB Description In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a c

Sicily 1176 Two Ends

1176. Two Ends Constraints Time Limit: 1 secs, Memory Limit: 64 MB Description In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a c

三方贸易-drop ship

一.三方贸易的简单理解 三方贸易:即当客户向我方下达采购订单要求订购货物,我方再向供应商下达相同采购订单,并要求供应商直接送货至客户处的交易形式,待交易完成后,由供应商向我方开具应收票据,我方向客户开具应收票据. 在ERP系统中,可通过Drop Ship的业务流程来实现. 如下图: 二.系统标准流程 Drop ship order 即在接单后,通过 Purchase release 发采购订单给 Supplier ,并直接指定由 supplier 送货给订货之 Customer.当 Custom