Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24742 Accepted Submission(s): 9967
(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
先按照L从大到小进行排序,然后直接DP求最长上升子序列即可.
答案即为最长上升子序列的长度+1
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAXN 10050
struct stick
{
int l,w;
}s[MAXN];
bool cmp(stick a,stick b)
{
if(a.l==b.l) return a.w>=b.w;
return a.l>b.l;
}
int dp[MAXN];
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i].l>>s[i].w;
}
memset(dp,0,sizeof(dp));
sort(s,s+n,cmp);
for(int i=n-1;i>=0;i--)
{
for(int j=i+1;j<n;j++)
{
if(s[j].w>s[i].w&&dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
}
}
}
int maxx=0;
for(int i=0;i<n;i++)
{
if(dp[i]>maxx)
{
maxx=dp[i];
}
}
cout<<maxx+1<<endl;
}
return 0;
}
482




被折叠的 条评论
为什么被折叠?



