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 & ...
随机推荐
- 【BZOJ-1391】order 最小割 + 最大全闭合图
1391: [Ceoi2008]order Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1334 Solved: 405[Submit][Statu ...
- 【BZOJ-1941】Hide and Seek KD-Tree
1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 830 Solved: 455[Submi ...
- 【BZOJ-1911】特别行动队 DP + 斜率优化
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3478 Solved: 1586[Submit][Statu ...
- BLOG搬家
前段时间一直用的新浪blog,发现推送信息是否杂乱,所以特地投奔学长CSDN,外加之前颓废心过重,现在打算新面貌认真学习
- kali 2.0 启动metasploit服务
kali 2.0 已经没有metasploit 这个服务了,所以service metasploit start 的方式不起作用. 在kali 2.0中启动带数据库支持的MSF方式如下: 首先启动po ...
- 【bzoj2038】 小Z的袜子(hose)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 (题目链接) 转自:http://blog.csdn.net/bossup/article/d ...
- C++开发的数据库连接查询修改小工具
项目相关地址 源码:https://github.com/easonjim/SQL_Table_Tool bug提交:https://github.com/easonjim/SQL_Table_Too ...
- 一款可以下拉搜索html下拉框控件
直接上图,组件不错,支持静态和动态搜索,这个只是在原控件上自己修改样式后的,这里主要记录一下,在修改别人控件时,应该如何去封装代码: 原控件:http://ivaynberg.github.com/s ...
- 关于AWR报告命中率指标的解释(转)
文章转自:http://blog.itpub.net/24558279/viewspace-762371/ 从Oracle 10g开始,Oracle给广大DBA提供了一个性能优化的利器,那便是Auto ...
- 强大的windbg定位内存泄露,两句命令搞定!
1.简单配置在windbg程序目录下有个gflags.exe,运行后设置: 运行CMD.EXE,输入"D:\Debugging Tools for Windows (x86)\gflags. ...