zoukankan      html  css  js  c++  java
  • nyoj 题目16 矩形嵌套

    矩形嵌套

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    有n个矩形,每个矩形可以用a,b来描述,表示长和宽。矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度)。例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)中。你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每一个矩形都可以嵌套在下一个矩形内。
     
    输入
    第一行是一个正正数N(0<N<10),表示测试数据组数,
    每组测试数据的第一行是一个正正数n,表示该组测试数据中含有矩形的个数(n<=1000)
    随后的n行,每行有两个数a,b(0<a,b<100),表示矩形的长和宽
    输出
    每组测试数据都输出一个数,表示最多符合条件的矩形数目,每组输出占一行
    样例输入
    1
    10
    1 2
    2 4
    5 8
    6 10
    7 9
    3 1
    5 8
    12 10
    9 7
    2 2
    
    样例输出
    5

    转换为最长上升子序列问题,代码如下
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 typedef pair<int,int> Matrix;
     9 Matrix mat[1002];
    10 int cnt[1002];
    11 
    12 bool cmp(Matrix a, Matrix b) {
    13     if(a.first == b.first) {
    14         return a.second < b.second;
    15     }
    16     else {
    17         return a.first < b.first;
    18     }
    19 }
    20 
    21 bool isOk(Matrix a, Matrix b) {
    22     return (a.first < b.first) && (a.second < b.second);
    23 }
    24 int main(int argc, char const *argv[])
    25 {
    26     int t;
    27     //freopen("input.txt","r",stdin);
    28     scanf("%d",&t);
    29     while(t--) {
    30         int n;
    31         scanf("%d",&n);
    32         for(int i = 0; i < n; i++) {
    33             scanf("%d %d",&mat[i].first,&mat[i].second);
    34             if(mat[i].first < mat[i].second) {
    35                 swap(mat[i].first,mat[i].second);
    36             }
    37         }
    38         sort(mat,mat+n,cmp);    
    39         fill(cnt,cnt+n,1);
    40         int ans = 0;
    41         for(int i = 0; i < n; i++) {
    42             int maxv = 0;
    43             for(int j = i-1; j >= 0; j--) {
    44                 if(isOk(mat[j], mat[i]) && maxv < cnt[j]) {
    45                     maxv = cnt[j];
    46                 }
    47             }
    48             cnt[i] = maxv+1;
    49             if(ans < cnt[i]) {
    50                 ans = cnt[i];
    51             }
    52         }
    53         printf("%d
    ", ans);
    54 
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    数据库系列之查询(4)
    数据库系列之查询(3)
    数据库系列之查询(2)
    数据库系列之查询(1)
    数据库系列之视图
    数据库系列之数据管理(删除数据)
    数据库系列之数据管理(更新数据)
    数据库系列之数据管理(插入数据)
    数据库管理之数据表管理(2)
    数据库管理之数据表管理(1)
  • 原文地址:https://www.cnblogs.com/jasonJie/p/6089340.html
Copyright © 2011-2022 走看看