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
    评论
GCJ-02坐标是一种加密坐标系,使用的是国测局制定的加密算法,因此需要进行转换才能得到标准的WGS-84坐标。以下介绍两种常见的转换方法: 1. 使用在线转换工具 目前网络上有很多在线的坐标转换工具,可以很方便地将GCJ-02坐标转换为WGS-84坐标。例如,可以使用坐标转换网站https://www.gpsspg.com/maps.htm 进行转换。具体步骤为: - 打开网站,点击左上方的“坐标转换”按钮; - 在弹出的页面中,选择“坐标系转换”; - 在“待转换坐标系”中选择“GCJ-02坐标系”,在“目标坐标系”中选择“WGS-84坐标系”,输入需要转换的坐标点; - 点击“开始转换”按钮,即可得到转换后的WGS-84坐标点。 2. 使用程序进行转换 除了使用在线转换工具,还可以通过编写程序实现GCJ-02坐标到WGS-84坐标的转换。例如,可以使用Python编写以下代码实现: ```python import math def gcj02_to_wgs84(lng, lat): a = 6378245.0 ee = 0.00669342162296594323 pi = 3.14159265358979324 dLat = transform_lat(lng - 105.0, lat - 35.0) dLng = transform_lng(lng - 105.0, lat - 35.0) radLat = lat / 180.0 * pi magic = math.sin(radLat) magic = 1 - ee * magic * magic sqrtMagic = math.sqrt(magic) dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi) dLng = (dLng * 180.0) / (a / sqrtMagic * math.cos(radLat) * pi) wgsLat = lat - dLat wgsLng = lng - dLng return wgsLng, wgsLat def transform_lat(lng, lat): pi = 3.14159265358979324 ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng)) ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0 ret += (20.0 * math.sin(lat * pi) + 40.0 * math.sin(lat / 3.0 * pi)) * 2.0 / 3.0 ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 * math.sin(lat * pi / 30.0)) * 2.0 / 3.0 return ret def transform_lng(lng, lat): pi = 3.14159265358979324 ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng)) ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0 ret += (20.0 * math.sin(lng * pi) + 40.0 * math.sin(lng / 3.0 * pi)) * 2.0 / 3.0 ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 * math.sin(lng / 30.0 * pi)) * 2.0 / 3.0 return ret # 示例 lng, lat = 116.3975, 39.9086 wgs_lng, wgs_lat = gcj02_to_wgs84(lng, lat) print(wgs_lng, wgs_lat) ``` 运行以上代码,即可将GCJ-02坐标转换为WGS-84坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值