POJ 1231 水题

来源:互联网 发布:discuz数据备份 编辑:程序博客网 时间:2024/06/05 07:11

提交数和AC人数都少的可怜,但是这是一道纯水题。。。可能是都不屑做这道题吧。。。


The Alphabet Game
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1042 Accepted: 416

Description

Little Dara has recently learned how to write a few letters of the English alphabet (say k letters). He plays a game with his little sister Sara. He draws a grid on a piece of paper and writes p instances of each of the k letters in the grid cells. He then asks Sara to draw as many side-to-side horizontal and/or vertical bold lines over the grid lines as she wishes, such that in each rectangle containing no bold line, there would be p instances of one letter or nothing. For example, consider the sheet given in Figure 1, where Sara has drawn two bold lines creating four rectangles meeting the condition above. Sara wins if she succeeds in drawing the required lines. Dara being quite fair to Sara, wants to make sure that there would be at least one solution to each case he offers Sara. You are to write a program to help Dara decide on the possibility of drawing the right lines. 

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case consists of two integers k (1 <= k <= 26), the number of different letters, and p (1 <= p <= 10), the number of instances of each letter. Followed by the first line, there are k lines, one for each letter, each containing p pairs of integers (xi, yi) for 1 <= i <= p. A pair indicates coordinates of the cell on the paper where one instance of the letter is written. The coordinates of the upper left cell of the paper is assumed to be (1,1). Coordinates are positive integers less than or equal to 1,000,000. You may assume that no cell contains more than one letter. 

Output

There should be one line per test case containing a single word YES or NO depending on whether the input paper can be divided successfully according to the constraints stated in the problem. 

Sample Input

23 26 4 8 44 2 2 12 3 2 43 31 1 3 1 5 12 1 4 1 6 12 2 4 2 8 1

Sample Output

YESNO

简而言之,就是看看如果用直线划分空间的话,能不能做到每个区域里只有一种数字


我们可以设置四个数组来r[], l[], u[], d[]来储存读入时覆盖没种数字的矩形的最右、左、上、下四个位置坐标。(可以认为r[]储存的是对第i个数来说,其右侧直线的最右坐标值,因为这时如果直线比r[]还靠右,那么这个数字必然无法分到同一个矩形里,另三个数组的意义同样)

然后慢慢更新这四组坐标,更新的依据是如果某个ri在另一个rj和lj之间,那么如果要划分的话,至少是要在rj处画直线,否则第j个数就无法分到同一个矩形里,所以这时更新ri=rj,同样的对l, u, d做同样的更新。

那么在所有的数据都更新完之后,我们在进行一次check,看看是否还有某个ri(或li)在rj和lj之间同时ui(或di)在uj和dj之间(就是某个矩形与另一个矩形有重合部分),如果有,那么进行划分时,必然会导致第i个数或者第j个数无法分到同一个矩形里。

这样我们便得到了解法

#include <cstdio>#include <cstdlib>#include <algorithm>#include <iostream>#include <map>#include <vector>#include <numeric>using namespace std;#define MAX(x,y) ((x)>(y)?(x):(y))#define MIN(x,y) ((x)<(y)?(x):(y))#define INF 1000000000int r[27], l[27], u[27], d[27];bool check(int n) {    for(int i = 0; i < n; i++) {        for(int j = 0; j < n; j++) {            if(i == j) continue;            if(((r[j] >= r[i] && r[j] <= l[i]) || (l[j] <= l[i] && l[j] >= r[i]))                && ((u[j] >= u[i] && u[j] <= d[i]) || (d[j] <= d[i] && d[j] >= u[i])))                    return 0;        }    }    return 1;}int main() {    int t, k ,p;    scanf("%d", &t);    while(t--) {        scanf("%d%d", &k, &p);        for(int i = 0; i < k; i++) {            int rr = INF, ll = 0, uu = INF, dd = 0,tmp, tmpp;            for(int j = 0; j < p; j++) {                scanf("%d%d", &tmp, &tmpp);                rr = min(rr, tmp), ll = max(ll, tmp), uu = min(uu, tmpp), dd = max(dd, tmpp);            }            r[i] = rr, l[i] = ll, u[i] = uu, d[i] = dd;        }        for(int i = 0; i < k; i++) {            for(int j = 0; j < k; j++) {                if(i == j) continue;                if(r[i] > r[j] && r[i] < l[j]) r[i] = r[j];                if(l[i] < l[j] && l[i] > r[j]) l[i] = l[j];                if(u[i] > u[j] && u[i] < d[j]) u[i] = u[j];                if(d[i] < d[j] && d[i] > u[j]) d[i] = d[j];            }        }        if(check(k)) puts("YES");        else puts("NO");    }    return 0;}


0 0
原创粉丝点击