USACO JANUARY——矩形[rects]
生活随笔
收集整理的這篇文章主要介紹了
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
48 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
總結
以上是生活随笔為你收集整理的USACO JANUARY——矩形[rects]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这样换得到吗
- 下一篇: “天色亦黄昏”上一句是什么