【贪心】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 ...
随机推荐
- bind系统调用
/* * Bind a name to a socket. Nothing much to do here since it's * the protocol's responsibility to ...
- Linux 内核驱动--多点触摸接口【转】
转自:http://blog.csdn.net/joard_yang/article/details/6225937 译自:linux-2.6.31.14/Documentation/input/mu ...
- 新浪的wap网站,发现原来我们的head存在着这样的差异
前一段时间一直被wap网站的自适应困惑…… 仔细研究了一下新浪的wap网站,发现原来我们的head存在着这样的差异…… <%@page contentType="text/html;c ...
- Javascript中的Callback方法浅析
什么是callback? 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数 ...
- 空洞卷积(dilated Convolution) 与感受野(Receptive Field)
一.空洞卷积 空洞卷积是是为了解决基于FCN思想的语义分割中,输出图像的size要求和输入图像的size一致而需要upsample,但由于FCN中使用pooling操作来增大感受野同时降低分辨率,导致 ...
- 用Max导出Unity3D使用的FBX文件流程注解(转载)
http://www.cnblogs.com/wantnon/p/4564522.html 从max导出FBX到Unity,以下环节需要特别注意.1,单位设置 很多人在建模,动画的时候,默认的ma ...
- SQLite数据库初步
Windows 10家庭中文版 想使用Python操作SQLite数据库,可是,不知道怎么建立数据库文件. 在SQLite官网溜达了一圈,总算使用上面的工具安装了建立了我需要的数据库文件. 1.进入官 ...
- 通过field:global给子元素添加css样式
{dede:arclist row=5 typeid=200} <li [field:global runphp=’yes’ name=autoindex](@me==1)?@me=”class ...
- java 多线程总结篇3之——生命周期和线程同步
一.生命周期 线程的生命周期全在一张图中,理解此图是基本: 线程状态图 一.新建和就绪状态 当程序使用new关键字创建了一个线程之后,该线程就处于新建状态,此时它和其他的Java对象一样,仅仅由Jav ...
- Codeforces 777E - Hanoi Factory(贪心+栈)
题目链接:http://codeforces.com/problemset/problem/777/E 题意:有n个环给你内环半径.外环半径和高度,叠这些环还要满足以下要求: ①:下面的环的外径要&g ...