原题:

http://acm.timus.ru/problem.aspx?space=1&num=1826

1826. Minefield
Time limit: 0.5 second
Memory limit: 64 MB
To fulfill an assignment, a reconnaissance group of n people must cross the enemy's minefield. Since the group has only one mine detector, the following course of action is taken: two agents cross the field to the enemy's side and then one agent brings the mine detector back to the remaining group. This is repeated until only two agents remain. These two agents then cross the field together.
Each person gets across the field at their own speed. The speed of a pair is determined by the speed of its slower member.
Find the minimal time the whole group needs to get over the minefield.
Input
The first line contains the integer n (2 ≤ n ≤ 100). The i-th of the following n lines specifies the time the i-th member of the group needs to get over the minefield (the time is an integer from 1 to 600).
Output
Output the minimal total time the group needs to cross the minefield.

2~100个人要过雷区,只有一个探雷器,一次最多只能2个人过去,所以过去的流程是:过去2个回来一个过去2个回来一个…直到只剩2个的时候一起过去。2个一起走的时候以速度慢的人的速度为准。样例我看了好久没看懂,样例是4个人,单独过去的时间分别为1,2,5,10,先是1和2过去,1回来,5和10过去,2回来,1和2过去,一共2+1+10+2+2=17。

大致有两种策略,一种是最小的那个带大的过去,再回来带第二大的过去,再回来;另一种是最小的两个过去,最小的回来,然后最大的两个过去,第二小的回来。

好像可以贪心,这两种策略结束时都是最小的两个还在这边,最大的两个都去了那边,状态是一样的,所以这里选最优的策略就好。

不过我做的时候没想通,用了动规,i从大到小,dp[i][0]记录把i送过去时第二小的还在自己这边所耗的总时间,dp[i][1]记录把i送过去时第二小的在对面,所用的总时间。这样记录了第二小的位置,比较方便讨论几种送法。

动规代码如下:

 #include<iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<vector>
#include<algorithm>
#include<cstring>
#define MAXN 111
#define RE freopen("in.txt","r",stdin);
#define inf 6666666
using namespace std; int n;
int a[MAXN];
int dp[MAXN][]; int farm();
void init(); int main()
{
//RE
while(scanf("%d",&n)!=EOF)
{
farm();
}
return ;
} int farm()
{
int i,ans;
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
memset(dp,-,sizeof(dp));
for(i=;i<=n+;i++)
{
dp[i][]=inf;
dp[i][]=inf;
}
dp[n][]=;
dp[n][]=a[]+a[];
for(i=n-;i>;i--)
{
dp[i][]=dp[i+][]+a[i]+a[];
if(i<n-) dp[i][]=min(dp[i][],dp[i+][]+a[i+]+a[]);
dp[i][]=dp[i+][]+a[i]+a[];
dp[i][]=min(dp[i][],dp[i][]+a[]+a[]);
}
ans=min(dp[][]+a[],dp[][]+a[]);
printf("%d\n",ans);
return ;
} void init()
{ }

贪心的我没写…应该比这个简单。

URAL1826. Minefield 题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. Zabbix邮件报警-->Script

    Version:3.0.1 邮件报警有两种media 1.Email zabbix发送报警邮件到指定smtp服务器(使用系统自带的sendmail,发送邮箱是zabbix服务器的本地邮箱账号) 再由s ...

  2. jquery的$

    <!DOCTYPE HTML> <HTML> <head> <title>aa</title> <script type=" ...

  3. 【转载】MFC键盘响应

    转载自:传送门 一:首先介绍键盘消息 系统消息: ALT,F1,--F24等,是由系统内部处理的,程序本身就存在,比如F1是帮助键. WM_SYSKEYDOWN WM_SYSKEYUP WM_SYSC ...

  4. 【Alpha版本】 第七天 11.15

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成我要招聘的招聘详情显示,完成简历填写及显示功能 完成我要应聘的 ...

  5. Linux rsync网站目录同步功能的实现

    实现目标: 172.16.1.64服务器上的/var/www/sw_service目录,与172.16.1.60服务器上的/var/www/sw_service目录实现同步, 即1.60主动向1.64 ...

  6. UML的目标

    http://zhidao.baidu.com/link?url=ghQvzG70vSCSLyQcrHDTd7xt1aSWBR73lPIMxBCEPo1ktkq9cQ3EE9TXX1mZyHINkVA ...

  7. WinForm------GridControl中通过判断单元格文字显示不同字体颜色或背景色

  8. WinForm------RepositoryItemCheckEdit属性介绍

    //去掉第三种状态 editcheck1.OptionView.NullStyle = UnChecked

  9. jquery 解析数据库中的json日期为正常的格式

    //在action从后台数据库中请求获得日期以后,得到的是json格式的数据,因此要解析才能显示在前台1.在jsp页面写的代码如下:<html> <script> Date.p ...

  10. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【六】——实现资源间的关联

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 这一篇文章主要介绍一下资源间的关联——例如在学生和课程之间就存在这样的关联:每一个课程都会有 ...