C. Skyscrapers
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by nn streets along the Eastern direction and mm streets across the Southern direction. Naturally, this city has nmnm intersections. At any intersection of ii-th Eastern street and jj-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings.
When Dora passes through the intersection of the ii-th Eastern and jj-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change.
Formally, on every of nmnm intersections Dora solves an independent problem. She sees n+m−1n+m−1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer xx and assign every skyscraper a height from 11 to xx. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible xx.
For example, if the intersection and the two streets corresponding to it look as follows:
Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved)
The largest used number is 55, hence the answer for this intersection would be 55.
Help Dora to compute the answers for each intersection.
Input
The first line contains two integers nn and mm (1≤n,m≤10001≤n,m≤1000) — the number of streets going in the Eastern direction and the number of the streets going in Southern direction.
Each of the following nn lines contains mm integers ai,1ai,1, ai,2ai,2, ..., ai,mai,m (1≤ai,j≤1091≤ai,j≤109). The integer ai,jai,j, located on jj-th position in the ii-th line denotes the height of the skyscraper at the intersection of the ii-th Eastern street and jj-th Southern direction.
Output
Print nn lines containing mm integers each. The integer xi,jxi,j, located on jj-th position inside the ii-th line is an answer for the problem at the intersection of ii-th Eastern street and jj-th Southern street.
题意:给一个n行m列的矩阵,对于矩阵中的每一个点(i,j),改变这个点所在行的元素的数值,使这一行元素的相对大小不变,改变这个点所在列的元素的数值,使这一列元素的相对大小不变,使该点所在行列的最大值最小。
分析:按照题意来讲就是离散化,但要保证行列交叉点的数值是一样的,那么就不能分别离散化。
对于点(i,j),求出所在行严格大于和严格小于它的点的个数,设为big1,sma1,所在列严格大于和严格小于它的点的个数,设为big2,sma2,那么该点所在行列全部离散化之后的最大值等于max(big1,big2)+max(sma1+sma2)+1。复杂度O(n^2logn)。
#include
using namespace std;
int a[1004][1004];
int big1[1004][1004];//行
int big2[1004][1004];//列
int sma1[1004][1004];
int sma2[1004][1004];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d",&a[i][j]);
}
}
vectorv;
for (int i = 1; i <= n; ++i) {
v.clear();
for (int j = 1; j <= m; ++j) {
v.push_back(a[i][j]);
}
sort(v.begin(),v.end());
int end=unique(v.begin(),v.end())-v.begin();
for (int j = 1; j <= m; ++j) {
big1[i][j]=end-(int)((upper_bound(v.begin(),v.begin()+end,a[i][j]))-v.begin());
sma1[i][j]=lower_bound(v.begin(),v.begin()+end,a[i][j])-v.begin();
}
}
for (int i = 1; i <= m; ++i) {
v.clear();
for (int j = 1; j <= n; ++j) {
v.push_back(a[j][i]);
}
sort(v.begin(),v.end());
int end=unique(v.begin(),v.end())-v.begin();
for (int j = 1; j <= n; ++j) {
big2[j][i]=end-(int)((upper_bound(v.begin(),v.begin()+end,a[j][i]))-v.begin());
sma2[j][i]=lower_bound(v.begin(),v.begin()+end,a[j][i])-v.begin();
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
printf("%d",1+max(sma1[i][j],sma2[i][j])+max(big1[i][j],big2[i][j]));
if(j==m)puts("");
else printf(" ");
}
}
}