OvO http://codeforces.com/contest/884/problem/E

  884e

  考虑并查集,每个点向上方和左方的点合并,答案即为1的总数减去需要合并的次数

  由于只有16MB,考虑动态数组

  由于动态数组,则并查集的时候需要一些细节处理,略OVO

  而且还卡常数

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std;
namespace fastIO {
#define BUF_SIZE 100000
//fread -> read
bool IOerror = 0;
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if(p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline bool read(int &x) {
char ch;
while(blank(ch = nc()));
if(IOerror)
return false;
for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
return true;
}
inline void read(char *s){
char ch=nc();
for (;blank(ch);ch=nc());
if (IOerror)return;
for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=0;
}
#undef BUF_SIZE
};
using namespace fastIO; const int M=(1<<14)+14;
int ma[M*2],n,m,id;
bool mp[2][M];
int dx[3]={0,0,-1};
int dy[3]={0,-1,0};
int ans;
char s[M/4]; inline int xnum(char spl)
{
if(spl>='A' && spl<='F')
return spl-'A'+10;
else
return spl-'0';
} //inline int getpos(int x,int y)
//{
// return (x-1)*m+y;
//} inline int xgetpos(int x,int y)
{
return (x-id+1-1)*m+y+m;
} //inline void getxy(int pos,int &x,int &y)
//{
// y=(pos-1)%m+1;
// x=(pos-y)/m+1-id+1;
//} inline int fff(int pos)
{
if(ma[pos]==pos) return pos;
return ma[pos]=fff(ma[pos]);
} inline bool check(int x,int y)
{
if(x<=0 || y<=0 || x>n || y>m || mp[x-id+1][y]==0)
return false;
return true;
} void deal(int x,int y)
{
int i,j,x0,y0,pos,pos0,p,p0,xx,xy,xx0,xy0;
pos=xgetpos(x,y);
for(i=1;i<=2;i++)
{
x0=x+dx[i]; y0=y+dy[i];
if(check(x0,y0)==false) continue;
pos0=xgetpos(x0,y0);
p=fff(pos); p0=fff(pos0);
if(p!=p0)
{
ans--;
ma[p0]=p;
}
}
} void xread()
{
int i,j,t,num;
// char chr;
// getchar();
read(s+1);
for(j=1;j<=m/4;j++)
{
// chr=getchar();
// num=xnum(chr);
num=xnum(s[j]);
for(t=4;t>=1;t--)
{
mp[1][(j-1)*4+t]=num&1;
if(num&1) ans++;
// cout<<(num&1)<<' ';
num>>=1;
}
}
// cout<<endl;
} void solve()
{
int i,j,pos;
ans=0;
for(i=1;i<=n;i++)
{
id=i;
for(j=1;j<=m;j++)
mp[0][j]=mp[1][j],ma[j]=ma[m+j]-m;
xread();
for(j=1;j<=m;j++)
if(mp[1][j])
{
pos=xgetpos(i,j);
ma[m+j]=pos;
}
for(j=1;j<=m;j++)
{
if(mp[1][j])
deal(i,j);
}
}
printf("%d\n",ans);
} int main()
{
// freopen("e.in","r",stdin);
int i,j,t,num,tmp;
read(n); read(m);
solve();
return 0;
}

  

Codeforces 884E E. Binary Matrix的更多相关文章

  1. CodeForces 803A Maximal Binary Matrix

    枚举. 枚举对角线上放多少个$1$,剩余的贪心放,更新答案. #include <iostream> #include <cstdio> #include <cstrin ...

  2. Educational Codeforces Round 20 A. Maximal Binary Matrix

    A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  4. 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

    题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...

  5. 【leetcode】1253. Reconstruct a 2-Row Binary Matrix

    题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...

  6. Maximal Binary Matrix CodeForces - 803A (贪心+实现)

    题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典 ...

  7. CodeForces 313C Ilya and Matrix

    Ilya and Matrix Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  8. Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  9. codeforces C. Ilya and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/313/C 题目意思:给定 4n 个整数(可以组成 2n × 2n 大小的矩阵),问通过对这些整数进行排列, ...

随机推荐

  1. 基于.NET架构的树形动态报表设计与应用

    在一些统计报表中,利用树形结构报表来实现维度钻取功能是十分常见的.通过逐级钻取,可以快速查看更细粒度的指标数据,如项目施工进度报告等. 使用葡萄城报表控件——ActiveReports ,即可轻松设计 ...

  2. ThinkPHP composer的安装,及image图像处理类库的加载

    以下教程针对windows系统,示例系统使用win7 composer安装 下载composer安装包,点击安装. 出现'composer choose the command-line php' 要 ...

  3. Vue 表情输入组件,微信face表情组件

    VUE表情包输入组件,先来张成品图看看. 年底了没事干,把以前做过的项目中的组件拿出来再复习一下, 先说说思路吧. 注意: 1. 项目是用vue-cli3.0搭建起来的项目, 参考cli3.0官网地址 ...

  4. oracle修改TNSLSNR的端口

    oracle 服务一启动 TNSLSNR.exe 会占用8080端口,这时,如果我们其他程序需要使用8080端口就会比较麻烦,所以需要改一下端口: 用dba账户登录 CMD>sqlplus sy ...

  5. Boot-crm管理系统开发教程(三)

    (ps:前两章我们已经把管理员登录和查看用户的功能实现了,那么今天我们将要实现:添加用户,删除用户,和修改用户功能) 由于Cusomer的POJO类型已经写好了,所以这次我们之前从CustomerCo ...

  6. List与Set区别

    List: 元素有序放入,元素可重复 Set: 元素无序保存,元素不可重复(通过==判断,非基本类型判断的是引用地址),因为set是无序的,故只能通过迭代器循环.ps:说是无序,但是其实set中的元素 ...

  7. 学生管理系统利用arrayList第二次优化

    package StuManage; public class Student { private String name;//姓名 private String stuNum;//学号 privat ...

  8. opencv 模板匹配, 已解决模板过大程序不工作的bug

    #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv ...

  9. LCN分布式事务管理(一)

    前言 好久没写东西了,9月份换了份工作,一上来就忙的要死.根本没时间学东西,好在新公司的新项目里面遇到了之前没遇到过的难题.那遇到难题就要想办法解决咯,一个请求,调用两个服务,同时操作更新两个数据库. ...

  10. vue导航栏制作

    1,在components新建commnn目录,然后再新建nav目录,在此目录下新建nav-bottom.vue文件和nav-item.vue文件 2,nav-bottom.vue中的内容: < ...