Wooden Sticks
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 21754 |
|
Accepted: 9277 |
Description
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) ,
and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since
there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1
, 2 ) , ( 2 , 5 ) .
Input
The
input consists of T test cases. The number of test cases (T) is
given in the first line of the input file. Each test case consists of
two lines: The first line has an integer n , 1 <= n <=
5000 , that represents the number of wooden sticks in the test
case, and the second line contains 2n positive integers l1 , w1
, l2 , w2 ,..., ln , wn , each of magnitude at most 10000 ,
where li and wi are the length and weight of the i th wooden
stick, respectively. The 2n integers are delimited by one or more
spaces.
Output
The output should contain the minimum setup time in minutes, one per line.
Sample Input
3
5
4 9 5 2 2 1 3 5 1 4
3
2 2 1 1 2 2
3
1 3 2 2 3 1
Sample Output
2
1
3
Source
Dilworth定理:对于一个偏序集,最少链划分等于最长反链长度。
Dilworth定理的对偶定理:对于一个偏序集,其最少反链划分数等于其最长链的长度。
也就是说把一个数列划分成最少的最长不升子序列的数目就等于这个数列的最长上升子序列的长度。
【解决】
要求最少的覆盖,按照Dilworth定理
最少链划分 = 最长反链长度
所以最少系统 = 最长导弹高度上升序列长度。
http://blog.csdn.net/acdreamers/article/details/7626671
pair对组 排序 先排fierst 而后排second 正好。
像最少系统,直接是线性的数据,所以不需要考虑,直接求解
而这题 是对组数据。需要先对first排序,之所以排序是因为转化成线性数据。直接看second数据就好。
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#define fi first
#define se second
#define pi pair<int,int>
#define md make_pair
#define ha pair<int,pair<int,pi> >
using namespace std;
const int maxn=5005;
pi s[maxn];
int dp[maxn];
main()
{
int t,mx,n;
//freopen("data.in","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d%d",&s[i].fi,&s[i].se);
sort(s,s+n);
mx=0;
for(int i=0;i<n;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(s[i].se<s[j].se)
dp[i]=max(dp[j]+1,dp[i]);
}
mx=max(mx,dp[i]);
}
printf("%d\n",mx);
}
}
Wooden Sticks
原文:http://www.cnblogs.com/acmtime/p/5740723.html