UVa - 1622 - Robot

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 ( $ \leq$ NM $ \leq$ 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值