すぬけ君の塗り絵 / Snuke's Coloring(AtCoder-2068)
Problem Description
We have a grid with?H?rows and?W?columns. At first, all cells were painted white.
Snuke painted?N?of these cells. The?i-th (?1≤i≤N?) cell he painted is the cell at the?ai-th row and?bi-th column.
Compute the following:
For each integer?j?(?0≤j≤9?), how many subrectangles of size?3×3?of the grid contains exactly?j?black cells, after Snuke painted?N?cells?
Constraints
- 3≤H≤109
- 3≤W≤109
- 0≤N≤min(105,H×W)
- 1≤ai≤H?(1≤i≤N)
- 1≤bi≤W?(1≤i≤N)
- (ai,bi)≠(aj,bj)?(i≠j)
Input
The input is given from Standard Input in the following format:
H W N
a1 b1
:
aN bN
Output
Print 10 lines. The (j+1)-th ( 0≤j≤9 ) line should contain the number of the subrectangles of size 3×3 of the grid that contains exactly j black cells.
Example
Sample Input 1
4 5 8
1 1
1 4
1 5
2 3
3 1
3 2
3 4
4 4
Sample Output 1
0
0
0
2
4
0
0
0
0
0
There are six subrectangles of size 3×3. Two of them contain three black cells each, and the remaining four contain four black cells each.
Sample Input 2
10 10 20
1 1
1 4
1 9
2 5
3 10
4 2
4 7
5 9
6 4
6 6
6 7
7 1
7 3
7 7
8 1
8 5
8 10
9 2
10 4
10 9
Sample Output 2
4
26
22
10
2
0
0
0
0
0
Sample Input 3
1000000000 1000000000 0
Sample Output 3
999999996000000004
0
0
0
0
0
0
0
0
0
題意:給一個長為 H 寬為 W 的網狀矩形,開始時所有的格子都是白色的,現在給出 n ?個格子的坐標,要把他們涂黑,將格子涂黑后,依次輸出有多少個 3*3 大小的子矩形包含 0~9 個黑色格子
思路:
由于 h、w 極大,暴力一定會超時,注意到要涂黑的 n 個格子最大為 1E5,那么可以從涂黑的格子考慮
注意到每個涂黑的格子在以其為中心的 3*3 矩形中貢獻了 1 個黑色,那么可以枚舉所有的黑色格子,統計其貢獻度,即以其為中心 3*3 的矩形貢獻度+1,然后再去枚舉所有涉及到的格子,由于 n 最大為 1E5,那么最壞情況下只需枚舉 1E5*9 個格子
可以發現,對于給定的 h、w 的矩形,其有 (h-2)*(w-2) 個 3*3 的矩形,即小矩形的中心不可能在矩形的邊上,故當統計完貢獻度后,枚舉所有不在矩形邊上的格子,將其貢獻度記錄到桶中,并記錄所有涉及到的總數,就是 3*3 矩形中存在 1~9 個格子的個數
最后再用總的矩形個數減去 1~9 個黑格個數就是 0 個黑格個數
由于要統計出現過的格子的坐標,使用 map+pair 極為方便
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 1000000+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std; LL bucket[10]; pair<int,int> node[N]; map<pair<int,int>,int> mp; int main(){LL h,w,n;scanf("%lld%lld%lld",&h,&w,&n);for(int i=1;i<=n;i++){int x,y;scanf("%d%d",&node[i].first,&node[i].second);mp[node[i]]=1;//涂黑的格子貢獻度為1}for(int i=1;i<=n;i++){for(int j=0;j<8;j++){//統計涂黑的格子周圍格子的貢獻度int nx=node[i].first+dx[j];int ny=node[i].second+dy[j];if(nx>=1&&nx<=h&&ny>=1&ny<=w){//越界判斷pair<int,int> temp(nx,ny);mp[temp]++;}}}LL sum=0;LL tot=(h-2)*(w-2);//總共的矩形數map<pair<int,int>,int>::iterator it;for(it=mp.begin();it!=mp.end();it++){//枚舉所有涉及到的格子int x=it->first.first;int y=it->first.second;int val=it->second;if(x>=2&&x<=h-1&&y>=2&y<=w-1){//統計除去最外一圈的格子bucket[val]++;sum++;}}bucket[0]=tot-sum;//為0的數量是總共的矩形減去所有非零的矩形for(int i=0;i<=9;i++)printf("%lld\n",bucket[i]);return 0; }?
總結
以上是生活随笔為你收集整理的すぬけ君の塗り絵 / Snuke's Coloring(AtCoder-2068)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACM 常用算法合集
- 下一篇: 剪花布条(HDU-2087)