GCJ 2015R1B(Noisy Neighbors-分类讨论)

Problem

You are a landlord who owns a building that is an R x C grid of apartments; each apartment is a unit square cell with four walls. You want to rent out N of these apartments to tenants, with exactly one tenant per apartment, and leave the others empty. Unfortunately, all of your potential tenants are noisy, so whenever any two occupied apartments share a wall (and not just a corner), this will add one point of unhappiness to the building. For example, a 2x2 building in which every apartment is occupied has four walls that are shared by neighboring tenants, and so the building's unhappiness score is 4.

If you place your N tenants optimally, what is the minimum unhappiness value for your building?

Input

The first line of the input gives the number of test cases, TT lines follow; each contains three space-separated integers: RC, and N.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum possible unhappiness for the building.

Limits

1 ≤ T ≤ 1000.
0 ≤ N ≤ R*C.

Small dataset

1 ≤ R*C ≤ 16.

Large dataset

1 ≤ R*C ≤ 10000.

Sample


Input 
 

Output 
 
4
2 3 6
4 1 2
3 3 8
5 2 0

Case #1: 7
Case #2: 0
Case #3: 8
Case #4: 0

In Case #1, every room is occupied by a tenant and all seven internal walls have tenants on either side.

In Case #2, there are various ways to place the two tenants so that they do not share a wall. One is illustrated below.

In Case #3, the optimal strategy is to place the eight tenants in a ring, leaving the middle apartment unoccupied.

Here are illustrations of sample cases 1-3. Each red wall adds a point of unhappiness.






分RC为奇偶,奇,偶偶讨论


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXR (10000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

int main()
{
//	freopen("B-large.in","r",stdin);
//	freopen("B-large.out","w",stdout);
	
	int T;
	cin>>T;
	For(kcase,T)
	{
		ll r,c,k;
		cin>>r>>c>>k;
		
		if (r<c) swap(r,c); 
		if (r%2==1&&c%2==0) swap(r,c);
		
		ll t1=r*c/2,S=r*c;
		ll t2=r*c-t1;
		
		if (t1>t2) swap(t1,t2);
		
		ll ans=0;
		ll k2=S-k;
		ll allsid=(r-1)*c+(c-1)*r;
			
		if (k<=t2) ans=0;
		else if (c==1)
		{
			ans=2*k2;
			ans=allsid-ans;
		}
		else 
		{
			
			if (r%2==0&&c%2==0)
			{
				ll p4=(r-2)*(c-2)/2,p3=r+c-4,p2=2;
				
				if (k2<=p4)
					ans=k2*4;
				else if (k2<=p4+p3)
					ans=p4*4+(k2-p4)*3;
				else 
					ans=p4*4+p3*3+(k2-p4-p3)*2;
				
				ans=allsid-ans;
			}
			else if (r%2==1&&c%2==1)
			{
				
					ll p4=(r-2)*(c-2)/2+1,p3=(r-2)/2*2+(c-2)/2*2,p2=4;
					
					if (k2<=p4)
						ans=k2*4;
					else if (k2<=p4+p3)
						ans=p4*4+(k2-p4)*3;
					else 
						ans=p4*4+p3*3+(k2-p4-p3)*2;
					ans=allsid-ans;
				
					{
						ll p4=(r-2)*(c-2)/2,p3=(r-2+1)/2*2+(c-2+1)/2*2,p2=0;
						ll ans2=0; 
						if (k2<=p4)
							ans2=k2*4;
						else if (k2<=p4+p3)
							ans2=p4*4+(k2-p4)*3;
						else 
							ans2=p4*4+p3*3+(k2-p4-p3)*2;
						ans2=allsid-ans2;
						
						ans=min(ans,ans2);
					}
									
			}
			else if (r%2==0&&c%2==1)
			{
				ll p4=(r-2)*(c-2)/2,p3=(r-2)+(c-2),p2=2;
				
				if (k2<=p4)
					ans=k2*4;
				else if (k2<=p4+p3)
					ans=p4*4+(k2-p4)*3;
				else 
					ans=p4*4+p3*3+(k2-p4-p3)*2;
				ans=allsid-ans;
			
			}
			
		}


		printf("Case #%d: %lld\n",kcase,ans);
	}
	
	
	return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值