GCJ 2015r1b (Noisy neighbors-category discussion)

Source: Internet
Author: User

Problem

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

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

Input

The first line of the input gives the number of test cases, T. T lines follow; Each contains three space-separated integers: R, C, 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 th E minimum possible unhappiness for the building.

Limits

1 ≤ T ≤1000.
0≤ Nr*c.

Small DataSet

1 ≤ r*c ≤16.

Large DataSet

1 ≤ r*c ≤10000.

Sample

Input

Output
42 3 1 3 2 0 
case #1:7Case #2:0Case #3:8Case #4:0 
in the case #1, every the occupied by a tenant and all seven internal walls has tenants on either side.

in case #2, there is various ways to place the and the tenants so and they do not share a wall. One is illustrated below.

in case #3, the optimal strategy are to place the eight tenants in a ring, leaving the middle apartment unoccupied.

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






Sub RC for odd, odd , even even discussion


#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)/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&LT;=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&LT;=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&LT;=P4) Ans=k2*4;else if ( K2&LT;=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&LT;=P4) Ans2=k2*4;else if (K2&LT;=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&LT;=P4) Ans=k2*4;else if(K2&LT;=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;}







GCJ 2015r1b (Noisy neighbors-category discussion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.