URAL1826. Minefield 题解
原题:
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 题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- 【UOJ#33】【UR#2】树上GCD 有根树点分治 + 容斥原理 + 分块
#33. [UR #2]树上GCD 有一棵$n$个结点的有根树$T$.结点编号为$1…n$,其中根结点为$1$. 树上每条边的长度为$1$.我们用$d(x,y)$表示结点$x,y$在树上的距离,$LC ...
- bzoj1121: [POI2008]激光发射器SZK
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- [NOIP2015] 普及组
金币 模拟 #include<iostream> #include<cstdio> using namespace std; int main(){ int k,n; scan ...
- dataGrid查询表格
waf("#Gird").dataGrid("reloadGrid") //重新发请求去装载数据 waf("#Grid").wafGrid( ...
- C#点击按钮关闭当前窗体 打开另一个窗体。
网上有很多是隐藏当前窗体,但是这样占用资源,效果不好,因此改进方法如下: private void button1_Click(object sender,EventArgs e) { this.hi ...
- Mac 配置 vim
Mac 配置 vim 安装 vim 7.4 cd ~ hg clone https://code.google.com/p/vim/ cd vim ./configure --with-feature ...
- HDU #2966 In case of failure
Overview 给出平面上两两不重合的\(n\)个整点, 求每个点到它在其他\(n-1\)个点的最近临点的欧几里得距离的平方. Solution k-d tree 模板题. 关于k-d tree, ...
- 如何写出优雅的css代码 ?
如何写出优雅的css代码 ? 对于同样的项目或者是一个网页,尽管最终每个前端开发工程师都可以实现相同的效果,但是他们所写的代码一定是不同的.有的优雅,看起来清晰易懂,代码具有可拓展性,这样的代码有利于 ...
- IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较
//代码块与代替代理的设计方法 我就以在自定义视图中(非视图控制器,不能实现视图控制功能),通过代理和代码块两种方法分别实现视图的跳转,进行对比 首先自定义了一个视图,上面有一个已经注册了得BUtto ...
- sql lock
要提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑.其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善在线大量查询 ...