Time limit : 2sec / Memory limit : 256MB
Score : 500 points
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 xi, yi 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?
B
or W
.Input is given from Standard Input in the following format:
N K
x1 y1 c1
x2 y2 c2
:
xN yN cN
Print the maximum number of desires that can be satisfied at the same time.
Copy
4 3
0 1 W
1 2 W
5 3 B
5 4 B
Copy
4
He can satisfy all his desires by painting as shown in the example above.
Copy
2 1000
0 0 B
0 1 W
Copy
2
Copy
6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W
Copy
4
题意:在一个黑块和白块交错的二维空间里(每个黑块和白块的边长为k),有一个二维直角坐标系(原点不确定),现在有n个点,每个点有相应的颜色,问最多有多少个点颜色正确。
例如,最后一组数据的图为如下(黄色为黑底)
这题做法要比较巧的方法,否则会超时,我的复杂度为O(n+5*k*k),也不知道各位大佬有没有更加快的方法
1
我们可以发现一个完整的黑白相间的图形是由2k为边长的正方形组成的
所以我把所有的点先对他们的x%2k ,y%2k处理一下 ,分不同颜色标记在w[][]和b[][]
这里所花费时间为n
然后dp,w[i][j]代表i*j这个长方形中有多少个白色的点
b[i][j]代表i*j这个长方形中有多少个黑色的点
这里所花费时间为4*k*k
2
我们不知道原点,那就暴力所有可能的情况,你会发现只需要沿x暴力k次,沿y暴力k次,然后我们会发现如下情况(假设k=4)
这是最优状况(黄色不一定代表是黑色的,也有可以是白色的)
这是一般情况
所以可以说s1=五块黑色中所存在的点(可以利用b[][]算出)+四块白色中所存在的点
(反着来) s2=五块白色中所存在的点(可以利用w[][]算出)+四块黑色中所存在的点
不存在的块点数为0
s=Max(s1,s2)
这里所花费时间为k*k
乎,写的时候比较小心,一次AC
#include
#include
using namespace std;
int w[2005][2005],b[2005][2005];
int jisuan(int x1,int y1,int x2,int y2,int t[][2005]){
if(x1>x2||y1>y2)
return 0;
return t[x2][y2]-t[x1-1][y2]-t[x2][y1-1]+t[x1-1][y1-1];
}
int main() {
int n,k,x,y;
char z;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d%d %c",&x,&y,&z);
if(z=='W')
w[x%(2*k)+1][y%(2*k)+1]++;
else
b[x%(2*k)+1][y%(2*k)+1]++;
}
for(int i=1;i<=2*k;i++)
for (int j=1;j<=2*k ;j++) {
w[i][j]=w[i-1][j]-w[i-1][j-1]+w[i][j-1]+w[i][j];
b[i][j]=b[i-1][j]-b[i-1][j-1]+b[i][j-1]+b[i][j];
}
int mmax=0;
int t1,t2;
for( x=0;x