原题:

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. 【BZOJ-3996】线性代数 最小割-最大流

    3996: [TJOI2015]线性代数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1054  Solved: 684[Submit][Statu ...

  2. 雷赛dmc2410控制卡,驱动器 光栅 加电机

    一般驱动器为9pin接口:(1:VCC 2:限位近端 3:限位远端 4:GND):电源是供给限位器的 ,5:A+ 6:A- 7:B+ 8:B- 注意驱动器的电流大小和细分设置,电流一般1A左右 电流过 ...

  3. 数据结构算法C语言实现---序言

    期末考试即将到来,打算花两周时间实现书上所有的算法.巩固学习成果(其实之前也没怎么听课......)毕竟考前突击,背背,ppt刷个90+是没多大意义的. 没错,就是下面这本 毕竟书也是借别人的,不抓紧 ...

  4. [iOS 主要框架的总结]

    原文地址:http://blog.csdn.net/GooHong/article/details/28911301 在iOS中框架是一个目录,包含了共享资源库,用于访问该资源库中储存的代码的头文件, ...

  5. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  6. JS+JavaBean判断管理员增删改的操作权限

    目标:用户分管理员和普通用户2种,都可以登陆,但是管理员才可以执行增删改的权限,普通用户可以看,但是执行的时候提示权限不足 帖代码片段:我只会这一种,在JSP页面判断 省略得权限数值方法: <% ...

  7. WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题

    最近在学习WPF过程中使用到了ListBox控件,在使用时遇到下面的奇怪问题: 代码如下: listBox.Items.Add("绘图"); listBox.Items.Add(& ...

  8. 网站缓存数据到tomcat服务器

    通过缓存使相同的数据不用重复加载,降低数据库的访问 public class CacheFilter implements Filter { //实例变量[每线程共享] private Map< ...

  9. 注释(Annotation)

    J2SE 5.0提供了很多新的特征.其中一个很重要的特征就是对元数据(Metadata)的支持.在J2SE 5.0中,这种元数据称为注释(Annotation).通过使用注释,程序开发人员可以在不改变 ...

  10. JavaScript----遇到的问题

    <script type="text/javascript" src="a.js"> alert('dd'); </script> 存在 ...