CodeForces 15D Map
5396 단어 codeforces
There is an area map that is a rectangular matrix n × m, each cell of the matrix contains the average height of a corresponding area part. Peter works for a company that has to build several cities within this area, each of the cities will occupy a rectangle a × b cells on the map. To start construction works in a particular place Peter needs to remove excess ground from the construction site where a new city will be built. To do so he chooses a cell of the minimum height within this site, and removes excess ground from other cells of the site down to this minimum level. Let's consider that to lower the ground level from h2 to h1 (h1 ≤ h2) they need to remove h2 - h1 ground units.
Let's call a site's position optimal, if the amount of the ground removed from this site is minimal compared to other possible positions. Peter constructs cities according to the following algorithm: from all the optimum site's positions he chooses the uppermost one. If this position is not unique, he chooses the leftmost one. Then he builds a city on this site. Peter repeats this process untill he can build at least one more city. For sure, he cannot carry out construction works on the occupied cells. Would you, please, help Peter place cities according to the algorithm?
Input
The first line contains four space-separated integers: map sizes n, m and city sizes a, b (1 ≤ a ≤ n ≤ 1000, 1 ≤ b ≤ m ≤ 1000). Then there follow n lines, each contains m non-negative space-separated numbers, describing the height matrix. Each number doesn't exceed 109.
Output
In the first line output k — the amount of constructed cities. In each of the following k lines output 3 space-separated numbers — the row number and the column number of the upper-left corner of a subsequent construction site, and the amount of the ground to remove from it. Output the sites in the order of their building up.
Sample Input
Input
2 2 1 2
1 2
3 5
Output
2
1 1 1
2 1 2
Input
4 4 2 2
1 5 3 4
2 7 6 1
1 1 2 2
2 2 1 2
Output
3
3 1 2
3 3 3
1 2 9
rmq , , , , 。
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
const int low(int x){return x&-x;}
const int maxn=1e3+10;
int n,m,a,b,h[maxn][maxn],tot;
int f[maxn][maxn],dp[maxn][20];
int flag[maxn][maxn];
LL sum[maxn][maxn];
int u[maxn*maxn];
struct point
{
int x,y;
LL c;
bool operator<(const point& a)const
{
return c==a.c?x==a.x?y<a.y:x<a.x:c<a.c;
}
point(int x=0,int y=0,LL c=0):x(x),y(y),c(c){};
}c[maxn*maxn];
int get(int x,int y)
{
int ans=0;
for (int i=x;i;i-=low(i))
for (int j=y;j;j-=low(j)) ans+=flag[i][j];
return ans;
}
void add(int x,int y,int z)
{
for (int i=x;i<=n;i+=low(i))
for (int j=y;j<=m;j+=low(j)) flag[i][j]+=z;
}
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&a,&b))
{
tot=0; memset(flag,0,sizeof(flag));
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
scanf("%d",&h[i][j]);
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+h[i][j];
dp[j][0]=h[i][j];
}
for (int k=1;(1<<k)<=b;k++)
{
for (int j=1;j<=m;j++)
{
dp[j][k]=min(dp[j][k-1],dp[j+(1<<k-1)][k-1]);
}
}
int k=log(1.0*b)/log(2.0);
for (int j=1;j<=m-b+1;j++)
{
f[i][j]=min(dp[j][k],dp[j+b-(1<<k)][k]);
}
}
for (int i=1;i<=m-b+1;i++)
{
for (int j=1;j<=n;j++) dp[j][0]=f[j][i];
for (int k=1;(1<<k)<=a;k++)
{
for (int j=1;j<=n;j++)
{
dp[j][k]=min(dp[j][k-1],dp[j+(1<<k-1)][k-1]);
}
}
int k=log(1.0*a)/log(2.0);
for (int j=1;j<=n-a+1;j++)
{
LL cost=min(dp[j][k],dp[j+a-(1<<k)][k]);
LL s=sum[j-1][i-1]+sum[j-1+a][i-1+b]-sum[j-1][i-1+b]-sum[j-1+a][i-1];
s-=cost*a*b;
u[tot]=0;
c[tot++]=point(j,i,s);
}
}
sort(c,c+tot);
int cnt=0;
for (int i=0;i<tot;i++)
{
if (get(c[i].x,c[i].y)) continue;
if (get(c[i].x+a-1,c[i].y)) continue;
if (get(c[i].x,c[i].y+b-1)) continue;
if (get(c[i].x+a-1,c[i].y+b-1)) continue;
u[i]=1; cnt++;
add(c[i].x,c[i].y,1);
add(c[i].x+a,c[i].y+b,1);
add(c[i].x,c[i].y+b,-1);
add(c[i].x+a,c[i].y,-1);
}
printf("%d
",cnt);
for (int i=0;i<tot;i++)
if (u[i]) printf("%d %d %lld
",c[i].x,c[i].y,c[i].c);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Codeforces Round #715 Div. 2C The Sports Festival: 구간 DP전형구간 DP의 초전형. 이하, 0-indexed. 입력을 정렬하여 어디서나 시작하고 최적으로 좌우로 계속 유지하면 좋다는 것을 알 수 있습니다. {2000})$의 주문이 된다. 우선, 입력을 소트하여 n개의 요소를 $...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.