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;
}
时间: 2024-08-24 02:50:48

GCJ 2015R1B(Noisy Neighbors-分类讨论)的相关文章

【线段树】【分类讨论】水果姐逛水果街Ⅰ

3304 水果姐逛水果街Ⅰ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了一个赚钱的方法:在某家水果店买一个水果,再到另外一家店卖出去,赚差价. 就在水果姐窃喜的时候,cgh突然出现,他为了为难水果姐,给出m个问题,每个问题要求水果姐从第x家店出发到第y家店

dp+分类讨论 Gym 101128E

题目链接:http://codeforces.com/gym/101128 感觉这个人写的不错的(我只看了题目大意):http://blog.csdn.net/v5zsq/article/details/61428924 Description n个小木条,一段前面有一个小箭头,给出第一个小木条的非箭头端端点横坐标以及每个小木条箭头端的坐标,现在要从下往上把这n'个木条按顺序叠放好,要求相邻两个小木条必须有一个共同端点且有交叠部分,问小木条有多少种放法 Input 第一行一整数n表示木条数量,之

BZOJ 1067 降雨量(RMQ+有毒的分类讨论)

1067: [SCOI2007]降雨量 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 4399  Solved: 1182 [Submit][Status][Discuss] Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意 Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2004和2005年的降雨量分别为4920,5901,2832和3890, 则可以说

cf 251 B Playing with Permutations 暴力 分类讨论

题链;http://codeforces.com/problemset/problem/251/B B. Playing with Permutations time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little Petya likes permutations a lot. Recently his mom has p

BZOJ 1099([POI2007]树Drz-9次线段树&amp;分类讨论+线段树与插入顺序维护2个参数)

1099: [POI2007]树Drz Time Limit: 15 Sec  Memory Limit: 162 MB Submit: 142  Solved: 55 [Submit][Status] Description CDQZ是一个偏远的小学校,FGD在学校里中了一排树.他却不喜欢这些树的顺序,因为他们高高矮矮显得那么参差不齐. FGD定义这些树的不整齐程度为相邻两树的高度差的和.设树高分别为h1,h2,h3,-,hn.那么不整齐程度定义为:|h1-h2|+|h2-h3|+--+|hn

POJ 2826 An Easy Problem?!(线段相交,分类讨论)

题意:给两个线段,问他们能收集到多少雨水. 链接:http://poj.org/problem?id=2826 解法:分四种情况讨论 1. 存在一个线段与x轴平行,答案为0 2. 两个线段没有交点,答案为0 3. 1和2都不满足时,令线段1为比较低的那个线段,且p1为其比较高的那个点,若该点往y轴正方向的射线与线段2有交点,则答案为0 4. 3不满足时,求出两线段交点x1,p1做一条平行于x轴的线,该线与线段2的交点x2,则三角形x1, x2, p1的面积就是答案 小结:此题属于分类讨论型的题,

Codeforces 460D Little Victor and Set --分类讨论+构造

题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四个数(L,L+1,L+2,L+3),这样的话(L^(L+1)) ^ ((L+2)^(L+3)) = 0,最优 如果L不是偶数,那么看从L+1到R有没有四个数,如果有则取该四个数,否则最小异或和达不到0,也达不到1了,不再考虑k=4,k=3时还有可能等于0,所以转到k=3 2. k=3时,要使异或和为0,那

【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树--但是会TLE. LCT一样无脑,但是少一个log,可以过. 正解是分类讨论, 如果t不在lca(s,f)的子树内,答案是dis(lca(s,f),f). 如果t在lca(s,f)的子树内,并且dep(lca(s,t))>dep(lca(f,t)),答案是dis(lca(s,t),f): 否则答案是dis(lca(f,t),f). #in

LA UVaLive 6862 Triples (数学+分类讨论)

题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数学题加分类讨论.首先对 x进行分类讨论. 当 0 = x 时,只要 y = z,就行,那么就有(m+1) *  (n-1) 个,因为 y 可能从0取到m ,j 可以从2取到 n. 当 0 != x 时,那么只要一个勾股定理能构成,只要幂大于2,就一下没解,所以我们把第一种中拿出j = 2时,的特殊情况,特殊考