There is a robot in each grid of an N x M Grid. These robots can execute commands. Commands include:
-
NORTH
- All robots move one grid north. SOUTH
- All robots move one grid south. WEST
- All robots move one grid west. EAST
- All robots move one grid east.
If a robot stays outside of the Grid after executing the command, it will be destroyed immediately.
Given the total number of each type of command, you task is to arrange an order of these commands, so that the maximized number of commands (i.e. the maximal sum of the number of commands executed by each robot) are executed by these N x M robots. (Note: a destroyed robot can not execute commands anymore.)
Input
Input contains several cases.
The first line of each case contains two positive integers N and M indicating the number of rows and columns in the grid ( 1 N, M
105). The second line contains four integers indicating the number of each type of command : NORTH, SOUTH, WEST and EAST respectively. Each of the four numbers will not exceed 105.
The last case is followed by a line containing two zeros.
Output
For each case, output an integer indicating the maximized number of commands that these robots can execute. The answer will be fit in a 64-bit signed integer.Sample Input
2 2 1 0 0 0 2 2 1 1 1 1 0 0
Sample Output
Case 1: 4 Case 2: 9
贪心,先保证cw >= ce,cn >= cs,并且先执行东西来回移动比南北更好。然后执行东西来回移动,接着对比每次向西移动和南北移动哪个比较好。知道仅剩西和北两个方向,则结束。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
using namespace std;
typedef long long LL;
int main()
{
ios::sync_with_stdio(false);
LL n, m, t = 0;
while (cin >> n >> m && m){
LL cn, cs, cw, ce, ans = 0;
cin >> cn >> cs >> cw >> ce;
if (cn < cs) {
swap(cs, cn); // s.t. cw>=ce
}
if (cw < ce) {
swap(ce, cw); // s.t. cn >= cs
}
LL t1 = n + (m - 1) * n * ce * 2 + (m - 1) + (m - 1) * (n - 1) * cs * 2;
LL t2 = m + m * (n - 1) * cs * 2 + (n - 1) + (m - 1) * (n - 1) * ce * 2;//判断是否需要交换。
if (cw - ce) {
t1 += (m - 1) * n;
t2 += (m - 1) * (n - 1);
}
if (cn - cs) {
t1 += (m - 1) * (n - 1);
t2 += m * (n - 1);
}
if (t1 < t2) {
swap(m, n);
swap(cn, cw);
swap(cs, ce);
}
if (ce) { // 先东西来回
ans += n + (m - 1) * n * ce * 2;
cw -= ce;
ce = 0;
m--;
}
if (cw) {
ans += m * n;
cw--;
}
cw = min(m, cw);
while (cw || cn){
if (cs){ // 判断是否开始进行南北移动。
LL t1 = m * n + (n - 1) * m * 2 * cs;
LL t2 = m * n + (m - 1) * n + (m - 1) * (n - 1) * (2 * cs - 1);
if (cn - cs) {
t1 = m * n + (n - 1) * m * (2 * cs + 1);
t2 = m * n + (m - 1) * n + (m - 1) * (n - 1) * 2 * cs;
}
if (t1 > t2 || !cw){ // cw为0时也要执行南北移动。
ans += m + m * (n - 1) * cs * 2;
cn -= cs;
cs = 0;
n--;
if (cn) {
ans += m * n;
cn--;
}
cn = min(n, cn);
}
else {
ans += m * n;
m--;
cw--;
}
}
else if (!cw) {
ans += m * cn * (2 * n - cn + 1) / 2;
cn = 0;
}
else if (!cn) {
ans += n * cw * (2 * m - cw + 1) / 2;
cw = 0;
}
else{
ans += m * n;
if (m > n) {
m--;
cw--;
}
else {
n--, cn--;
}
}
}
cout << "Case " << ++t << ": " << ans << endl;
}
return 0;
}