申請SAE

如果您發現本博客的外觀很難看,那是因為部分外觀文件被中國.國家.防火.牆屏.蔽所致!
請翻~牆!

我的Wordpress博客的地址: http://zhuyf.tk/
顯示具有 USACO 標籤的文章。 顯示所有文章
顯示具有 USACO 標籤的文章。 顯示所有文章

2012年3月3日 星期六

USACO Feb12 Bronze Rope Folding 折繩子 題解


USACO 2012 February Contest, Bronze Division

Problem 1. Rope Folding




Problem 1: Rope Folding [Brian Dean, 2012] 
Farmer John has a long rope of length L (1 <= L <= 10,000) that he uses for various tasks around his farm. The rope has N knots tied into it at various distinct locations (1 <= N <= 100), including one knot at each of its two endpoints. 

FJ notices that there are certain locations at which he can fold the rope back on itself such that the knots on opposite strands all line up exactly with each-other:
  
Please help FJ count the number of folding points having this property. Folding exactly at a knot is allowed, except folding at one of the endpoints is not allowed, and extra knots on the longer side of a fold are not a problem (that is, knots only need to line up in the areas where there are two strands opposite each-other). FJ only considers making a single fold at a time; he fortunately never makes multiple folds. 

PROBLEM NAME: folding
INPUT FORMAT:
 * Line 1: Two space-separated integers, N and L.
 * Lines 2..1+N: Each line contains an integer in the range 0...L specifying the location of a single knot. Two of these lines will always be 0 and L. 

2012年2月18日 星期六

[計算幾何]USACO Feb08 USACO Silver Game of Lines 連線遊戲

Title: 連線遊戲
Input: lines.in
Output: lines.out
Time Limit: 1000 ms
Memory Limit: 16 MB
Level: ★☆
Farmer John最近發明瞭一個遊戲,來考驗自命不凡的貝茜。遊戲開始的時候,FJ會給貝茜一塊畫著N (2 <= N <= 200)個不重合的點的木板,其中第i個點的橫、縱座標分別為X_i和Y_i (-1,000 <= X_i <=1,000;-1,000 <= Y_i <= 1,000)。
貝茜可以選兩個點畫一條過它們的直線,當且僅當平面上不存在與畫出直線平行的直線。遊戲結束時貝茜的得分,就是她畫出的直線的總條數。為了在遊戲中勝出,貝茜找到了你,希望你幫她計算一下最大可能得分。
程序名: lines

2012年2月16日 星期四

[多源最短路]USACO 奶牛聚會 spart

譯: zqzas
N(1 ≤ N ≤ 1000)個農場中的每個農場都有一隻奶牛去參加位於第X個農場的聚會.共有M (1 ≤ M ≤ 100,000)條單向的道路,每條道路連接一對農場.通過道路i會花費Ti (1 ≤ Ti ≤ 100)的時間.
作爲參加聚會的奶牛必須走到聚會的所在地(農場X).當聚會結束時,還要返回各自的農場.奶牛都是很懶的,她們想找出花費時間最少的路線.由於道路都是單向的,所有她們前往農場X的路線可能會不同於返程的路線.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back? 對於所有參加聚會的奶牛,找出前往聚會和返程花費總時間最多的奶牛,輸出這隻奶牛花費的總時間.

2012年2月11日 星期六

USACO Contest Feb2012 Bronze Moo 翻譯+題解

翻譯(質量不高,將就看吧):
USACO Contest Feb2012 Bronze
Problem 3. Moo

奶牛們迷上了一個名為“Moo”的新的單詞遊戲。
在玩該遊戲時,奶牛們站成長長的一排,在隊列中的每一頭
奶牛都有責任盡可能快的大聲說出一個特定的字母。

在Moo遊戲中,這個單詞序列嚴格上說是無窮的,它是這樣開始的:
m o o m o o o m o o m o o o o m o o m o o o m o o m o o o o o

這一串最好由遞歸表示:令S(0)為三個字符的序列“moo”
那麼更長的字符串S(k)由三部分組成,第一部分是S(k-1),第二部分是"m o...o"(k+2個'o'),第三部分又是S(k-1)。例如:
S(0)="m o o"
S(1)="m o o m o o o m o o"
S(2)="m o o m o o o m o o m o o o o m o o m o o o m o o"

正如你所看到的,這個過程最終將會產生一個無窮的長字符串,並且
這個長字符串正是被玩Moo遊戲的奶牛一個一個說出。

Bessie這頭奶牛,自我感覺很聰明,他想要預測第N頭奶牛將會說出m還是o。請你幫助他!

2012年2月10日 星期五

[記憶化搜索]USACO Jan09 Silver Laserphones 激光電話



**********************************************************************

Problem 8: Laserphones [Rob Kolstad, 2008]

The cows have a new laser-based system so they can have casual
conversations while out in the pasture which is modeled as a W x H
grid of points (1 <= W <= 100; 1 <= H <= 100).

The system requires a sort of line-of-sight connectivity in order
to sustain communication. The pasture, of course, has rocks and
trees that disrupt the communication but the cows have purchased
diagonal mirrors ('/' and '\' below) that deflect the laser beam
through a 90 degree turn. Below is a map that illustrates the
problem.

2012年2月8日 星期三

[搜索]USACO Open05 疾病管理 disease 解題報告

Title: 疾病管理
Input: disease.in
Output: disease.out
Time Limit: 1000 ms
Memory Limit: 128 MB
Level: ★☆


【問題描述】
天啊,真是不幸!最近在農夫 John 的農場上有 D(1<=D<=15) 種疾病 ( 疾病的編號為 1..D) 在奶牛當中流行。 John 想要給他的 N(1<=N<=1000) 頭奶牛擠牛奶。擠出來的牛奶都被放在一個罐子裡面。如果這些牛奶中包含了超過 K(1<=K<=D) 種的疾病,那麼這些牛奶就要全部被丟棄掉了(浪費啊 -_-! )。 John 應該給這 N 頭奶牛當中的哪些奶牛擠奶,才能使得牛奶不被丟棄,並且擠牛奶的數量最多呢?

USACO Dec07 泥潭 mud 解題報告

Title: 泥潭
Input: mud.in
Output: mud.out
Time Limit: 1000 ms
Memory Limit: 128 MB
Level: ★☆
譯 by CmYkRgB123
描述
Farmer John在早晨6點準時去給貝茜擠奶,然而昨天晚上下了大雨,他的牧場變得泥濘不堪了。Farmer John的家在座標平面的 (0,0) 處,貝茜在 (X, Y) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500)。他看見了所有的 N (1 ≤ N ≤ 10,000) 個泥潭,分別在 (Ai, Bi) (-500 ≤ Ai ≤ 500; -500 ≤ Bi ≤ 500) 。每個泥潭只佔一個點的位置。
Farmer John 剛剛買了新的靴子,他絕對不想把靴子踩進泥潭弄髒,而他又想儘快的找到貝茜。他已經快晚了,因爲他花了大量的時間來找到所有的泥潭的位置。 Farmer John 只能平行於座標軸移動,每次移動一個單位。請你幫助 Farmer John 找到一條路,使得 Farmer John 能夠最快的找到貝茜,而且不會弄髒靴子。我們約定一定存在一條路使 Farmer John 找到貝茜。

2012年2月6日 星期一

USACO Oct07 Silver 障礙訓練場 obstacle 解題報告

Title: 障礙訓練場
Input: obstacle.in
Output: obstacle.out
Time Limit: 1000 ms
Memory Limit: 128 MB
Level: ★☆
譯 By CmYkRgB123
考慮一個 N x N (1 <= N <= 100)的有1個個方格組成的正方形牧場。有些方格是奶牛們不能踏上的,它們被標記爲了'x'。例如下圖:
        . . B x .
        . x x A .
        . . . x .
        . x . . .
        . . x . .
貝茜發現自己恰好在點A出,她想去B處的鹽塊添鹽。緩慢而且笨拙的動物,比如奶牛,十分討厭轉彎。儘管如此,當然在必要的時候她們還是會轉彎的。對於一個給定的牧場,請你計算從A到B最少的轉彎次數。開始的時候,貝茜可以使面對任意一個方向。貝茜知道她一定可以到達。

2012年2月5日 星期日

USACO 2009 9th 熱浪 heatwv 解題報告

USACO/heatwv

第九題: 熱浪 [300分] [Rob Kolstad (傳統題目), 2009]
德克薩斯純樸的民眾們這個夏天正在遭受巨大的熱浪!!!他們的德克薩斯長角牛吃起來不錯, 可是他們並不是很擅長生產富含奶油的乳製品。Farmer John此時以先天下之憂而憂,後天下 之樂而樂的精神,身先士卒地承擔起向德克薩斯運送大量的營養冰涼的牛奶的重任,以減輕德 克薩斯人忍受酷暑的痛苦。
FJ已經研究過可以把牛奶從威斯康星運送到德克薩斯州的路線。這些路線包括起始點和終點先 一共經過T (1 <= T <= 2,500)個城鎮,方便地標號為1到T。除了起點和終點外地每個城鎮 由兩條雙向道路連向至少兩個其它地城鎮。每條道路有一個通過費用(包括油費,過路費等等)。 考慮這個有7個城鎮的地圖。城鎮5是奶源,城鎮4是終點(括號內的數字是道路的通過費用)。

2012年2月2日 星期四

USACO Feb07 奶牛詞典 Cow Lexicon

Title: 奶牛詞典
Input: lexicon.in
Output: lexicon.out
Time Limit: 1000 ms
Memory Limit: 128 MB
Level: ★★
譯: zqzas

題目描述:

沒有幾個人知道,奶牛有她們自己的字典,裡面的有W (1 ≤ W ≤ 600)個詞,每個詞的長度不超過25,且由小寫字母組成.她們在交流時,由於各種原因,用詞總是不那麼準確.比如,貝茜聽到有人對她 說"browndcodw",確切的意思是"browncow",多出了兩個"d",這兩個"d"大概是身邊的噪音.

奶牛們發覺辨認那些奇怪的資訊很費勁,所以她們就想讓你幫忙辨認一條收到的消息,即一個只包含小寫字母且長度為L (2 ≤ L ≤ 300)的字元串.有些時候,這個字元串裡會有多餘的字母,你的任務就是找出最少去掉幾個字母就可以使這個字元串變成準確的"牛語"(即奶牛字典中某些詞 的一個排列).



2012年1月28日 星期六

[最短路徑][二分答案]USACO Jan08 Silver 架設電話線 phoneline 解題報告

Title: 架設電話線【試題傳送門
Input: phoneline.in
Output: phoneline.out
Time Limit: 1000 ms
Memory Limit: 16 MB
Level: ★★☆
Farmer John打算將電話線引到自己的農場,但電信公司並不打算為他提供免費服務。於是,FJ必須為此向電信公司支付一定的費用。
FJ的農場周圍分佈著N(1 <= N <= 1,000)根按1..N順次編號的廢棄的電話線杆,任意兩根電話線杆間都沒有電話線相連。一共P(1 <= P <= 10,000)對電話線杆間可以拉電話線,其餘的那些由於隔得太遠而無法被連接。
第i對電話線杆的兩個端點分別為A_i、B_i,它們間的距離為L_i (1 <= L_i <= 1,000,000)。資料中保證每對{A_i,B_i}最多隻出現1次。編號為1的電話線杆已經接入了全國的電話網絡,整個農場的電話線全都連到了編號 為N的電話線杆上。也就是說,FJ的任務僅僅是找一條將1號和N號電話線杆連起來的路徑,其餘的電話線杆並不一定要連入電話網絡。
經過談判,電信公司最終同意免費為FJ連結K(0 <= K < N)對由FJ指定的電話線杆。對於此外的那些電話線,FJ需要為它們付的費用,等於其中最長的電話線的長度(每根電話線僅連結一對電話線杆)。如果需要連 結的電話線杆不超過K對,那麼FJ的總支出為0。
請你計算一下,FJ最少需要在電話線上花多少錢。
程序名: phoneline

USACO Jan08 Bronze 化裝晚會 costume 解題報告

Title: 化装晚会【題目連結
Input: costume.in
Output: costume.out
Time Limit: 1000 ms
Memory Limit: 16 MB
Level: ★☆
萬聖節又到了!Farmer John打算帶他的奶牛去參加一個化裝晚會,但是,FJ只做了一套能容 下兩頭總長不超過S(1 <= S <= 1,000,000)的牛的恐怖服裝。FJ養了N(2 <= N <= 20,000)頭按1..N順序編號的奶牛,編號為i的奶牛的長度為L_i(1 <= L_i <= 1,000,000)。如果兩頭奶牛的總長度不超過S,那麼她們就能穿下這套服裝。
FJ想知道,如果他想選擇兩頭不同的奶牛來穿這套衣服,一共有多少種滿足條件的方案。
程序名: costume

2012年1月25日 星期三

[搜索]USACO Feb08 Silver 流星雨 meteor 解題報告

題目連結:http://cogs.yeefanblog.tk/t/138
Title: 流星雨
Input: meteor.in
Output: meteor.out
Time Limit: 1000 ms
Memory Limit: 128 MB
Level: ★☆
貝茜聽說了一個駭人聽聞的消息:一場流星雨即將襲擊整個農場,由於流星體積過大它們無法在撞擊到地面前燃燒殆盡,屆時將會對它撞到的一切東西造成毀 滅性的打擊。很自然地,貝茜開始擔心自己的安全問題。以FJ牧場中最聰明的奶牛的名譽起誓,她一定要在被流星砸到前,到達一個安全的地方(也就是說,一塊 不會被任何流星砸到的土地)。如果將牧場放入一個直角座標系中,貝茜現在的位置是原點,並且,貝茜不能踏上一塊被流星砸過的土地。
根據預報,一共有M顆流星(1 <= M <= 50,000)會墜落在農場上,其中第i顆流星會在時刻T_i (0 <= T_i <= 1,000)砸在座標為(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300)的格子裡。流星的力量會將它所在的格子,以及周圍4個相鄰的格子都化為焦土,當然貝茜也無法再在這些格子上行走。
貝茜在時刻0開始行動,它只能在第一象限中,平行於座標軸行動,每1個時刻中,她能移動到相鄰的(一般是4個)格子中的任意一個,當然目標格子要沒有被燒焦才行。如果一個格子在時刻t被流星撞擊或燒焦,那麼貝茜只能在t之前的時刻在這個格子裡出現。
請你計算一下,貝茜最少需要多少時間才能到達一個安全的格子。
程序名: meteor

2012年1月14日 星期六

[最大流]USACO 2002 Winter Green:New Years Party 解題報告

題目鏈接:http://oj.jzxx.net/problem.php?id=1674
代碼發芽網代碼高亮:http://fayaa.com/code/view/25178/

题目描述

A group of N (3 <= N <= 200) cows is having a New Year's party. Each cow is able to cook several different kinds of food (in units called a "dish"). There are a total of D (5 <= D <= 100) different kinds of food. Each kind of food is denoted by an integer in the range 1..D. The cowrdinator wants to maximize the total number of dishes brought to the party, but has specified a limit for the number of dishes of each type. Each cow can bring K (1 <= K <= 5) dishes, but they must be different from each other (she can't bring 3 bovine pies, for example, but she could bring a pie, some bread, and some nice alfalfa in orange sauce). What is the maximum amount of food that can be brought?

USACO Dec11 Hay Bales 原題、翻譯、題解

【英文原題】
Problem 1: Hay Bales [Brian Dean, 2011]

The cows are at it again!  Farmer John has carefully arranged N (1 <= N <=
10,000) piles of hay bales, each of the same height.  When he isn't
looking, however, the cows move some of the hay bales between piles, so
their heights are no longer necessarily the same.  Given the new heights of
all the piles, please help Farmer John determine the minimum number of hay
bales he needs to move in order to restore all the piles to their original,
equal heights.

PROBLEM NAME: haybales

INPUT FORMAT:

* Line 1: The number of piles, N (1 <= N <= 10,000).

* Lines 2..1+N: Each line contains the number of hay bales in a single
        pile (an integer in the range 1...10,000).

SAMPLE INPUT (file haybales.in):

4
2
10
7
1

INPUT DETAILS:

There are 4 piles, of heights 2, 10, 7, and 1.

OUTPUT FORMAT:

* Line 1: An integer giving the minimum number of hay bales that need
        to be moved to restore the piles to having equal heights.

SAMPLE OUTPUT (file haybales.out):

7

OUTPUT DETAILS:

By moving 7 hay bales (3 from pile 2 to pile 1, 2 from pile 2 to pile 4, 2
from pile 3 to pile 4), we can make all piles have height 5.

【中文翻譯】
USACO美國信息學月賽 2011年12月賽  銅組 
Problem 1: 乾草堆

奶牛們又來了!農夫約翰成功地準備了N(1<=N<=10,000)捆乾草,每捆乾草都有相同的高度。然而,當他不注意的時候

,他的奶牛們在兩捆乾草之間移動了一部分乾草,所以這些乾草的數量不再和以前一樣了。

給你每捆乾草新的數量,請幫助農夫約翰計算出他最少需要移動多少乾草,使每捆都恢復到初始時的數量,也就是那個

相同的數量。

程序名稱:haybales

輸入格式:

*第1行:乾草的捆數N(1<=N<=10,000)。

*第2~1+N行,每行包含一個整數,表示第i捆乾草的數量,(一個1、10,000之間的整數)。

輸入樣例(file haybales.in):

4
2
10
7
1

輸入解釋:

一共有4捆乾草,它們的高度分別是2,10,7和1。

輸出格式:

* 第一行:一個整數,表示至少需要移動多少乾草,
才能使每捆乾草恢復到相等的高度。

輸出樣例(file haybales.out):

7

輸出解釋:
移動7份乾草(把3份乾草從第2捆移至第一捆,把2份乾草從第2捆移至第4捆,把2份乾草從第3捆移至第4捆),我們就

可以把每捆乾草的高度都變成5。
 【分析】

 貪心就可以了,也是【NOIP2002提高組 均分紙牌】的這種類型。
先統計出平均數,再計算出 每捆乾草數量與平均數的差值(的絕對值)之和,然後除以2即可。

時間複雜度:O(N*2)
空間複雜度:N

【我的代碼】



1  /*
2  ID:yeefan
3  LANG:C++
4  PROB:haybales
5  */
6  #include <iostream>
7  #include <cstdio>
8  #include <cstdlib>
9  using namespace std;
10  int N;
11  int A[10001];
12  long long Sum=0;
13  long long Avg=0;
14  
15  int abs(int x)
16  {
17   if(x<0)
18   x=-x;
19   return x;
20  }
21  
22  void init()
23  {
24   scanf("%d\n",&N);
25   for (int i=1;i<=N;i++)
26   {
27   scanf("%d\n",&A[i]);
28   Avg+=A[i];
29   }
30   Avg/=N;
31  }
32  
33  void work()
34  {
35   for (int i=1;i<=N;i++)
36   {
37   Sum+=abs(A[i]-Avg);
38   }
39   cout<<Sum/2<<endl;
40  }
41  
42  int main()
43  {
44   freopen("haybales.in","r",stdin);
45   freopen("haybales.out","w",stdout);
46   init();
47   work();
48   return 0;
49  }

2011年12月19日 星期一

[最短路徑]USACO Silver09 找工作 jobhunt 解題報告

問題描述:
貝茜牛身無分文了,她正忙着找工作。農夫約翰知道這個情況,他想讓他的牛去周遊世界,於是他推行了一個規則:在他的牛到另 一個城市工作之前,她們只能在一個城市掙得 D ( 1 <= D <= 1,000 )美元。不管怎樣,貝茜可以在別的城市工作過之後,再返回到某個城市,並在這個城市再掙 D 美元,她可以無限次數地這樣做。
貝茜牛的世界包括 P ( 1 <= P <= 150 )條單向邊,這些邊連接着 C ( 2 <= C <= 220 )個城市,城市按 1 到 C 的順序編號,貝茜牛目前正待在 S 城 (1 <= S <= C) 。單向邊 i 從城市 A_i 連到城市 B_i ,其中 1 <= A_i <= C; 1 <= B_i <= C ,在路上不花費任何代價。
爲了幫助貝茜,約翰授權它使用他的私人噴氣飛機服務。這項服務配置了 F 條航綫,每條航綫是由城市 J_i 到城市 K_i (1 <=J_i <= C; 1 <= K_i <= C) 的單向航綫,且在該航綫上的費用是 T_i( 1 <= T_i <= 50,000 ) 美元,如果貝茜牛手頭沒有現錢,它可以將來掙到錢之後再支付飛行費用。
只要它願意,貝茜可以隨時隨地選擇退出。不限時間,假定它所有去過的城市都能掙足 D 美元,最後貝茜最多能得到多少錢?如果這個數目沒有限制的話輸出 -1 。
程序名:jobhunt
輸入格式:
第1行:五個空格隔開的整數,D,P,C,F,S;
第2至P+1行:第i行包括兩個空格隔開的整數,表示從城市A_i到B_i有一條單向邊。
第P+2至P+F+1行:第P+i行包括三個空格隔開的整數,表示從城市J_i到T_i有一條單向航綫,費用是T_i。
輸入樣例:(jobhunt.in):
100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120
輸入樣例解釋:這個世界有5個城市,三條有向邊,和兩條飛行航綫,貝茜從城市1開始,在每個城市它能最多掙到100美元。
輸出格式:
只有一行,一個整數,表示在遵守規則的情況下,它最多能得到多少錢。
輸出樣例:(jobhunt.out):
250
輸出樣例解釋:貝茜能從城市1→城市5→城市2→城市3,最後共得到4*100 - 150 = 250美元。
「分析」
單源最短路問題,賺錢是負權,航費是正權,用SPFA處理負邊權即可。
「我的代碼」
#include "cstdio"
#include "iostream"
#include "cstdlib"
#include "queue"
using namespace std;
const int MAX=230;
int Map[MAX][MAX];
int dist[MAX];
int times[MAX];
bool flag[MAX];
const int MAXN=1000000000;
int D;//在每個城市最多掙得D美金
int P;//P條單向邊
int C;//C個城市
int F;//F個單項航線
int S;//源點
typedef queue QUEUE;
void init()
{
 scanf("%d %d %d %d %d\n",&D,&P,&C,&F,&S);
 for (int i=1;i<=C;i++)
  for (int j=1;j<=C;j++)
   Map[i][j]=MAXN;
 for (int i=1;i<=C;i++)
  Map[i][i]=0;
 for (int i=1;i<=P;i++)
 {
  int a,b;
  scanf("%d %d\n",&a,&b);
  Map[a][b]=-D;
 }
 for (int i=1;i<=F;i++)
 {
  int a,b,c;
  scanf("%d %d %d\n",&a,&b,&c);
  if(Map[a][b]==MAXN)
  {
   Map[a][b]=c-D;
  }
 }
 return;
}
QUEUE Q;
void SPFA()
{
 for (int i=1;i<=C;i++)
  dist[i]=MAXN,flag[i]=false,times[i]=0;
 dist[S]=-D;
 Q.push(S);
 int x;
 while(Q.size())
 {
  x=Q.front();
  Q.pop();
  flag[x]=false;
  for(int i=1;i<=C;i++)
  {
   int tmp=dist[x]+Map[x][i];
   if(tmpC)
     {
      printf("-1\n");
      return;
     }
    }
   }
  }
 }
 int Max=MAXN;
 for (int i=1;i<=C;i++)
  if(Max>dist[i])
   Max=dist[i];
 printf("%d\n",-Max);
 return;
}
int main()
{
 freopen("jobhunt.in","r",stdin);
 freopen("jobhunt.out","w",stdout);
 init();
 SPFA();
 return 0;
}

2011年12月10日 星期六

USACO Dec2011 Bronze: Escaping the Farm 翻譯+題解

Problem 3: Escaping the Farm [Brian Dean and Kalki Seksaria, 2011]
【英文原題】
The cows have decided on a daring plan to escape from the clutches of
Farmer John. They have managed to procure a small inflatable raft, and
during the cover of night, a group of cows will board the raft and row
across the river bordering the farm. The plan seems perfect, until the
cows realize that their small inflatable raft may not be able to hold
much weight!
The N cows (1 group of cows is light enough to avoid sinking the raft, the cows add up
all of the weights in the group. Unfortunately, cows are notoriously bad at
arithmetic, and if the addition of the weights of the cows in a group
causes any carries to occur (using standard base 10 addition), then the
cows give up and conclude that group must weigh too much to use the raft.
Any group whose weights can be added without any carries is assumed to be
light enough to fit on the raft.
Please help the cows determine the size of the largest group that they
believe can fit on the raft (that is, the largest group whose weights can
be added together with no carries).
PROBLEM NAME: escape
INPUT FORMAT:
* Line 1: The number of cows, N (1
* Lines 2..N+1: Each line contains the weight of one cow, an integer
in the range 1…100,000,000.
SAMPLE INPUT (file escape.in):
5
522
6
84
7311
19
INPUT DETAILS:
There are 5 cows, with weights 522, 6, 84, 7311, and 19.
OUTPUT FORMAT:
* Line 1: The number of cows in the largest group whose weights can be
added together with no carries.
SAMPLE OUTPUT (file escape.out):
3
OUTPUT DETAILS:
The three weights 522, 6, and 7311, can be added together with no carries:
522
6
+ 7311
——
7839

【中文翻譯】
Problem 3:逃離農場
譯by Freddy
【題目描述】
奶牛們做了一個魯莽的計劃:那就是逃離農場主Farmer John。她們已經獲得了一個可充氣的小型木筏,計劃在某天夜晚中,一群奶牛通過使用木筏渡而過位於農場邊界的河流。這個計劃似乎很完美,直到奶牛們意識到她們的小木筏可能不能承受住她們的體重。

這N頭奶牛(1<=N<=20)的體重w_1…w_N。為了計算出一群奶牛的體重能否避免木筏沉沒的悲劇,一群奶牛把她們的體重加在一起。
不幸的是,奶牛們在算術方面臭名遠揚,並且一群奶牛內的各奶牛體重相加的過程中如果出現了進位(標準的10進制),那麼這群奶牛只好放棄因為她們知道她們的體重對於小木筏來說太重了。

所有 那些群內奶牛體重相加不出現進位的奶牛群都被認為可以乘坐那個木筏而不發生沉沒。

請幫奶牛們找出能乘坐木筏而不沉沒的奶牛群的最大奶牛數。(也就是說,找出最多的奶牛使她們的體重相加而不出現進位。)

程序名:escape
輸入格式:
▪第1行:奶牛的數量,N(1<=N<=20)
▪第2…N+1行:每行包含一頭奶牛的體重,一個整數(1…100,000,000)。
輸入樣例(file escape.in):
5
522
6
84
7311
19
輸入解釋:
有5只奶牛,她們的體重分別為522,6,84,7311和19。
輸出格式:
只有一行,表示一群使她們的體重相加而不出現進位的奶牛的最大奶牛數量。
輸出樣例(file escape.out):
3
輸出解釋:



這三個奶牛的體重分別為:522,6,7311,它們相加不會出現進位:
    522
       6
+7311
 ——–
  7839

【分析】
搜索題目,可以用DFS。遞歸每頭牛的兩種狀態:要或者不要,每次遞歸判斷一下是否符合要求(沒有進位),然後更新最優值即可。
時間複雜度:
DFS遞歸:O(2^N)
判斷:O(KN) 『K是讀入的數字的平均長度,根據題意,可取5~6』

【我的代碼】
/*
ID:yeefan
LANG:C++
PROB:escape
website:http://yeefan.tk/
*/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int N;
int W[21];
int A=0;
int Q[21];
int Best=0;
void init()
{
 scanf("%d\n",&N);
 for (int i=1;i<=N;i++)
  scanf("%d\n",&W[i]);
 return;
}
void check()
{
 int T[11];
 memset(T,0,sizeof(T));
 for(int i=1;i<=A;i++)
 {
  int K=1;
  int tmp=W[Q[i]];
  while(tmp!=0)
  {
   T[K]+=tmp%10;
   tmp/=10;
   K++;
  }
 }
 for(int i=1;i<=10;i++)
 {
  if(T[i]>=10)
   return;
 }
 if(A>Best)
  Best=A;
}
void dfs(int num)
{
 if(num==N+1)
 {
  check();
  return;
 }
 Q[++A]=num;
 dfs(num+1);
 A--;
 dfs(num+1);
}
int main()
{
 freopen("escape.in","r",stdin);
 freopen("escape.out","w",stdout);
 init();
 dfs(1);
 printf("%d\n",Best);
 //printf("%d\n",clock());
 return 0;
}

2011年11月25日 星期五

[動態規劃]USACO Nov07 Silver :Milking Time 擠奶時間(milkprod) 解題報告

譯 By CmYkRgB123
描述
貝茜是一隻非常努力工作的奶牛,她總是專注於提高自己的產量。爲了產更多的奶,她預計好了接下來的N (1 ≤ N ≤ 1,000,000)個小時,標記爲0..N-1。
Farmer John 計劃好了 M (1 ≤ M ≤ 1,000) 個可以擠奶的時間段。每個時間段有一個開始時間(0 ≤ 開始時間 ≤ N), 和一個結束時間 (開始時間 < 結束時間 ≤ N), 和一個產量 (1 ≤ 產量 ≤ 1,000,000) 表示可以從貝茜擠奶的數量。Farmer John 從分別從開始時間擠奶,到結束時間爲止。每次擠奶必須使用整個時間段。
但即使是貝茜也有她的產量限制。每次擠奶以後,她必須休息 R (1 ≤ R ≤ N) 個小時才能下次擠奶。給定Farmer John 計劃的時間段,請你算出在 N 個小時內,最大的擠奶的量。
輸入
  • 第 1 行: 三個整數 N, M, R
  • 第 2..M+1 行: 第 i+1 行 每行三個整數,爲每個時間段的開始時間、結束時間、產量
輸出
  • 第 1 行:一個整數 在 N 個小時內,最大的擠奶的量Farmer John放入擠奶計劃,開始時間,結束時間,產量。
樣例輸入
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
樣例輸出
43


【分析】
線性動態規劃。
由於題目中說每個擠奶時間段的結束時間都小於等於N,所以我們可以把每個擠奶時間段的結束時間加上R,可以在不影響結果的情況下為DP提供方便。

接著對擠奶時間段以每個時間段的開始時間為關鍵字進行快速排序。
狀態設定:
    V[i]:排序後第i個區間的產量
    S[i]:排序後第i個區間的開始時間
    E[i]:排序後第i個區間的結束時間+R
    F[i]:對於前i個時間段,在結束時間小於 第i個時間段的結束時間(即E[i])時 所獲得的最大擠奶產量。

邊界條件:
    F[0]=0
 狀態轉移方程:
    F[i]=max{F[j]}+V[i] (1≤j≤i-1,E[j]≤S[i])
目標結果:
   F[i]=max{F[i]}

【我的代碼】 
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
class MILK
{
public:
    int S;
    int E;
    int V;
}P[1001];
int F[1001];
int N,M,R;

int cmp(const void *a,const void *b)
{
    class MILK *c=(class MILK *)a;
    class MILK *d=(class MILK *)b;
    return c->S-d->S;
}

int Max(int a,int b)
{
    if(b>a)
        a=b;
    return a;
}

void init()
{
    scanf("%d %d %d\n",&N,&M,&R);
    for (int i=1;i<=M;i++)
    {
        scanf("%d %d %d\n",&P[i].S,&P[i].E,&P[i].V);
        P[i].E+=R;
    }
    qsort(P+1,M,sizeof(MILK),cmp);
}

void dynamic()
{
    F[0]=0;
    int Maxn=0;
    for (int i=1;i<=M;i++)
    {
        Maxn=0;
        for (int j=1;j<=i-1;j++)
        {
            if(P[j].E<=P[i].S)
            {
                Maxn=Max(Maxn,F[j]);
            }
        }
        Maxn+=P[i].V;
        F[i]=Maxn;
    }
   
    Maxn=0;
    for (int i=1;i<=M;i++)
    {
        Maxn=Max(Maxn,F[i]);
    }
    printf("%d\n",Maxn);
}

int main()
{
    freopen("milkprod.in","r",stdin);
    freopen("milkprod.out","w",stdout);
    init();
    dynamic();
    return 0;
}

 

2011年11月24日 星期四

[最短路]USACO 3.2.6 Sweet Butter 香甜的黃油 butter 解題報告

Farmer John has discovered the secret to making the sweetest butter in all of 
Wisconsin: sugar. By placing a sugar cube out in the pastures, he knows the N
 (1 <= N <= 500) cows will lick it and thus will produce super-sweet butter
 which can be marketed at better prices. Of course, he spends the extra money 
on luxuriesfor the cows.

FJ is a sly farmer. Like Pavlov of old, he knows he can train the cows to go to a 
certain pasture when they hear a bell. He intends to put the sugar there and 
then ring the bell in the middle of the afternoon so that the evening's milking 
produces perfect milk.

FJ knows each cow spends her time in a given pasture (not necessarily alone). 
Given the pasture location of the cows and a description of the paths the 
connect the pastures, find the pasture in which to place the sugar cube so
 that the total distance walked by the cows when FJ rings the bell is minimized. 
FJ knows the fields are connected well enough that some solution is 
always possible.


PROGRAM NAME: butter
INPUT FORMAT
  • Line 1: Three space-separated integers: N, the number of pastures: 
  • P (2 <= P <= 800), and the number of connecting paths:
  • C (1 <= C <= 1,450).
  • Cows are uniquely numbered 1..N. Pastures are uniquely numbered 1..P.
  • Lines 2..N+1: Each line contains a single integer that is the pasture number
  • in which a cow is grazing. Cow i's pasture is listed on line i+1.
  • Lines N+2..N+C+1: Each line contains three space-separated integers that
  • describe a single path that connects a pair of pastures and its length. 
  • Paths may be traversed in either direction. 
  • No pair of pastures is directly connected by more than one path. 
  • The first two integers are in the range 1..P; 
  • the third integer is in the range (1..225).
SAMPLE INPUT (file butter.in)
3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
INPUT DETAILS This diagram shows the connections geometrically:
P2  
 P1 @--1--@ C1
     \    |\
      \   | \
       5  7  3
        \ |   \
         \|    \ C3
       C2 @--5--@
          P3    P4
OUTPUT FORMAT
  • Line 1: A single integer that is the minimum distance the cows must walk 
  • to a pasture with a sugar cube.
SAMPLE OUTPUT (file butter.out)
8
OUTPUT DETAILS:

Putting the cube in pasture 4 means: cow 1 walks 3 units; cow 2 walks 5
units; cow 3 walks 0 units -- a total of 8.

------------------------------------------------------------------------------------
 
描述
農夫John發現做出全威斯康辛州最甜的黃油的方法:糖。把糖放在一片牧場上,他知道N(1<=N<=500)只奶牛會過來舔它,這樣就能做出能賣好價錢的超甜黃油。當然,他將付出額外的費用在奶牛上。
農夫John很狡猾。像以前的Pavlov,他知道他可以訓練這些奶牛,讓它們在聽到鈴聲時去一個特定的牧場。他打算將糖放在那裏然後下午發出鈴聲,以至他可以在晚上擠奶。
農夫John知道每隻奶牛都在各自喜歡的牧場(一個牧場不一定只有一頭牛)。給出各頭牛在的牧場和牧場間的路綫,找出使所有牛到達的路程和最短的牧場(他將把糖放在那)
格式
PROGRAM NAME: butter
INPUT FORMAT:
(file butter.in)
第一行: 三個數:奶牛數N,牧場數(2<=P<=800),牧場間道路數C(1<=C<=1450)
第二行到第N+1行: 1到N頭奶牛所在的牧場號
第N+2行到第N+C+1行: 每行有三個數:相連的牧場A、B,兩牧場間距離D(1<=D<=255),當然,連接是雙向的
OUTPUT FORMAT:
(file butter.out)
一行 輸出奶牛必須行走的最小的距離和
SAMPLE INPUT
3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
樣例圖形
P2  
P1 @--1--@ C1
    \    |\
     \   | \
      5  7  3
       \ |   \
        \|    \ C3
      C2 @--5--@
         P3    P4
SAMPLE OUTPUT
8
說明:
放在4號牧場最優

【分析】
圖論,多源最短路徑。由於節點數過大所以多源最短路算法Floyd無法使用,但由於圖比較稀疏,所以可以用SPFA算法就可以,經過堆優化的Dijkstra算法也可以快速滿分。

我寫了SPFA,使用鄰接表存儲。

【我的代碼】
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
int Map[1000][1000];
int Val[1000][1000];
int Abut[1000]={0};
int dist[1000][1000];
bool flag[1000]={0};
int Cow[1000]={0};
int C,N,M;
const int MAXN=2000000;

void spfa(int S)
{
    int q[10000];
    int h=0,t=1;
    int x,i;
    memset(q,0,sizeof(q));
    for (i=1;i<=N;i++)
    {
        dist[S][i]=MAXN;
        flag[i]=false;
    }
    dist[S][S]=0;
    q[t]=S;
    flag[S]=true;
    while(h<t)
    {
        h++;
        x=q[h];
        flag[x]=0;
        for(int i=1;i<=Abut[x];i++)
        {
            if(dist[S][Map[x][i]]-Val[x][i]>dist[S][x])
            {
                dist[S][Map[x][i]]=dist[S][x]+Val[x][i];
                if(!flag[Map[x][i]])
                {
                    t=t+1;
                    q[t]=Map[x][i];
                    flag[Map[x][i]]=true;
                }
            }
        }
    }
}

void init()
{
    scanf("%d %d %d\n",&C,&N,&M);
    int tmp;
    for (int i=1;i<=C;i++)
    {
        scanf("%d\n",&tmp);
        Cow[tmp]++;
    }
    int a,b,c;
    for (int i=1;i<=M;i++)
    {
        scanf("%d %d %d\n",&a,&b,&c);
        Abut[a]++;
        Map[a][Abut[a]]=b;
        Val[a][Abut[a]]=c;
       
        Abut[b]++;
        Map[b][Abut[b]]=a;
        Val[b][Abut[b]]=c;
    }
}

void work()
{
    for (int i=1;i<=N;i++)
        spfa(i);
    long long num=2000000000;
    //int pos=1;
    for(int i=1;i<=N;i++)
    {
        long long tmp=0;
        for (int j=1;j<=N;j++)
            tmp+=Cow[j]*dist[j][i];
        if(tmp<num)
        {
            num=tmp;
            //pos=i;
        }
    }
    int res=num;
    printf("%d\n",res);
}

int main()
{
    freopen("butter.in","r",stdin);
    freopen("butter.out","w",stdout);
    init();
    work();
    return 0;
}

[最短路]USACO Nov07 Silver :Cow Hurdles 奶牛跨欄 hurdles 解題報告

奶牛跨欄
譯 by CmYkRgB123
描述
Farmer John 想讓她的奶牛準備郡級跳躍比賽,貝茜和她的夥伴們正在練習跨欄。她們很累,所以她們想消耗最少的能量來跨欄。
顯然,對於一頭奶牛跳過幾個矮欄是很容易的,但是高欄卻很難。於是,奶牛們總是關心路徑上最高的欄的高度。
奶牛的訓練場中有 N (1 ≤ N ≤ 300) 個站臺,分別標記爲1..N。所有站臺之間有M (1 ≤ M ≤ 25,000)條單向路徑,第i條路經是從站臺Si開始,到站臺Ei,其中最高的欄的高度爲Hi (1 ≤ Hi ≤ 1,000,000)。無論如何跑,奶牛們都要跨欄。
奶牛們有 T (1 ≤ T ≤ 40,000) 個訓練任務要完成。第 i 個任務包含兩個數字 Ai 和 Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N),表示奶牛必須從站臺Ai跑到站臺Bi,可以路過別的站臺。奶牛們想找一條路徑從站臺Ai到站臺Bi,使路徑上最高的欄的高度最小。
你的任務就是寫一個程序,計算出路徑上最高的欄的高度的最小值。
輸入
* 行 1: 兩個整數 N, M, T
* 行 2..M+1: 行 i+1 包含三個整數 Si , Ei , Hi
* 行 M+2..M+T+1: 行 i+M+1 包含兩個整數,表示任務i的起始站臺和目標站臺: Ai , Bi
輸出
* 行 1..T: 行 i 爲一個整數,表示任務i路徑上最高的欄的高度的最小值。如果無法到達,輸出 -1。
輸入樣例
5 6 3
1 2 12
3 2 8
1 3 5
2 5 3
3 4 4
2 4 8
3 4
1 2
5 1
輸出樣例
4
8
-1 

【分析】
由題目描述可知,這是一個求最短路問題,由於是多源的,並且N最多只有300個,所以Floyd算法再合適不過了。
但是本題並不是單純的Floyd算法,需要把Floyd變形一下,每次求出Map[i][k]和Map[k][j]中的最大值,在與Map[i][j]比較,取小者賦給Map[i][j]。輸出用一個O(N^2)的枚舉找出最優解即可。

【我的代碼】
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int Max(int a,int b)
{
    return a>b?a:b;
}

int mat[301][301];
int N,M,Q;

void Floyd()
{
    int temp;
    for (int k=1;k<=N;k++) 
    { 
        for(int i=1;i<=N;i++) 
                { 
                        for(int j=1;j<=N;j++) 
                        { 
                                if ( mat[i][k]!=-1 && mat[k][j]!=-1) 
                                   { 
                    temp=Max(mat[i][k],mat[k][j]);
                    if ( (mat[i][j]==-1) || ( mat[i][j]>temp ) ) 
                        mat[i][j]=temp; 
                                   } 
                                  } 
                } 
        } 
}

void init()
{
    scanf("%d %d %d\n",&N,&M,&Q);
   
    for (int i=1;i<=N;i++)
        for (int j=1;j<=N;j++)
            mat[i][j]=-1;
   
    for(int i=1;i<=N;i++)
        mat[i][i]=0;
   
    int a,b,c;
    for (int i=1;i<=M;i++)
    {
        scanf("%d %d %d\n",&a,&b,&c);
        mat[a][b]=c;
    }
    Floyd();
    for (int i=1;i<=Q;i++)
    {
        scanf("%d %d\n",&a,&b);
        printf("%d\n",mat[a][b]);
    }
    return;
}


int main()
{
    freopen("hurdles.in","r",stdin);
    freopen("hurdles.out","w",stdout);
    init();
    return 0;
}