Codeforces Round #545 (Div. 2) C. Skyscrapers (离散化)

题目传送门

题意:

给你n*m个点,每个点有高度h [ i ][ j ] ,用[1,x][1,x]的数对该元素所处十字上的所有元素重新标号,

并保持它们的相对大小不变。n,m1000n,m≤1000 ,求其最小标号的最大值。

思路:

把行列离散化一下,然后答案是把十字上的所有数相对大小的最大值

代码:

#include<bits/stdc++.h>
using namespace std;
#define N 1005
int n,m;
vector<int>r[N],c[N];
int h[N][N];

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1;i<=n;i++) r[i].clear();
        for(int j=1;j<=m;j++) c[j].clear();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
              scanf("%d",&h[i][j]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                r[i].push_back(h[i][j]);
            sort(r[i].begin(),r[i].end());
            r[i].erase(unique(r[i].begin(),r[i].end()),r[i].end());
        }
        for(int j=1;j<=m;j++)
        {
            for(int i=1;i<=n;i++)
                c[j].push_back(h[i][j]);
            sort(c[j].begin(),c[j].end());
            c[j].erase(unique(c[j].begin(),c[j].end()),c[j].end());
        }
       /* for(int i=1;i<=n;i++)
        {
            for(int j=0;j<r[i].size();j++)
                cout<<r[i][j]<<" ";
            cout<<endl;
        }*/
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                int tp1=lower_bound(r[i].begin(),r[i].end(),h[i][j])-r[i].begin();
                int tp2=lower_bound(c[j].begin(),c[j].end(),h[i][j])-c[j].begin();
                int tp3=r[i].end()-lower_bound(r[i].begin(),r[i].end(),h[i][j]);
                int tp4=c[j].end()-lower_bound(c[j].begin(),c[j].end(),h[i][j]);
                printf("%d ",max(tp1,tp2)+max(tp3,tp4));
            }
            printf("\n");
        }
    }
    return 0;
}
View Code

从中也学习了一下unique的用法

链接

参考博客:https://www.cnblogs.com/antiquality/p/10501112.html#_label2_0

猜你喜欢

转载自www.cnblogs.com/zhgyki/p/10555839.html