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 }

 

转载于:https://www.cnblogs.com/937337156Zhang/p/6025862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值