Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K. Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square. Below is an example of a checked pattern of side 3:

AtCoDeer has N desires. The i-th desire is represented by xiyi and ci. If ci is B, it means that he wants to paint the square (xi,yi) black; if ci is W, he wants to paint the square (xi,yi) white. At most how many desires can he satisfy at the same time?

Constraints

  • 1  N  105
  • 1  K  1000
  • 0  xi  109
  • 0  yi  109
  • If i  j, then (xi,yi)  (xj,yj).
  • ci is B or W.
  • NKxi and yi are integers.

Input

Input is given from Standard Input in the following format:

N K
x1 y1 c1
x2 y2 c2
:
xN yN cN

Output

Print the maximum number of desires that can be satisfied at the same time.


Sample Input 1

Copy
4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

Copy
4

He can satisfy all his desires by painting as shown in the example above.


Sample Input 2

Copy
2 1000
0 0 B
0 1 W

Sample Output 2

Copy
2

Sample Input 3

Copy
6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

Copy
4

这道题数据挺大的,首先是移动到2k*2k的框框里,不影响坐标(也就是横纵坐标都移动2k的整数倍,如果只移动k的整数倍不能保证颜色不变)。
最初先默认数黑色或者白色都可以,这里先默认数黑色,白色的通过变色移动(横或纵坐标+k)映射到黑色的位置,然后整个图里只剩下黑色的。
最后求出结果,只需要取sum 和 n - sum中大的那个就行了,因为默认是黑色,黑白可以互换。
然后是移动k宫格在不同位置,一共有k*k种情况,直接在2k*2k的框框里操作,需要计算二维前缀和,之前是一个一个点去判断超时了,想想也不能一味的暴力啊,肯定得用点妙招,然后移动x轴,计算一维前缀和时间勉强没超。
k宫格是从左下角开始移动就是从(0,0)开始,需要记录的是k宫格里黑色的个数,以及斜对角的矩形里黑色的个数,因为斜对角的颜色相同,横竖相邻的区域颜色相异。
计算二维前缀和需要用离散学的容斥定理,最后求单独区域需要前缀和相减,也要用到容斥定理。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip> using namespace std; int v[][];
int n,k;
int num(int x1,int y1,int x2,int y2){
return v[x2][y2] - v[x2][y1] - v[x1][y2] + v[x1][y1];///前缀和 减去左边 下边和左下边的k宫格前缀和 容斥定理:左边和下边都包含了左下边 需要加上一个左下边
}
int main()
{
int x,y,ans = 0,sum;
char ch;
scanf("%d%d",&n,&k);
for(int i = ;i <= n;i ++){
scanf("%d%d %c",&x,&y,&ch);
x %= * k;///把所有点都移动到 2k * 2k 的区域
y %= * k;
x += k * (ch == 'W');///x可以变成y W 也可以变成 B 这里白色都变成黑色的 如果转换后的黑色是满足的那么原来的白色也一定是满足的
v[x % ( * k) + ][y % ( * k) + ] ++;///求前缀和要求从(1,1)开始
}
for(int i = ;i <= * k;i ++)///求前缀和
for(int j = ;j <= * k;j ++)
v[i][j] += v[i - ][j] + v[i][j - ] - v[i - ][j - ];///加上左边下边和左下角的和 容斥定理:左边和下边的都包含了左下边的 要减去一个左下边的for(int i = ;i <= k;i++)///k * k个格子依次做为起点构成新的k宫格 也就是移动k宫格 看看有几个黑色点包含在内 (默认黑色 可互换)
for(int j = ;j <= k;j++)
{
///斜对角的k宫格是相同颜色 2k * 2k区域最多有五个这样的区域
sum = num(,,i,j) + num(i,j,k + i,k + j) + num(k + i,k + j, * k, * k) + num(k + i,, * k,j) + num(,k + j,i, * k);
ans = max(ans,max(sum,n - sum));///黑白色可以互换
}
printf("%d\n",ans);
}

AtCoder Beginner Contest 086 D - Checker的更多相关文章

  1. AtCoder Beginner Contest 086 (ABCD)

    A - Product 题目链接:https://abc086.contest.atcoder.jp/tasks/abc086_a Time limit : 2sec / Memory limit : ...

  2. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  3. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  4. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  7. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  8. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  9. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

随机推荐

  1. UI自动化--PageObjects(页面对象)

    核心的核心:减少了重复代码的数量,减少变更涉及面:做到如果UI发生更改,则只需在一个位置应用此修复程序. PageObject:将页面作为一个对象,进行封装,包括元素定位,封装获取各元素.操作的方法: ...

  2. PAT天梯赛L2-004 这是二叉搜索树吗【递归】

    L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...

  3. 扩展KMP算法小记

    参考来自<拓展kmp算法总结>:http://blog.csdn.net/dyx404514/article/details/41831947 扩展KMP解决的问题: 定义母串S和子串T, ...

  4. 怎么点击div之外的区域就隐藏这个div啊 找了很久,都没有很好解决

    方法一. <!DOCTYPE html><html><head><meta http-equiv="Content-Type" conte ...

  5. strut2的标签

         DIY部落 新闻中心 交流论坛 千寻搜索   点击浏览该栏目下的更多电子书  收藏本站   struts2标签详解 文章整理: www.diybl.com 文章来源: 网络 去论坛 建我的b ...

  6. SPOJ - DWARFLOG Manipulate Dwarfs 线段树+想法题;

    题意:给你2e5个矮人,编号1~N.有2e5个操作:操作1 读取x,y,交换编号为x,y的矮人.操作2 读取AB 判断编号为A,A+1····B的矮人是否连续(不必有序). 题解:首先用pos[i]保 ...

  7. PHP漏洞

    http://os.51cto.com/art/201204/328766.htm 针对PHP的网站主要存在下面几种攻击方式: 1.命令注入(Command Injection) 2.eval注入(E ...

  8. Linux执行Cron Job失败,在Shell sh下执行却能成功 - 环境变量?

    博客分类: Linux linuxcrontabpermissionetc/profile环境变量  一.我们常常碰到在shell下执行某个命令能够成功,比如执行一个java程序: java -jar ...

  9. UITableView _endCellAnimationsWithContext崩溃

    http://www.cocoachina.com/bbs/read.php?tid-315222.html 删除cell之前先把数据源也删除一条,直接删除cell会崩溃 下面是正确的姿势: cell ...

  10. 新建虚拟机_WIN7 32位系统

    准备工作:下载win7 32位纯净版镜像文件 大部分步骤与安装XP系统相似,此处只说明一下不同: 创建好虚拟机后启动有报错:CHS data ERROR,无法从CD/DVD启动 编辑虚拟机--> ...