计算机程序设计竞赛艺术(单调栈扩展)

计算机程序设计竞赛艺术
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

Viewer discretion is advised. The following problem is very academic and that should not be understood by anyone.
An undirected graph is a graph in which the nodes are connected by undirected arcs.
An undirected arc is an edge that has no direction. Both ends of an undirected arc are equivalent. We denote an undirected arc between node A and B as A-B.
A directed graph is a graph in which the nodes are connected by directed arcs.
A directed arc is an edge that has direction. One of the endpoints of a directed arc is called the head endpoint and the other is called tail endpoint. We denote a directed arc between node A and B As A->B, in which A is head endpoint and B is the tail endpoint.
For a node, the number of head endpoints adjacent to a node is called the indegree of the node.
An orientation of an undirected graph is a transformation which turns the graph into a directed graph by assigning each arc a direction. We notate the original undirected graph as G:=(V,E) and the transformed directed graph as G':=(v',E').
A transitive orientation of an undirected graph is an orientation which satifies the following constraint:
For any three distinct nodes A,B and C
由A->B属于E'并且B->C属于E'推出A->C属于E'.
An undirected graph is called a comparability graph iff it has a transitive orientation.
It was proved in 1962 that a graph is a comparability graph iff it does not contain a 2-chordless odd cycle.
In 1977 an algorithm was designed to check if a given graph is comparability graph with complexity O(d*|E|), where d is the maximum degree of nodes. And in 2000 a paper presented a new algorithm solving the same problem in O(|E|*log2|V|). Both algorithm return a transitive orientation of the inputted graph if it is found to be comparability graph.
A complete graph is an undirected graph in which each pair of nodes is connected by an edge. It is trivial that all complete graphs are comparability graphs, because complete graths never contain chordless cycle.
ICTOP(indegree-constrained-transitive-orientation-problem) is to find a transitive orientation of an undirected graph under the upper-bound indegree of each node. In the case that such orientation doesn't exist, the problem aims at finding the transitive orientation which maximize the number of nodes whose indegree doesn't exceed the corresponding upper-bound. In the case that no transitive orientation exists, the problem aims at outputting "Oops" in a single line.
However, here comes the problem.
Z is so stupid that he doesn't understand the above bullshit at all. To spare his professional time on the contest, he decides to draw pictures on the test paper.
As you can see, the test paper is full of bullshit and there's just too little room left for painting arts. Z wonders if he could have enough room for an X*Y size painting. To generalize the problem, he divides the test paper into N*M grids. Each grid is either empty or occupied. He wonders if there's an X*Y sub-grid which contains no occupied grid. Can you help him?

Input

First line of the input contains a positive integer, indicating the number of test cases.
Each test case starts with a line containing 2 integers N and M(1<=N,M<=1000)
The next N lines each contain M integers. Each integer is either 0, which means the corresponding grid is empty, or 1, which means it's occupied.
Next line is an integer K(K<=1000000), indicating the number of Z's queries.
Each of the next K lines contains two integers A and B, which means Z wonders if there's an empty sub-grid fo size A*B(1<=A<=N;1<=B<=M).
Note that query is just asking question. Z will not draw anything during his query stage. So the test paper remains the same for every query in a test case.

Output

For each query, just output a line containing "Y" if the corresponding empty A*B sub-grid can be found, or "N" otherwise.

Sample Input

1
3 3
1 1 1
0 0 0
1 0 0
5
1 1
1 3
3 1
2 2
2 3

Sample Output

Y
Y
N
Y
N

Author

HYNU
 
题目说了一大堆,其实主要就是后面的有用,给定一个n*m的矩阵和k个询问,问是否存在一个全为0的子矩阵。是输出Y,否则输出N。
思路是预处理出所有满足条件的子矩阵,然后查询的时候就可以在O(1)时间得出,预处理跟最大矩形那题类似,也是枚举每一行,每行上方就是一个最大矩形问题,然后不用更新最大值,只是用一个标记数组把把这个矩形标记为1即可,表示这个矩形存在,这样处理完后只是得出了大矩形的位置,但是还需要更新小的矩形,这样在枚举完后我单独用了两重循环遍历,把隐藏的求出来,就可以查询了。
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int N = 1000 + 10;
int flag[N][N],map[N][N],f[N],ff[N];
void solve(int ff[],int m)
{
    stack<int>s;
    int i,l,k,temp;
    ff[m+1]=-1;
    for(i=1;i<=m+1;i++)
    {
        if(s.empty()||ff[i]>ff[s.top()])
        {
            s.push(i);
        }
        else if(ff[i]<ff[s.top()])
        {
            while(!s.empty()&&ff[i]<ff[s.top()])
            {
            flag[ff[s.top()]][i-s.top()]=1;
            //printf("%d %d\n",ff[s.top()],i-s.top());
            temp=s.top();
            s.pop();
            }
        s.push(temp);
        ff[temp]=ff[i];
        }
    }
}
int main()
{
    //freopen("a.txt","r",stdin);
    int t,n,m,k,a,b,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            scanf("%d",&map[i][j]);
        memset(flag,0,sizeof(flag));
        memset(f,0,sizeof(f));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
                if(map[i][j]==0) f[j]++;
                else f[j]=0;

            memcpy(ff,f,sizeof(f));
            solve(ff,m);
        }
        /*for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
                printf("%d ",flag[i][j]);
            printf("\n");
        }*/
        for(i=n;i>=1;i--)  //遍历出小的矩形,并且是从后往前,一步步求出。
        {
            for(j=m;j>=1;j--)
                flag[i][j]=flag[i][j]||flag[i+1][j]||flag[i][j+1];
        }
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d%d",&a,&b);
            if(flag[a][b]) printf("Y\n");
            else printf("N\n");
        }
    }
    return 0;
}

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值