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. Lucky Sorting(CodeForces-109D)【思维】

    题意:给出一组数,要求从小到大排序,并且排序的过程中,发生交换的两个数至少一个为幸运数(十进制位均为4或7),问能否在(2×n)次交换内完成排序,如果能,输出交换的方案(不要求步骤数最少). 思路:首 ...

  2. win32多线程: 线程创建与结束等待

    #include<Windows.h> #include<iostream> using namespace std; /*1.在启动一个线程之前,必须为线程编写一个全局的线程 ...

  3. X86逆向7:特殊窗体的破解思路

    本章我们来看两个案例,这两个案例同样使用爆破的方式破解,但是与其他的程序不同,这个程序没有弹窗,提示成功或失败使用的是图片或是一个类似图片的窗体,本章将学习两个新的API函数的使用技巧. ------ ...

  4. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

  5. js获取图片信息

    网络图片: fetch(item.path).then(function(res){ // 计算图片大小 return res.blob() }).then(function(data){ conso ...

  6. golang编写二叉树

    最近开始找golang 开发工程师职位,针对算法相关二叉树相关常用面试题搞一遍: package tree import (     "math"     "fmt&qu ...

  7. OpenStreetMap全球库

    https://www.loveyu.org/5344.html https://www.jianshu.com/p/957aa4a933d7 https://blog.csdn.net/mrib/a ...

  8. 安装.Net Framework 2.0出现 Error 25015

    ---------------------------Microsoft .NET Framework 2.0 安装程序---------------------------Error 25015.安 ...

  9. nps内网渗透利用

    0x00 前言 对比了比较多的代理工具,如ew流量不稳定,容易断:frsocks目前免杀,也是容易断.frp需要落地配置文件,不符合渗透的规则.reg正向的socks,速度比较慢,扫描是个问题.其实我 ...

  10. vue +echarts树状图

    <template> <div :class="className" :id="id" :style="{height:height ...