# 111qqz的小窝

poj 3294

poj3974

## 1660: [Usaco2006 Nov]Bad Hair Day 乱发节

Time Limit: 2 Sec  Memory Limit: 64 MB
Submit: 872  Solved: 415
[Submit][Status][Discuss]

## Input

* Line 1: 牛的数量 N。

* Lines 2..N+1: 第 i+1 是一个整数，表示第i头牛的高度。

## Output

* Line 1: 一个整数表示c[1] 至 c[N]的和。

6
10
3
7
4
12
2

5

3+0+1+0+1=5

## 1657: [Usaco2006 Mar]Mooo 奶牛的歌声

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 634  Solved: 447
[Submit][Status][Discuss]

## Description

Farmer John’s N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the range 1..2,000,000,000 nanometers (FJ really is a stickler for precision). Each cow moos at some volume v in the range 1..10,000. This “moo” travels across the row of cows in both directions (except for the end cows, obviously). Curiously, it is heard only by the closest cow in each direction whose height is strictly larger than that of the mooing cow (so each moo will be heard by 0, 1 or 2 other cows, depending on not whether or taller cows exist to the mooing cow’s right or left). The total moo volume heard by given cow is the sum of all the moo volumes v for all cows whose mooing reaches the cow. Since some (presumably taller) cows might be subjected to a very large moo volume, FJ wants to buy earmuffs for the cow whose hearing is most threatened. Please compute the loudest moo volume heard by any cow.

Farmer John的N(1<=N<=50,000)头奶牛整齐地站成一列“嚎叫”。每头奶牛有一个确定的高度h(1<=h<=2000000000)，叫的音量为v (1<=v<=10000)。每头奶牛的叫声向两端传播，但在每个方向都只会被身高严格大于它的最近的一头奶牛听到，所以每个叫声都只会 被0，1，2头奶牛听到（这取决于它的两边有没有比它高的奶牛）。 一头奶牛听到的总音量为它听到的所有音量之和。自从一些奶牛遭受巨大的音量之后，Farmer John打算买一个耳罩给被残害得最厉 害的奶牛，请你帮他计算最大的总音量。

## Input

* Line 1: A single integer, N.

* Lines 2..N+1: Line i+1 contains two space-separated integers, h and v, for the cow standing at location i.

## Output

* Line 1: The loudest moo volume heard by any single cow.

## Sample Input

3
4 2
3 5
6 10

INPUT DETAILS:

Three cows: the first one has height 4 and moos with volume 2, etc.

7

Silver

## 1656: [Usaco2006 Jan] The Grove 树木

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 143  Solved: 88
[Submit][Status][Discuss]

## Description

The pasture contains a small, contiguous grove of trees that has no ‘holes’ in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She’s just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn’t think you could pass ‘through’ the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here’s a typical example where ‘.’ is pasture (which Bessie may traverse), ‘X’ is the grove of trees, ‘*’ represents Bessie’s start and end position, and ‘+’ marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): …+… ..+X+.. .+XXX+. ..+XXX+ ..+X..+ …+++* The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she’s starting ‘outside’ the grove instead of in a sort of ‘harbor’ that could complicate finding the best path.

贝茜很想知道，最少需要多少步能围绕树林走一圈，最后回到起点．她能上下左右走，也能走对角线格子．牧场被分成R行C列(1≤R≤50，1≤C≤50)．下面是一张样例的地图，其中“．”表示贝茜可以走的空地，  “X”表示树林，  “*”表示起点．而贝茜走的最近的路已经特别地用“+”表示出来．

## Input

* Line 1: Two space-separated integers: R and C

* Lines 2..R+1: Line i+1 describes row i with C characters (with no spaces between them).

第1行输入R和C，接下来R行C列表示一张地图．地图中的符号如题干所述．

## Output

* Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.

输出最少的步数．

6 7
…….
…X…
..XXX..
…XXX.
…X…
……*

13

## 3407: [Usaco2009 Oct]Bessie’s Weight Problem 贝茜的体重问题

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 88  Solved: 79
[Submit][Status][Discuss]

56 4
15
19
20
21

56

## 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 353  Solved: 190
[Submit][Status][Discuss]

## Description

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1,$2, and $3. Farmer John has exactly$5 to spend. He can buy 5 tools at $1 each or 1 tool at$3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are: 1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1 Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

约翰到奶牛商场里买工具．商场里有K(1≤K≤100).种工具，价格分别为1，2，…，K美元．约翰手里有N(1≤N≤1000)美元，必须花完．那他有多少种购买的组合呢？

## Input

A single line with two space-separated integers: N and K.

仅一行，输入N，K.

## Output

A single line with a single integer that is the number of unique ways FJ can spend his money.

不同的购买组合数．

5 3

5

## 1653: [Usaco2006 Feb]Backward Digit Sums

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 349  Solved: 258
[Submit][Status][Discuss]

## Description

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this: 3 1 2 4 4 3 6 7 9 16 Behind FJ’s back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ’s mental arithmetic capabilities. Write a program to help FJ play the game and keep up with the cows.

## Input

* Line 1: Two space-separated integers: N and the final sum.

## Output

* Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

4 16

## Sample Output

3 1 2 4
OUTPUT DETAILS:

There are other possible sequences, such as 3 2 1 4, but 3 1 2 4
is the lexicographically smallest.

## hdu 2586 How far away ？ (tarjan算法求LCA模板题)

dfs跑出根到每个点的距离，设为dis[i]

## 树边，前向边，后向边，横叉边

vis = 0,表示该顶点没没有被访问
vis = 1,表示该顶点已经被访问，但其子孙后代还没被访问完，也就没从该点返回
vis = 2,，表示该顶点已经被访问，其子孙后代也已经访问完，也已经从该顶点返回

《算法导论》334页有这4种边的准确定义，在此不累述
DFS过程中，对于一条边u->v
vis[v] = 0,说明v还没被访问，v是首次被发现，u->v是一条树边
vis[v] = 1,说明v已经被访问，但其子孙后代还没有被访问完（正在访问中），而u又指向v？说明u就是v的子孙后代，u->v是一条后向边，因此后向边又称返祖边
vis[v] = 3,z说明v已经被访问，其子孙后代也已经全部访问完，u->v这条边可能是一条横叉边，或者前向边

## 1652: [Usaco2006 Feb]Treats for the Cows

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 290  Solved: 226
[Submit][Status][Discuss]

## Description

FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons: * The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats. * Like fine wines and delicious cheeses, the treats improve with age and command greater prices. * The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000). * Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a. Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally? The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

•零食按照1．．N编号，它们被排成一列放在一个很长的盒子里．盒子的两端都有开口，约翰每
天可以从盒子的任一端取出最外面的一个．
•与美酒与好吃的奶酪相似，这些零食储存得越久就越好吃．当然，这样约翰就可以把它们卖出更高的价钱．
•每份零食的初始价值不一定相同．约翰进货时，第i份零食的初始价值为Vi(1≤Vi≤1000)．
•第i份零食如果在被买进后的第a天出售，则它的售价是vi×a．
Vi的是从盒子顶端往下的第i份零食的初始价值．约翰告诉了你所有零食的初始价值，并希望你能帮他计算一下，在这些零食全被卖出后，他最多能得到多少钱．

## Input

* Line 1: A single integer,

N * Lines 2..N+1: Line i+1 contains the value of treat v(i)

## Output

* Line 1: The maximum revenue FJ can achieve by selling the treats

## Sample Input

5
1
3
1
5
2

Five treats. On the first day FJ can sell either treat #1 (value 1) or
treat #5 (value 2).

## Sample Output

43

OUTPUT DETAILS:

FJ sells the treats (values 1, 3, 1, 5, 2) in the following order
of indices: 1, 5, 2, 3, 4, making 1×1 + 2×2 + 3×3 + 4×1 + 5×5 = 43.

dp[i][j]表示最左端为i，最右端为j能得到的最大价值。

（注意观察，（i,j）的状态是由(i+1,j)或者(i,j-1)得到的，因为是从里向外推。）