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 & ...
随机推荐
- Restful api介绍
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
- sublime编写markdown文件中Ctrl+B的作用
今天,手残,用markdown编辑是按了Ctrl+B,结果发现直接在同一文件夹目录下编译生成了html文件,之前都是Alt+m,可以直接预览,可是后来由于系统更新还是什么,Alt+m只会在用户目录(而 ...
- UML的目标
http://zhidao.baidu.com/link?url=ghQvzG70vSCSLyQcrHDTd7xt1aSWBR73lPIMxBCEPo1ktkq9cQ3EE9TXX1mZyHINkVA ...
- uC/OS-II标志(flag)块
/*************************************************************************************************** ...
- mysql常用命令之-用户密码修改
--创建用户 CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1'; GRANT SELECT,INSERT,UPDATE,DELETE ON * ...
- tomcat7.0配置CORS(跨域资源共享)
平时我们做前台页面时可能会遇到浏览器以下提示(浏览器控制台): 已阻止跨源请求:同源策略禁止读取位于 http://xxx.xxx.com 的远程资源.(原因:CORS 头缺少 'Access-Con ...
- PHPCMS后台统计点击量时出现点击数不改变可能丢失了JS代码。
<script language="JavaScript" src="{APP_PATH}api.php?op=count&id={$id}&mod ...
- phpcms响应式布局导航条
- mysql中文坑爹的东西
1.首先pb需要utf8格式,所以由ansi转换成utf8格式才能传输 2.在接收端,需要将utf8转换成ansi才能用 3.在插入数据库之前需要将sql从ansi转换成utf8格式 4.数据库设置字 ...
- 最好的vim教程莫过于vimtutor
最好的vim教程莫过于vimtutor 直接运行vimtutor即可