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, 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 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
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;}
- GCJ 2015R1B(Noisy Neighbors-分类讨论)
- 分类讨论
- TCO2016 R1B
- GCJ 2015 Round D
- GCJ 2015 1A
- GCJ
- hdu5105Math Problem(分类讨论)
- UOJ295,大力分类讨论
- BZOJ 2503 相框 分类讨论
- Codeforces R278 div2B 分类讨论
- Finals in arithmetic 分类讨论
- codeforces 789B(分类讨论)
- bzoj1177 [Apio2009] Oil 分类讨论
- NLP 分类问题的讨论
- OpenCV Machine Learning 之 K最近邻分类器 K-Nearest Neighbors
- OpenCV Machine Learning 之 K最近邻分类器的应用 K-Nearest Neighbors
- CV最简单的分类算法——knn(k nearest neighbors)
- ML:Scikit-Learn 学习笔记(2) --- Nearest Neighbors 最近邻 分类
- 预处理程序
- Cordys BOP 4.3平台使用小经验—数据库连接管理、导出流程模型为XPDL
- Windows 远程登录 Linux 服务器
- Make it Anagram
- Android---如何提高模拟器的速度--安装
- GCJ 2015R1B(Noisy Neighbors-分类讨论)
- KMP算法的理解与实现
- 黑马程序员之 ---IO
- android错误集结,慢慢积累
- FTP文件传输协议两种模式-主动模式和被动模式
- 数据库之各种常用数据库连接
- L脚本语言语法手册
- CAS实现SSO单点登录原理
- 关于提高C语言执行效率的几点