URAL - 1826 Minefield

Minefield

Time Limit: 500MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

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.

Sample Input

input output
4
1
10
5
2
17

题目讲n个士兵过雷区,可就一个探测器,每次最多两个人一起走,还要有一个人把探测器拿回了,一直到全部人渡过雷区。一起走时即较慢的那个人的用时,求所用最短的时间

先讲下例子的走法,1和2过去(2),2回来(2+2),5和10过去(2+2+10),1回来(2+2+10+1),1和2再回去(2+2+10+1+2)。脑子笨这个例子就想了好久。

先排序,然后可能得最优解法有两种:

1.最小两个过去,最小的一个把探测器带回来,再带最大的过去,再带第二大的过去……

2.最小的两个过去,最小的回来,最大两个过去,第二小的回来……

要注意下结束条件,然后就交给递归吧。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<utility>
#include<vector>
using namespace std;
#define eps 1e-6
#define inf 0x3f3f3f3f
int a[1010];
int count(int n)
{
	if(n==2)
	{
		return a[1];
	}
	if(n==3)
	{
		return a[0]+a[1]+a[2];
	}
	if(2*a[0]+a[n-1]+a[n-2]<a[0]+a[1]+a[n-1]+a[1])
	{
		return 2*a[0]+a[n-1]+a[n-2]+count(n-2);
	}
	else
	{
		return a[0]+a[1]+a[n-1]+a[1]+count(n-2);
	}
}
int main()
{
	int n;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",a+i);
		}
		sort(a,a+n);
		cout<<count(n)<<endl;
	}
	return 0;
}
时间: 2024-10-02 06:01:03

URAL - 1826 Minefield的相关文章

URAL 1826. Minefield 贪心

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

URAL 1826. 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 detecto

URAL1826. Minefield 题解

原题: http://acm.timus.ru/problem.aspx?space=1&num=1826 1826. MinefieldTime limit: 0.5 secondMemory limit: 64 MBTo fulfill an assignment, a reconnaissance group of n people must cross the enemy's minefield. Since the group has only one mine detector, t

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn