日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

积木(DP)问题

發布時間:2024/4/17 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 积木(DP)问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題:Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy, now he grows up. In the dead of night, he often misses something, including a simple game which brings him much happy when he was child. Here are the game rules: There lies many blocks on the ground, little LLL wants build "Skyscraper" using these blocks. There are three kinds of blocks signed by an integer d. We describe each block's shape is Cuboid using four integers ai, bi, ci, di. ai, bi are two edges of the block one of them is length the other is width. ci is
thickness of the block. We know that the ci must be vertical with earth ground. di describe the kind of the block. When di = 0 the block's length and width must be more or equal to the block's length and width which lies under the block. When di = 1 the block's length and width must be more or equal to the block's length which lies under the block and width and the block's area must be more than the block's area which lies under the block. When di = 2 the block length and width must be more than the block's length and width which lies under the block. Here are some blocks. Can you know what's the highest "Skyscraper" can be build using these blocks?
?

Input
The input has many test cases.
For each test case the first line is a integer n ( 0< n <= 1000) , the number of blocks.
From the second to the n+1'th lines , each line describing the i‐1'th block's a,b,c,d (1 =< ai,bi,ci <= 10^8 , d = 0 or 1 or 2).
The input end with n = 0.
?

Output
Output a line contains a integer describing the highest "Skyscraper"'s height using the n blocks.
?

Sample Input
3
10 10 12 0
10 10 12 1
10 10 11 2
2
10 10 11 1
10 10 11 1
0

?
Sample Output
24
11

回答:題意大概是給你些積木,分三類,0:只能放在小于等于它的長和寬的積木上,1:只能放在長和寬小于等于它且面積小于他的木塊上,2:只能放在長和寬偶小于它的木塊上。求積木的最高高度。

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct block
{
?? ?int x,y,z,d;
}b[1005];
long long dp[1005];
int n;
bool cmp(block a,block b)
{
?? ?if(a.x!=b.x)return a.x<b.x;
?? ?if(a.y!=b.y)return a.y<b.y;
?? ?return a.d>b.d;
}
void DP()
{
?? ?long long ans=b[0].z;
?? ?for(int i=0;i<n;i++)
?? ?{
?? ??? ?dp[i]=b[i].z;
?? ??? ?ans=max(ans,dp[i]);
?? ?}
?? ?for(int i=1;i<n;i++)
?? ?{
?? ??? ?if(b[i].d==0)
?? ??? ?{
?? ??? ??? ?for(int j=0;j<i;j++)
?? ??? ??? ?{
?? ??? ??? ??? ?if(b[j].x<=b[i].x&&b[j].y<=b[i].y)
?? ??? ??? ??? ??? ?dp[i]=max(dp[i],dp[j]+b[i].z);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(b[i].d==1)
?? ??? ?{
?? ??? ??? ?for(int j=0;j<i;j++)
?? ??? ??? ?{
?? ??? ??? ??? ?if(b[j].x<=b[i].x&&b[i].y>=b[j].y&&(b[i].y*b[j].y||b[i].x>b[j].x))
?? ??? ??? ??? ??? ?dp[i]=max(dp[i],dp[j]+b[i].z);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(b[i].d==2)
?? ??? ?{
?? ??? ??? ?for(int j=0;j<i;j++)
?? ??? ??? ?{
?? ??? ??? ??? ?if(b[i].x>b[j].x&&b[i].y>b[j].y)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?dp[i]=max(dp[i],dp[j]+b[i].z);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ???? ans=max(ans,dp[i]); ?
?? ?}
?? ?cout<<ans<<'\n';
}

int main()
{
?? ?while(1)
?? ?{
?? ??? ?scanf("%d",&n);
?? ??? ?if(n==0)break;
?? ??? ?for(int i=0;i<n;i++)
?? ??? ?{
?? ??? ??? ?scanf("%d%d%d%d",&b[i].x,&b[i].y,&b[i].z,&b[i].d);
?? ??? ??? ?if(b[i].x<b[i].y)
?? ??? ??? ??? ?swap(b[i].x,b[i].y);
?? ??? ?}
?? ??? ?sort(b,b+n,cmp);
?? ??? ?DP();
?? ?}
?? ?return 0;
}

轉載于:https://www.cnblogs.com/benchao/p/4606309.html

總結

以上是生活随笔為你收集整理的积木(DP)问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。