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
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 output
4
1
10
5
2
17

题目:有n个人在雷区的左边。他们各自有自己跨越雷区的所需时间。他们只有一个扫雷器。为了能安全通过,他们只能两个人或者一个人带着扫雷器在雷区里走。两个人走过雷区的时间,以时间长的算。因为只有一个扫雷器,如果左边还有人,右边的人还要把扫雷器送回到左边。

注意开始只有两个人的时候的特判。

走法:当人数大于不等于四个人的时候。 有两种走法。 挑出 走的最快的a,第二快的b。然后再从 最慢的 z,和第二慢的 y开始。 原则  是a,b来送z和y过去,但是a,b 要回到左边。这里有两种走法,一种只用到a,  a和z一起过去,再a一个人回来,再a和y一起过去,再a回来。耗时是a*2+y+z。 第二种走法是,a和b过去,a一个人回来,y和z一起过去,b一个人把扫雷器带回来。 耗时是 b*2+a+z。

再计算 三个人和四个人的情况就好了。见代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <vector> 

int main()
{
	int n;
	int arr[110];
	while(scanf("%d",&n)!=EOF)
	{

		for(int i=0;i<n;i++)
			cin>>arr[i];

		sort(arr,arr+n);
		if(n==2)
		{
			cout<<arr[1]<<endl;
			continue;
		}
		int j=n-1;
		int ans=0;
		for(;j>=4;j-=2)
		{
			ans+=min(arr[0]*2+arr[j]+arr[j-1],arr[0]+arr[1]*2+arr[j]);//四个人以上的情况
		}
		if(n&1)
			ans+=arr[0]+arr[j]+arr[j-1];//最后三个人
		else
			ans+=min(arr[0]*2+arr[1]+arr[j]+arr[j-1],arr[0]+arr[1]*3+arr[j]);//最后四个人
		cout<<ans<<endl;
	}
	return 0;
}
时间: 2025-01-01 17:20:20

URAL 1826. Minefield 贪心的相关文章

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

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,

URAL 2092 Bolero 贪心

C - Bolero Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2092 Description Winter in Yekaterinburg is the longest time of the year. And everyone spends long winter evenings in his own way. Den

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 1203 Scientific Conference(贪心 || DP)

Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校,补多校的题目,刷一下一直薄弱的DP.多校如果有计算几何一定要干掉-.- 题意:给你N个报告会的开始时间跟结束时间,问你做多可以听几场报告会.要求报告会之间至少间隔为1. 思路:其实是个活动安排问题,可以用贪心也可以用DP,贪心写起来会比较简单一些,因为练习DP,所以又用DP写了一遍. 贪心的话就是一个很简单的活动选择问题,从结束时间入手,找每次的最优选择. 1 struct n

URAL 2000. Grand Theft Array V(贪心啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2000 2000. Grand Theft Array V Time limit: 0.5 second Memory limit: 64 MB A long anticipated game called Grand Theft Array V is about to appear in shops! What, haven't you heard of it? Then we must te

URAL 2026 Dean and Schedule 贪心、双端队列(deque)、队列(queue)

C - Dean and Schedule Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2026 Description A new academic year approaches, and the dean must make a schedule of classes for first-year students. Ther

URAL 1025. Democracy in Danger (贪心)

1025. Democracy in Danger Time limit: 1.0 second Memory limit: 64 MB Background In one of the countries of Caribbean basin all decisions were accepted by the simple majority of votes at the general meeting of citizens (fortunately, there were no lots

贪心 URAL 1303 Minimal Coverage

题目传送门 1 /* 2 题意:最少需要多少条线段能覆盖[0, m]的长度 3 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 4 接着就是从p=0开始,以p点为标志,选取 (node[i].l <= p && p < node[i+1].l) 5 详细解释:http://www.cnblogs.com/freezhan/p/3219046.html 6 */ 7 #include <cstdio> 8 #include <iostream>