zoukankan      html  css  js  c++  java
  • USACO JANUARY——矩形[rects]

    Description

        给出N个矩形(1≤N≤100)和它的长和宽(不超过1000),写一个程序找出最大的K,使得
    有K个矩形满足层层包含的关系,即里层的矩形被所有外层的矩形包含.一个矩形P1包含另一个
    矩形P2,则P2的一边小于P1的一边,并且P9的另一边不超过P1的另一边.如果两个矩形相同,视为不包含.如2 x 1的矩形被2x2的矩形包含,不被1 x 2的矩形包含.
        注意:矩形的顺序可以是任意的,且矩形可以旋转.

    Input

        第1行:整数N.
        第2到N+1行:矩形的长和宽,均为整数.

    Output

        一行,输出最大的包含数K.

    Sample Input

    4
    8 14
    16 28
    29 12
    14 8

    Sample Output

    2
     
    由于数据小,所以可以先把相同的矩形全部删去,然后处理的时候,输入保证长小于宽,再用长排序,求宽的最长不下降序列(本来可以写成nlogn的,但是想偷个懒~)
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn=1005;int maxx=0;
     7 int a[maxn],b[maxn],c[maxn];
     8 bool map[maxn][maxn];
     9 struct node{
    10     int l,r;
    11 }w[maxn];
    12 int n;
    13 bool comp(const node &q,const node &e)
    14 {
    15     if(q.l==e.l)return q.r<e.r;
    16     return q.l<e.l;
    17 }
    18 int main()
    19 {
    20     freopen("recks.in","r",stdin);
    21     freopen("recks.out","w",stdout);
    22     scanf("%d",&n);
    23     int temp=0;
    24     for(int i=1;i<=n;i++)
    25     {
    26         int x,y;
    27         scanf("%d%d",&x,&y);
    28         if(map[x][y]==false)
    29         {
    30             map[x][y]=true;
    31             map[y][x]=true;
    32             w[++temp].l=x;w[temp].r=y;
    33             if(w[temp].l>w[temp].r)swap(w[temp].l,w[temp].r);
    34         }
    35         else continue;
    36     }
    37     sort(w+1,w+temp+1,comp);
    38     for(int i=1;i<=temp;i++)c[i]=1;
    39     for(int i=2;i<=temp;i++)
    40     {
    41         maxx=0;
    42         for(int j=1;j<i;j++)
    43         {
    44             if(w[i].r>w[j].r&&maxx<c[j]+1)
    45             {
    46                 maxx=c[j]+1;
    47                 c[i]=maxx;
    48                 b[i]=j;
    49             }
    50             else if(w[i].r==w[j].r&&w[i].l!=w[j].l&&maxx<c[j]+1)
    51             {
    52                 maxx=c[j]+1;
    53                 c[i]=maxx;
    54                 b[i]=j;
    55             }
    56         }
    57     }
    58     int pos=0;maxx=0;
    59     for(int i=1;i<=temp;i++)
    60     {
    61         if(c[i]>maxx)
    62         {
    63             maxx=c[i];pos=i;
    64         }
    65     }
    66     int ans=0;
    67     while(b[pos])
    68     {
    69         ans++;
    70         pos=b[pos];
    71     }
    72     printf("%d",ans+1);
    73     return 0;
    74 }
  • 相关阅读:
    POJ 3669 Meteor Shower(bfs)
    MongoDB 分片的原理、搭建、应用
    Linux下Mongodb安装和启动配置
    目录操作
    一阶段第四次整理(关于滚动条监听的进一步解释)
    HTML DOM 节点介绍(nodeName,nodeValue,nodeType)
    ASP.NET 开发人员应该知道的8个网站
    Java 里快如闪电的线程间通讯
    php-多态
    Winform控件学习-TreeView
  • 原文地址:https://www.cnblogs.com/937337156Zhang/p/6025862.html
Copyright © 2011-2022 走看看