JZOJ.【USACO 2017 US Open Platinum】Problem 1 Modern Art题解

Description

小TY突然想画画,他有独特的艺术风格,他从N×N空白画布开始,其中0表示画布的空单元格。然后他会在画布上绘制恰好矩形,每个颜色是1到N×N中的一个。他每次可以选择任意一种未使用过的颜色进行绘画。例如,他可以从颜色2的矩形开始,画出这样的画布:

2 2 2 0

2 2 2 0

2 2 2 0

0 0 0 0

然后他可以用颜色7绘制一个矩形:

2 2 2 0

2 7 7 7

2 7 7 7

0 0 0 0

然后他可以在颜色3上绘制一个小矩形:

2 2 3 0

2 7 3 7

2 7 7 7

0 0 0 0

每个矩形都平行于画布边缘,而且矩形可以与整个画布一样大或者像一个单元一样小。每个颜色从1到正好使用一次,后来的颜色可能完全覆盖一些较早画上的颜色。

现在已知画布的最终状态,请计算有多少种颜色可能被第一个被画。
 

Input

The first line of input contains N , the size of the canvas ( 1 ≤ N ≤ 1000 ). The next N lines describe the final picture of the canvas, each containing N integers that are in the range 0 … N^2 . The input is guaranteed to have been drawn as described above, by painting successive rectangles in different colors.

Output

Please output a count of the number of colors that could have been drawn first.
 

Sample Input

4
2 2 3 0
2 7 3 7
2 7 7 7
0 0 0 0

Sample Output

14
 

Data Constraint

 
 

Hint

In this example, color 2 could have been the first to be painted. Color 3 clearly had to have been painted after color 7, and color 7 clearly had to have been painted after color 2. Since we don't see the other colors, we deduce that they also could have been painted first.

思路:

N^2种颜色,故ANS初值为N^2

求每种颜色的上下左右界限,以此得到左上角、右下角坐标

暴力枚举每种颜色区间,寻找其中的不同颜色(重叠),每当发现一种ANS-1

记得要标记每种颜色有没有重叠,每种颜色只能算一次

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int ans,n,a[1005][1005],cnt,ber[1000005][2],b[1000005];
bool bz=1;
struct node
{
    int u,d,l,r,c;
}c[1000005];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&a[i][j]);
            int col=a[i][j];
            if(col==0)continue;
            if(ber[col][0]==0)
            {
                ber[col][0]=1;
                ber[col][1]=++cnt;
                c[cnt].c=col;
                c[cnt].u=1000000000;
                c[cnt].l=1000000000;
            }
            c[ber[col][1]].u=min(i,c[ber[col][1]].u);
            c[ber[col][1]].d=max(i,c[ber[col][1]].d);
            c[ber[col][1]].l=min(j,c[ber[col][1]].l);
            c[ber[col][1]].r=max(j,c[ber[col][1]].r);
        }
    }
    ans=n*n;
    for(int i=1;i<=cnt;i++)
    {
        for(int x=c[i].u;x<=c[i].d;x++)
        {
            for(int y=c[i].l;y<=c[i].r;y++)
            {
                if(a[x][y]!=c[i].c&&b[a[x][y]]==0)
                {
                    ans--;
                    bz=0;
                    b[a[x][y]]=1;
                }
            }
        }
    }
    if(cnt==1)ans=n*n-1;
    else
    {
        if(bz)
        { 
            ans=n*n;
        }
    }
    printf("%d\n",ans);
}

 

posted @ 2020-05-23 17:19  HYDcn666_JZOJ  阅读(145)  评论(0编辑  收藏  举报