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

来源:互联网 发布:php后端开发教程 编辑:程序博客网 时间:2024/05/29 19:32

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 
 
42 3 64 1 23 3 85 2 0
Case #1: 7Case #2: 0Case #3: 8Case #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