POJ 1231 水题
2014-02-27 21:31
218 查看
提交数和AC人数都少的可怜,但是这是一道纯水题。。。可能是都不屑做这道题吧。。。
The Alphabet Game
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
Sample Output
简而言之,就是看看如果用直线划分空间的话,能不能做到每个区域里只有一种数字
我们可以设置四个数组来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 1000000000
int 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;
}
The Alphabet Game
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1042 | Accepted: 416 |
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
2 3 2 6 4 8 4 4 2 2 1 2 3 2 4 3 3 1 1 3 1 5 1 2 1 4 1 6 1 2 2 4 2 8 1
Sample Output
YES NO
简而言之,就是看看如果用直线划分空间的话,能不能做到每个区域里只有一种数字
我们可以设置四个数组来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 1000000000
int 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;
}
相关文章推荐
- POJ 2545+2591+2247+1338简单水题
- POJ 2955 Brackets(区间DP水题)
- POJ 1269 Intersecting Lines(线段相交,水题)
- Packets - POJ 1017 水题
- poj 1951 Extra Krunch 字符串处理水题
- poj--3624--Charm Bracelet(动态规划 水题)
- Poj 1316 Self Numbers(水题)
- POJ 3628 Bookshelf 2 0-1背包 or DFS 水题
- POJ-3984迷宫问题 (BFS,水题)
- POJ-3672-Long Distance Racing-水题、%c输入要去除换行
- POJ 1837(Dp水题)
- poj_1007 排序水题
- poj_2000 水题
- 挑战第二章 poj 3009 水题DFS
- POJ 1002 UVA 755 487--3279 电话排序 简单但不容易的水题
- POJ-1573 Robot Motion 模拟水题
- POJ 1083 Moving Tables(水题)
- poj 2245 水题
- POJ 2350水题(注意%的输出)
- POJ 1251 Jungle Roads(最小生成树水题) - from lanshui_Yang