矩形嵌套
-
描述
- 有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
- 第一行是一个正正数N(0<N<10),表示测试数据组数,
struct Rec{
int With ;
int Height ;
Rec(){}
Rec(int w , int h):With(w),Height(h){
if(With < Height)
swap(With , Height) ;
}
friend bool operator < (const Rec &A , const Rec &B){
return A.Height < B.Height && A.With < B.With ;
}
};
int cmp(const Rec &A , const Rec &B){
if(A.With == B.With)
return A.Height < B.Height ;
else
return A.With < B.With ;
}
int dp[1008] ;
int DP(vector<Rec> &List){
sort(List.begin() , List.end() , cmp) ;
int i , j ;
for(i = 0 ; i < List.size() ; i++){
dp[i] = 1 ;
for(j = 0 ; j < i ; j++){
if(List[j] < List[i])
dp[i] = max(dp[i] , dp[j]+1) ;
}
}
return *max_element(dp , dp+List.size()) ;
}
int main(){
int T , i , n , x , y ;
vector<Rec> List ;
cin>>T ;
while(T--){
List.clear() ;
scanf("%d" , &n) ;
for(i = 0 ; i < n ; i++){
scanf("%d%d" , &x , &y) ;
List.push_back(Rec(x,y)) ;
}
printf("%d\n" ,DP(List)) ;
}
return 0 ;
}
HDU 4001 To Miss Our Children Time
题意: 给你些积木(长,宽 ,价值 ,类型),分三 类:
0:只能放在小于等于它的长和宽的积木上,
1:只能放在长和宽小于等于它且面积小于他的木块上,
2:只能放在长和宽偶小于它的木块上。求积木的最高高度。
贪心排一下序,最长单调子序列 。
排序注意地方 。 A=(5,5,10,0) B=(5,5,10,1)排的顺序。
A可以在B上, B不能在A上。 显然, B在A左边 。
最近代码洁癖越来越严重了。
typedef long long LL ;
struct Rec{
int With ;
int Height ;
int money ;
int type ;
Rec(){}
Rec(int w , int h , int c , int t):With(w),Height(h),money(c),type(t){
if(With < Height)
swap(With , Height) ;
}
friend int operator < (const Rec &A , const Rec &B){
if(A.With != B.With)
return A.With < B.With ;
if(A.Height != B.Height)
return A.Height < B.Height ;
return A.type > B.type ;
}
bool Type0_OK (const Rec &B){
return Height <= B.Height && With <= B.With ;
}
bool Type1_OK (const Rec &B){
LL Sa = (LL)Height * (LL)With ;
LL Sb = (LL)B.Height * (LL)B.With ;
return Height <= B.Height && With <= B.With && Sa < Sb ;
}
bool Type2_OK (const Rec &B){
return Height < B.Height && With < B.With ;
}
};
LL dp[1008] ;
LL DP(vector<Rec> &List){
sort(List.begin() , List.end()) ;
int i , j ;
for(i = 0 ; i < List.size() ; i++){
dp[i] = (LL)List[i].money ;
for(j = 0 ; j < i ; j++){
if( List[i].type == 0 && List[j].Type0_OK(List[i])
|| List[i].type == 1 && List[j].Type1_OK(List[i])
|| List[i].type == 2 && List[j].Type2_OK(List[i]) )
dp[i] = max(dp[i] , dp[j] + (LL)List[i].money) ;
}
}
return *max_element(dp , dp+List.size()) ;
}
int main(){
int T , i , n , x , y , c ,d ;
vector<Rec> List ;
while(scanf("%d" , &n) && n){
List.clear() ;
for(i = 0 ; i < n ; i++){
scanf("%d%d%d%d" , &x , &y , &c , &d) ;
List.push_back(Rec(x,y,c,d)) ;
}
cout<<DP(List)<<endl ;
}
return 0 ;
}
飞翔
- 描述
-
鹰最骄傲的就是翱翔,但是鹰们互相都很嫉妒别的鹰比自己飞的快,更嫉妒其他的鹰比自己飞行的有技巧。于是,他们决定举办一场比赛,比赛的地方将在一个迷宫之中。
这些鹰的起始点被设在一个N*M矩阵的左下角map[1,1]的左下角。终点被设定在矩阵的右上角map[N,M]的右上角,有些map[i,j]是可以从中间穿越的。每一个方格的边长都是100米。如图所示:
没有障碍,也没有死路。这样设计主要是为了高速飞行的鹰们不要发现死路来不及调整而发生意外。潘帕斯雄鹰冒着减RP的危险从比赛承办方戒备森严的基地中偷来了施工的地图。但是问题也随之而来,他必须在比赛开始之前把地图的每一条路都搞清楚,从中找到一条到达终点最近的路。(哈哈,笨鸟不先飞也要拿冠军)但是此鹰是前无古鹰,后无来鹰的吃菜长大的鹰--菜鸟。他自己没有办法得出最短的路径,于是紧急之下找到了学OI的你,希望找到你的帮助。
- 输入
- 本题有多组数据。以EOF为输入结束的标志。
每组测试数据的首行为n,m(0<n,m<=1000000),第2行为k(0<k<=1000)表示有多少个特殊的边。以下k行为两个数,i,j表示map[i,j]是可以直接穿越的。 输出 - 仅一行,1,1-->n,m的最短路径的长度,四舍五入保留到整数即可 样例输入
-
3 231 13 21 2
样例输出 -
383
- 本题有多组数据。以EOF为输入结束的标志。
const int Max_N = 1008 ;
struct Node{
int x ;
int y ;
friend bool operator < (const Node &A , const Node &B){
if(A.x != B.x)
return A.x < B.x ;
else
return A.y < B.y ;
}
friend bool operator > (const Node &A , const Node &B){
return (A.x > B.x) && (A.y > B.y);
}
};
Node Fei[Max_N] ;
int N , M , K ;
int dp[Max_N] ;
double DP(){
int i , j ;
double ans = 100.0 * (N+M) ;
sort(Fei+1 , Fei+1+K) ;
for(i = 1 ; i <= K ; i++){
dp[i] = 1 ;
for(j = 1 ; j < i ; j++){
if(Fei[i] > Fei[j])
dp[i] = max(dp[i] , dp[j]+1) ;
}
}
for(i = 1 ; i <= K ; i++){
ans = min(ans , 100.0*(N+M-2*dp[i]) + sqrt(20000.0)*dp[i]) ;
}
return ans ;
}
int main(){
while(scanf("%d%d" ,&N ,&M) != EOF){
scanf("%d" ,&K) ;
for(int i = 1 ; i <= K ; i++)
scanf("%d%d" ,&Fei[i].x , &Fei[i].y) ;
printf("%.0lf\n" ,DP()) ;
}
return 0 ;
}