题目描述

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

XidianOJ 1176 ship相关推荐

  1. c语言运算符感想,C语言运算符学习笔记

    运算符不仅具有不同的优先级,还有不同的结合性.在表达式中,各运算量参与运算的先后顺序不仅要遵守运算符优先级别的规定,还要受运算符结合性的制约,以便确定是自左向右进行运算还是自右向左进行运算. C语言的 ...

  2. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  3. BZOJ 1176([Balkan2007]Mokia-CDQ分治-分治询问)

    1176: [Balkan2007]Mokia Time Limit: 30 Sec   Memory Limit: 162 MB Submit: 185   Solved: 94 [ Submit] ...

  4. Java---先设计一个Moveable可移动接口,然后分别设计 3 个类,即汽车Car、轮船Ship、飞机Aircraft来实现该接口,最后设计一个应用程序来使用它们。

    要求如下: 设计一个Moveable可移动接口: 分别设计 3 个类,即汽车Car.轮船Ship.飞机Aircraft来实现该接口: 设计一个应用程序来使用它们. 源代码如下: import java ...

  5. Visual Studio 2005 Beta 2 will ship on April 25th

    Visual Studio 2005 Beta 2 will ship on April 25th<?xml:namespace prefix = o ns = "urn:schema ...

  6. HD 1176 免费馅饼 (DP)

    Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁 ...

  7. HDU 1176 免费馅饼 矩阵取数, dp + 滚动数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1176 首先可以处理出整张地图的状态. book[T][POS]表示第T秒,在第pos个地方有多少个馅饼. dp[ ...

  8. #1176 : 欧拉路·一(欧拉通路的判定)

    #1176 : 欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找 ...

  9. POJ 1176 Party Lamps USACO 2.2 派对灯(搜索)

    题目地址 http://poj.org/problem?id=1176 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都 ...

最新文章

  1. 【Java自顶向下】试手小公司,面试官问我ConcurrentHashMap,我微微一笑……
  2. 学习ASP.NET-初探Application状态
  3. 经典题目螺旋方阵的详解
  4. 接口入参形式_花椒测试平台 接口篇
  5. 呼叫我,或异步REST
  6. 深度学习-自然语言处理中的近似训练
  7. tqdm模块不能单行输出问题
  8. CSS Positioning页面
  9. 转:阿里负责人揭秘面试潜规则
  10. 11G新特性 -- ASM Fast Mirror Resync
  11. redis jar包_「建议收藏」手把手教你从零搭建一个redis服务
  12. android 开发按键精灵,安卓按键精灵实现后台
  13. 震惊!!【微信拼图红包】继微信语音红包后又一新技能
  14. PCB 18种特殊走线的画法与技巧!
  15. 在推送Git之前合并多个提交[重复]
  16. 2021-10-21-Flask-04 模型关系的建立,增删改查的演示,一篇文章让你直接搞懂Flask模型
  17. 名家名言 Chuck Thacker
  18. oracle SQL以结尾函数,Oracle SQL 内置函数大全
  19. Android下的单元测试
  20. currentTimeMillis()的解释

热门文章

  1. 十六进制转float和“float转十六进制”经典代码
  2. 【MFC】带组合框的工具栏
  3. 【IT资讯】Linus Torvalds:我们都老了,但Linux维护真的很难找
  4. 【Linux】一步一步学Linux——df命令(79)
  5. 深入理解python.md_linux-深入理解python.pdf
  6. python开发cs软件_python cs架构实现简单文件传输
  7. fullcalendar5.X版本 显示自定义html内容
  8. java 与 乱码问题_透彻分析和解决一切javaWeb项目乱码问题
  9. mysql update 几万 非常慢_mysqL update 太慢,求解决方法
  10. Android开源底部导航,一个开源JPTabBar for Android,炫酷的底部导航栏