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. DOM、BOM

    DOM DOM 是 W3C(万维网联盟)的标准. DOM 定义了访问 HTML 和 XML 文档的标准: “W3C 文档对象模型 (DOM) 是中立于平台和语言的接口,它允许程序和脚本动态地访问和更新 ...

  2. CString VariantToString(VARIANT var);转换

    Variant类型转换成CString代码 对数据库的操作常常会用Variant和_bstr_t类型,在进行MFC等程序中,通常会用到CString类.从variant转换成CString代码如下:v ...

  3. 小白必看的Python爬虫流程

    定义: 网络爬虫(Web Spider),又被称为网页蜘蛛,是一种按照一定的规则,自动地抓取网站信息的程序或者脚本. 简介: 网络蜘蛛是一个很形象的名字.如果把互联网比喻成一个蜘蛛网,那么Spider ...

  4. linux内核钩子--khook

    简介 本文介绍github上的一个项目khook,一个可以在内核中增加钩子函数的框架,支持x86.项目地址在这里:https://github.com/milabs/khook 本文先简单介绍钩子函数 ...

  5. Android中如何判断内存卡是否存在

    if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { /* 得到SD卡得路 ...

  6. Java 实现 海康摄像头抓拍图像 Windows、Linux

    先抱怨一下,打死都想不到,海康的摄像头SDK居然是一个Java类,还有必须的两个jar包(jna.jar,examples.jar).鬼能想得到会这么命名. 下面开始吧. Windows 把从官网下载 ...

  7. SSH远程连接工具汇总

    1)Xshell 常见问题: 1) 终端中的字体横向显示 字体中带有@的均为横向字体, 只要选择一个不带@的字体即可 2)putty 常见问题: 1)putty中编辑脚本,文字呈现蓝色,辨识度较差,需 ...

  8. 【Git的基本操作一】文件初始化及设置签名

    1. 本地库初始化 命令: git init 效果: 

  9. 简单聊聊服务发现(redis, zk,etcd, consul)(转载)

    服务发现并没有怎样的高深莫测,它的原理再简单不过.只是市面上太多文章将服务发现的难度妖魔化,读者被绕的云里雾里,顿觉自己智商低下不敢高攀. 服务提供者是什么,简单点说就是一个HTTP服务器,提供了AP ...

  10. 使用docker compose部署服务

    谈到微服务的话题,技术上我们往往会涉及到多服务.多容器的部署与管理. Docker 有三个主要的作用:Build, Ship和Run.使用docker compose我们可以在Run的层面解决很多实际 ...