1.4.2 USACO Barn Repair(貪心)

Barn Repair

It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full.

The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width.

Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.

Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.

Print your answer as the total number of stalls blocked.

PROGRAM NAME: barn1

INPUT FORMAT

Line 1: M, S, and C (space separated)
Lines 2-C+1: Each line contains one integer, the number of an occupied stall.

SAMPLE INPUT (file barn1.in)

4 50 18
3
4
6
8
14
15
16
17
21
25
26
27
30
31
40
41
42
43

OUTPUT FORMAT

A single line with one integer that represents the total number of stalls blocked.

SAMPLE OUTPUT (file barn1.out)

25

[One minimum arrangement is one board covering stalls 3-8, one covering 14-21, one covering 25-31, and one covering 40-43.] 

翻譯:修理牛棚

題目描述

在一個月黑風高的暴風雨夜,Farmer John 的牛棚的屋頂、門被吹飛了 好在許多牛正在度假,所以牛棚沒有住滿。

牛棚一個緊挨着另一個被排成一行,牛就住在裏面過夜。有些牛棚裏有牛,有些沒有。 所有的牛棚有相同的寬度。

自門遺失以後,Farmer John 必須儘快在牛棚之前豎立起新的木板。他的新木材供應商將會供應他任何他想要的長度,但是吝嗇的供應商只能提供有限數目的木板。 Farmer John 想將他購買的木板總長度減到最少。

給出 m,s,cm,s,c,表示木板最大的數目、牛棚的總數、牛的總數;以及每頭牛所在牛棚的編號,請算出攔住所有有牛的牛棚所需木板的最小總長度。

輸入格式

一行三個整數 m,s,cm,s,c,意義如題目描述。
接下來 cc 行,每行包含一個整數,表示牛所佔的牛棚的編號。

輸出格式

輸出一行一個整數,表示所需木板的最小總長度。

輸入輸出樣例

輸入 #1複製

4 50 18
3 
4 
6 
8 
14
15 
16 
17 
21
25 
26 
27 
30 
31 
40 
41 
42 
43

輸出 #1複製

25

說明/提示

【數據範圍】
對於 100\%100% 的數據,1\le m \le 501≤m≤50,1\le c \le s \le 2001≤c≤s≤200。

USACO Training Section 1.3

解析:

(一)木板數多於所佔牛棚數

1 2 3 4 5

假設有4塊木板,此時最小的總長度是每個牛棚佔據一個木板,最小總長度ans = 所佔牛棚數c 

(二)木板數少於所佔牛棚數

思路:可以從假設只有一個木板結果是什麼,然後假設有兩個木板,假設有3個木板。

其實可以轉化爲在白色空格處分割m-1次的問題。

1. 假設只有一個木板。

ans = a[c]-a[1]+1;

2. 假設有兩個木板

只需要從空白處隔開。

怎麼選擇空白處呢? (貪心)空白處最多的地方隔開,此時ans最小

因此,需要計算空白處的間隔,然後將空白處的間隔從大到小排序,然後ans每分割一次,減去最大的間隔即可。 

 

/*
ID: L
PROG: barn1 
LANG: C++ 
*/ 
#include<bits/stdc++.h> 
using namespace std;
int a[205],b[205];
bool cmp(int x,int y)
{
	return x > y;
}
int main()
{
	freopen("barn1.in","r",stdin);
	freopen("barn1.out","w",stdout);
	int m,s,c;
	cin >> m >> s >> c;
	for(int i = 1; i <= c; ++i)
		cin >> a[i];
	if(m >= c)//特判,當木板數大於所佔牛棚數 
	{
		cout << c << endl;
		return 0;
	 } 
	sort(a+1,a+1+c);//牛棚編號從小到大排序,題目不一定是從小到大排序的 
	int ans = a[c]-a[1]+1;//ans:所需木板最小總長度,初始條件假設只有一個木板 
	for(int i = 1; i < c; ++i)//間隔的空格數 
		b[i] = a[i+1] - a[i];
	sort(b+1,b+c,cmp);//間隔空格數從大到小排序 
	for(int i = 1; i <= m-1; ++i)//分割m-1次,每次從最大間隙中分割 
		ans = ans - b[i] + 1;	
	cout << ans << endl;
	fclose(stdin);
	fclose(stdout);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章