D - Checker
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 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?
Constraints
- 1 ≤ N ≤ 105
- 1 ≤ K ≤ 1000
- 0 ≤ xi ≤ 109
- 0 ≤ yi ≤ 109
- If i ≠ j, then (xi,yi) ≠ (xj,yj).
- ci is
B
orW
. - N, K, xi 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
題意:在一個黑塊和白塊交錯的二維空間裏(每個黑塊和白塊的邊長爲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 <cstdio>
#include <algorithm>
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<k;x++)
for(y=0;y<k;y++)
{
t1=jisuan(x+1,y+1,x+k, y+k,w)+jisuan(1,1,x,y,w)+jisuan(x+k+1, y+k+1,2*k,2*k,w)+jisuan(x+k+1, 1,2*k,y,w)+jisuan(1, k+y+1,x,2*k,w);//百色最多五種
t1+=jisuan(1, y+1, x, y+k, b)+jisuan(x+1,1,x+k, y, b)+jisuan(x+k+1, y+1, 2*k, y+k, b)+jisuan(x+1, y+k+1, x+k, 2*k, b);//黑色有4種
//反着來一下
t2=jisuan(x+1,y+1,x+k, y+k,b)+jisuan(1,1,x,y,b)+jisuan(x+k+1, y+k+1,2*k,2*k,b)+jisuan(x+k+1, 1,2*k,y,b)+jisuan(1, k+y+1,x,2*k,b);
t2+=jisuan(1, y+1, x, y+k, w)+jisuan(x+1,1,x+k, y, w)+jisuan(x+k+1, y+1, 2*k, y+k, w)+jisuan(x+1, y+k+1, x+k, 2*k, w);
mmax=max(t1, max(t2, mmax));
}
printf("%d\n",mmax);
return 0;
}