【贪心】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 ...
随机推荐
- 安卓微信、QQ自带浏览器 UXSS 漏洞
安卓微信.QQ自带浏览器 UXSS 漏洞 注:PDF报告原文下载链接 Author: hei@knownsec.com Date: 2016-02-29 一.漏洞描述 在安卓平台上的微信及QQ自带浏览 ...
- springboot中报异常Whitelabel Error Page
开始以为是url写错了,但其实不是,然后启动application类在的包是要在最顶部,并且和pom中groupid一样 这个也没错,后来发现能访问RestController中的url,但是进不了方 ...
- 你需要知道的12个Git高级命令【转】
转自:http://www.linuxidc.com/Linux/2016-01/128024.htm 众所周知,Git目前已经是分布式版本控制领域的翘楚,围绕着Git形成了完整的生态圈.学习Git, ...
- RabbitMQ--Publish/Subscribe(四)
先前例子中,我们创建了一个简单的日志系统,广播messages到consumer接收方. 但如果有日志错误级别的,不同的consumer接收不同错误级别的信息.比如consumer1接收info和wa ...
- 组合比较符(PHP7+)
php7+支持组合比较符,即<=>,英文叫做combined comparison operator,组合比较运算符可以轻松实现两个变量的比较,当然不仅限于数值类数据的比较. 语法:$a& ...
- 【h5标签转小程序标签】小程序使用wxParse解析html教程
一.先下载所需文件,下载地址:https://pan.baidu.com/s/1umZO9uI24zUTRd7VqaWbAg ,下载完毕后会得到一个wxParse文件夹,后面会用到: 二.先拷贝cs ...
- python+selenium第一步 - 环境搭建
刚开始学习一门技术,肯定是要从环境搭建开始的,我也不例外. 首先选择需要安装的版本,我使用的是mac自带的2.7版本. selenium2,和火狐浏览器 为求稳定不会出现未知问题,我选择了seleni ...
- Effective STL 笔记 -- Item 6 ~ 7: Container and Object Pointer
Effective STL 笔记 – Item 6 ~ 7: Container and Object Pointer 中间两次笔记被删掉了,简单补一下: Item 3 中提到如果将对象直接放入容器中 ...
- CentOs 安装 swftools
一 下载安装包 #wget http://www.swftools.org/swftools-0.9.1.tar.gz 二 安装相关依赖库 #yum install gcc* automake zli ...
- 20169211《Linux内核原理与分析》第五周作业
1.在自己的linux系统中搭建实验环境: 2.使用GDB调试内核跟踪启动过程: 3.分析start_kernel的代码. 1.在自己的linux系统中搭建实验环境 1.1 下载linux-3.18. ...