#P2905. [USACO08OPEN] Crisis on the Farm G

    ID: 1948 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>字符串动态规划,dp搜索2008USACO

[USACO08OPEN] Crisis on the Farm G

Description

John and his cows have formed a band called "Backstreet Cows," and they are rehearsing on the pasture. The cows are grouped into NN piles. In each pile, 3030 cows stand one on another, forming a tower. There are also MM tall haystacks on the pasture.

As a skilled conductor, John can use four whistle tones. Each tone makes all towers move one cell in one of the four directions: east, west, south, or north.

Each time a tower reaches a cell that contains a haystack, the cow at the top of the tower jumps onto the haystack and stays there, while the remaining cows continue to stand as a tower on that cell. When only one cow remains in a tower, that cow also jumps onto the haystack.

Suddenly, the neighbor’s milk tank explodes. A torrent of milk is rushing toward John’s pasture and will flood it soon. John must act immediately and use whistle tones to save as many cows as possible. Cows on haystacks will not drown.

John has KK whistle blows left. How many cows can he save at most? Compute the maximum number of cows that can be saved, and a sequence of whistle tones to achieve it. The sequence is represented by E,W,S,N\mathtt{E, W, S, N} for east, west, south, and north. If multiple sequences achieve the maximum, output the lexicographically smallest one.

Input Format

  • The first line contains three positive integers N,M,KN, M, K.
  • The next NN lines each contain two positive integers Xi,YiX_i, Y_i, the coordinates (Xi,Yi)(X_i, Y_i) of a cow pile.
  • The next MM lines each contain two positive integers Xi,YiX_i, Y_i, the coordinates (Xi,Yi)(X_i, Y_i) of a haystack.

Output Format

  • The first line contains an integer, the maximum number of cows that can be saved.
  • The second line contains a string of length KK that is the lexicographically smallest whistle tone sequence achieving that maximum.
3 6 3 
3 4 
6 2 
5 7 
8 2 
9 2 
6 4 
5 4 
6 7 
8 7 

6 
EEE 

Hint

Sample explanation:

After 33 whistles, 33 haystacks each save one cow.

Constraints:

For 100%100\% of the testdata, 1K301 \le K \le 30, 1N,M,Xi,Yi10001 \le N, M, X_i, Y_i \le 1000.

Translated by ChatGPT 5