【贪心】Google Code Jam Round 1A 2018 Waffle Choppers
题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等。
设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1)。
先考虑横行,贪心地扫过去,如果到了某一行,当前统计的黑点数恰好为sum/(h+1),就在这里切一刀,接着统计。否则,如果>sum/(h+1),则无解。在这个过程中,每一行被切到哪一横组里就确定了。
然后纵切,过程跟横切类似,只不过统计的不是一个变量,而是一个大小为(h+1)的数组,如果到了某一列,当前统计的数组的每个分量的黑点数恰好为sum/(h+1)/(v+1),就在这里切一刀,接着统计。否则,如果数组的某个分量>sum/(h+1),则无解。
#include<cstdio>
#include<cstring>
using namespace std;
int T,n,m,h,v;
char a[105][105];
int hq[105];
int cnts[105],t;
bool check1(){
for(int i=1;i<=h+1;++i){
if(cnts[i]!=t){
return 0;
}
}
return 1;
}
bool check2(){
for(int i=1;i<=h+1;++i){
if(cnts[i]>t){
return 0;
}
}
return 1;
}
int main(){
//freopen("a.in","r",stdin);
scanf("%d",&T);
for(int zu=1;zu<=T;++zu){
printf("Case #%d: ",zu);
scanf("%d%d%d%d",&n,&m,&h,&v);
for(int i=1;i<=n;++i){
scanf("%s",a[i]+1);
}
int sum=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
sum+=(a[i][j]=='@');
}
}
if(sum%(h+1)!=0){
puts("IMPOSSIBLE");
continue;
}
t=sum/(h+1);
int cnt=0;
bool flag=1;
int last=0,num=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
cnt+=(a[i][j]=='@');
}
if(cnt==t){
++num;
for(int k=last+1;k<=i;++k){
hq[k]=num;
}
last=i;
cnt=0;
}
else if(cnt>t){
flag=0;
break;
}
}
if(!flag){
puts("IMPOSSIBLE");
continue;
} if(sum/(h+1)%(v+1)!=0){
puts("IMPOSSIBLE");
continue;
}
t=sum/(h+1)/(v+1);
memset(cnts,0,sizeof(int)*(2+h));
flag=1;
for(int i=1;i<=m;++i){
for(int j=1;j<=n;++j){
cnts[hq[j]]+=(a[j][i]=='@');
}
if(check1()){
memset(cnts,0,sizeof(int)*(2+h));
}
else if(!check2()){
flag=0;
break;
}
}
if(!flag){
puts("IMPOSSIBLE");
continue;
}
puts("POSSIBLE");
}
return 0;
}
【贪心】Google Code Jam Round 1A 2018 Waffle Choppers的更多相关文章
- 【二分答案】Google Code Jam Round 1A 2018
题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...
- Google Code Jam Round 1A 2015 解题报告
题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...
- Google Code Jam Round 1A 2015 Problem B. Haircut 二分
Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...
- [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
Problem A. Minimum Scalar Product This contest is open for practice. You can try every problem as ...
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
- Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words
Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...
- Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit
Google Code Jam Qualification Round Africa 2010 Problem A. Store Credit https://code.google.com/code ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
随机推荐
- 快速修改Matlab默认启动路径(Windows/Mac)
如何修改Matlab启动路径/Windows or Mac 控制台内输入一下两行命令,之后重启MATLAB即可 newpath = '你所要设定的路径'; userpath(newpath) ...
- SqlServer存储过程(增删改查)
* IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...
- Linux硬盘镜像获取与还原(dd、AccessData FTK Imager)
1.硬盘镜像获取工具:dd dd是Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 1.1 本地取数据 查看磁盘及分区 # fdisk - ...
- go 切片
切片定义 切片是基于数组类型做的一层封装.它非常灵活,可以自动扩容. var a []int //定义一个int类型的空切片 切片初始化, a[start:end]创建一个包括从start到end-1 ...
- ispoweroftwo 判断2的次幂
首先结果是: public bool IsPowerOfTwo(int n) { if(n<1) return false;//2的次幂一定大于0 return ((n & (n -1) ...
- SDN核心技术剖析和实战指南---读书笔记
第一章 SDN定义如下: SDN是一种新兴的基于软件的网络架构及技术,其最大的特点在于具有松耦合的控制平面与数据平面.支持集中化的网络状态控制.实现底层网络设施对上层应用的透明. SDN和NFV: O ...
- SSD固态硬盘检测工具AS SSD参数
一. 使用AS SSD Benchmark进行查看 包括了4个方面的测试(顺序读写.4K随机读写.64线程4K读写.寻道时间) AS SSD的主要测试,也是网上最常见得到测试成绩的,是它主界面上持续. ...
- Docker中安装wiki Confluence
一下内容在centos 7安装成功. 一.安装docker 1.yum安装docker yum update # 更新yum yum install docker # yum安装docker 2.开启 ...
- Unix IPC之Posix消息队列(1)
部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...
- qt 问题及处理
1. 包依赖问题 在windows平台时,通过microsoft process Explorer可以查看所以来的dll.并将这些dll复制到应用程序目录,加上qt.conf就可以使用了. [Path ...