AtCoder Regular Contest 089 D Checker
2018-01-22 14:05
489 查看
D - Checker
Time limit : 2sec / Memory limit : 256MBScore : 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 ≤ 1051 ≤ K ≤ 1000
0 ≤ xi ≤
4000
109
0 ≤ yi ≤ 109
If i ≠ j,
then (xi,yi) ≠ (xj,yj).
ci is
Bor
W.
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
Copy4 3 0 1 W 1 2 W 5 3 B 5 4 B
Sample Output 1
Copy4
He can satisfy all his desires by painting as shown in the example above.
Sample Input 2
Copy2 1000 0 0 B 0 1 W
Sample Output 2
Copy2
Sample Input 3
Copy6 21 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W
Sample Output 3
Copy4
题目大意:给你一个K*K的无限格子图,你可以自由移动格子图。然后给你N个要求:坐标Xn Yn 处为黑("B")或白("B"),问你有几个请求可以实现。
通过观察可以发现点(x,y)和点(x,y+2k),(x+2k,y)是等价的,要求(x,y)处是某种颜色和在(x,y-k),(x-k,y)处是它的反色等价,读入数据时可以把数据整理下,先整理成一种颜色,然后把x和y全部MOD(2*k)。由于给了两秒,所以第一印象就是穷举所有的(x,y)(0<=x,y<k)点为格子图白色和黑色的右下角起点,判断最多的满足要求数目。有注意到在(x,y)处放黑色块和放白色块也是可以转换的,因为假设放黑色块满足的数目是ans,那么必然有N-ans个请求是不满足的,也就是说这些请求请求的是某色但实际是它的反色。所以当你把放黑块改成放白块后,这些请求就满足了,但之前满足的请求就不满足了。所以对于每一种(x,y)情况取ans和N-ans即可。
然后一波暴力过去,结果超时....
Test case Case name Status Exec time Memory usage 0_000.txt AC 6 ms 16640 KB 0_001.txt AC 35 ms 16640 KB 0_002.txt AC 6 ms 16640 KB 1_003.txt AC 22 ms 16640 KB 1_004.txt AC 6 ms 16640 KB 1_005.txt AC 6 ms 16640 KB 1_006.txt AC 6 ms 16640 KB 1_007.txt AC 6 ms 16640 KB 1_008.txt AC 22 ms 16640 KB 1_009.txt AC 6 ms 16640 KB 1_010.txt AC 6 ms 16640 KB 1_011.txt AC 7 ms 16640 KB 1_012.txt AC 315 ms 16640 KB 1_013.txt TLE 1_014.txt AC 36 ms 17536 KB 1_015.txt AC 49 ms 17536 KB 1_016.txt AC 142 ms 17536 KB 1_017.txt TLE 1_018.txt TLE 1_019.txt AC 1459 ms 16768 KB 1_020.txt AC 1459 ms 16768 KB 1_021.txt AC 1449 ms 16768 KB 1_022.txt AC 1453 ms 16768 KB 1_023.txt AC 1438 ms 16768 9ab1 KB 1_024.txt AC 1441 ms 16768 KB 1_025.txt AC 390 ms 17536 KB 1_026.txt TLE 1_027.txt AC 358 ms 17536 KB 1_028.txt TLE 1_029.txt AC 393 ms 17536 KB 1_030.txt TLE
超时原因很显然,这种方法是O(N*K*K)的。对于每种情况都要把所有N个请求比较下还是比较蠢的,所以可以一开始就把每个(x,y)到(0,0)的矩形内的要求总数算出来,然后对于每个枚举情况下的(x,y)用类似计算面积的方法统计满足的个数就能直接出答案。
AC代码#include<bits/stdc++.h> using namespace std; int xx; int yy; int lt[2050][2050]; int main () { memset(lt,0,sizeof(lt)); int n,k,x,y; char c[10]; cin>>n>>k; for(int i=0; i<n; i++) { scanf("%d %d %s",&xx,&yy,c); if(c[0]=='W') { yy+=k; } xx%=2*k; yy%=2*k; lt[xx][yy]++; } for(int i=0; i<=2*k; i++) for(int j=1; j<=2*k; j++) lt[i][j]+=lt[i][j-1]; for(int i=1; i<=2*k; i++) for(int j=0; j<=2*k; j++) lt[i][j]+=lt[i-1][j]; int ans=0; for(int a=1; a<=k; a++) { for(int b=1; b<=k; b++) { int c1=lt[b-1][a-1]; int c2=lt[b-1][2*k-1]-lt[b-1][k+a-1]; int c3=lt[2*k-1][a-1]-lt[k+b-1][a-1]; int c4=lt[2*k-1][2*k-1]-lt[k+b-1][2*k-1]-lt[2*k-1][k+a-1]+lt[k+b-1][a+k-1]; int c5=lt[k+b-1][a+k-1]-lt[b-1][a+k-1]-lt[b+k-1][a-1]+lt[b-1][a-1]; ans=max(ans,c1+c2+c3+c4+c5); ans=max(ans,n-(c1+c2+c3+c4+c5)); } } printf("%d\n",ans); return 0; }
Test case Case name Status Exec time Memory usage 0_000.txt AC 5 ms 16640 KB 0_001.txt AC 29 ms 16640 KB 0_002.txt AC 5 ms 16640 KB 1_003.txt AC 29 ms 16640 KB 1_004.txt AC 5 ms 16640 KB 1_005.txt AC 5 ms 16640 KB 1_006.txt AC 5 ms 16640 KB 1_007.txt AC 5 ms 16640 KB 1_008.txt AC 30 ms 16640 KB 1_009.txt AC 6 ms 16640 KB 1_010.txt AC 6 ms 16640 KB 1_011.txt AC 6 ms 16640 KB 1_012.txt AC 6 ms 16640 KB 1_013.txt AC 30 ms 16640 KB 1_014.txt AC 32 ms 16640 KB 1_015.txt AC 32 ms 16640 KB 1_016.txt AC 32 ms 16640 KB 1_017.txt AC 33 ms 16640 KB 1_018.txt AC 57 ms 16640 KB 1_019.txt AC 8 ms 16640 KB 1_020.txt AC 7 ms 16640 KB 1_021.txt AC 8 ms 16640 KB 1_022.txt AC 7 ms 16640 KB 1_023.txt AC 8 ms 16640 KB 1_024.txt AC 8 ms 16640 KB 1_025.txt AC 28 ms 16640 KB 1_026.txt AC 53 ms 16640 KB 1_027.txt AC 28 ms 16640 KB 1_028.txt AC 52 ms 16640 KB 1_029.txt AC 28 ms 16640 KB 1_030.txt AC 52 ms 16640 KB
相关文章推荐
- AtCoder Regular Contest 089 D - Checker 思维题、点的转移、二维前缀和
- AtCoder Regular Contest 089 D - Checker
- AtCoder Regular Contest 089 C - Traveling
- AtCoder Regular Contest 089
- AtCoder Regular Contest 089 E-GraphXY-构造题
- AtCoder Regular Contest 076 F - Exhausted? 霍尔定理+线段树
- AtCoder Regular Contest 082 E - ConvexScore 乱搞
- AtCoder Regular Contest D - Remainder Reminder 取余问题
- AtCoder Regular Contest 091 E - LISDL
- AtCoder Regular Contest 092 C - 2D Plane 2N Points 贪心 匈牙利算法模板
- AtCoder Regular Contest 093(E-Bichrome Spanning Tree)
- AtCoder Regular Contest 098 F.Donation
- AtCoder Regular Contest 073 D - Simple Knapsack(贪心)
- AtCoder Regular Contest 074 瞎打记
- AtCoder Regular Contest 078 C
- AtCoder Regular Contest 079-C - Cat Snuke and a Voyage
- 【递归】【线段树】【堆】AtCoder Regular Contest 080 E - Young Maids
- AtCoder Regular Contest 080 CD题
- Atcoder Regular Contest 081 总结
- Atcoder Regular Contest 084