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. LEN()和DATALENGTH()的区别

    原文:LEN()和DATALENGTH()的区别 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.n ...

  2. 对Android应用签名

    Android使用包名作为唯一标识,当在同一台手机安装两个包名相同的应用,后安装的应用就会覆盖前面的应用(签名相同的情况下). 签名有两个主要作用: 1.确定发布者身份.由于应用开发者可以通过使用相同 ...

  3. Java设置时区

    TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));

  4. 使用原生node.js搭建HTTP服务器,支持MP4视频、图片传输,支持下载rar文件

    前言 如何安装node.js,如何搭建一个简易的http服务器我这里就不再赘述了,不懂的同学可以先去学习一下.当然了,我写的也就属于简易版的增强版,大家有什么高见的欢迎提出,然后进入正题. 目录结构 ...

  5. element-ui Cascader 级联选择器 点击label选中

    通过修改label的样式解决: 注意:el-cascader-panel 是直接挂载在body上的,所以需要全局设置 .el-cascader-panel .el-radio{ width: 100% ...

  6. python+opencv+sift环境配置教程

    最近在做对应点估计homography,需要用到opencv,c++的接口不如python的接口来的方便 但是在安装python接口的opencv的时候,遇到了各种问题,主要是函数找不到的问题 比如在 ...

  7. 我要研究一下minio,管理大量的照片

    随着年龄的增长,电脑里的照片越来越多了,管理和浏览也越来越困难了.

  8. IDEA springboot maven 项目部署

  9. 离线下载pytorch安装包

    1. 选择合适的安装包下载 https://anaconda.org/pytorch/repo?type=conda&label=main 2. 安装命令: conda install 安装包 ...

  10. linux网络协议栈(四)链路层 vlan处理

    转:http://blog.csdn.net/u010246947/article/details/18224517 4.6.VLAN处理: 4.6.1.vlan原理 对于带vlan的以太网报文,其以 ...