Problem 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 (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).
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 n 2 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
Asia 2001, Taejon (South Korea)
题目的大概意思是说,有一堆木材需要加工,第一根木材加工需要1分钟,如果其他的木材的长度和宽度都大于或等于已经加工过的木材,则这根木材的加工不费时间,否则时间也为1分钟。求至少需要用多长时间。
本人的解题思路是:
假如木棍A的长度与宽度均大于木棍B,则木棍A是木棍B的子集;
如果木棍A的长度与宽度均小于木棍B,则木棍B是木棍A的母集;
如果木棍A仅有长度或宽度大于木棍B,则木棍A与木棍B不相关,A与B皆为母集;
而加工用的时间就等价于母集的个数;
伪程序如下:
int main()
{
int time(所需时间)
输入T组数据
while(T--)
{
time=1;
输入n根木棍的l,w;
木棍【t】=母集1;
for(i=0;i<n;i++)
{
(明显):
if(母集1>木棍【i】)
continue;
else if(母集1<=木棍【i】)
{
木棍【i】=母集1;
for(j=0;j<time;j--)
if(有大于母集1的母集j)
删除母集j
}
else
{
if(当前母集为所有母集最后一个)
创造新母集
else
由母集n到母集n+1,go to (明显)
}
}
}
代码如下:
#include<stdio.h>
#include<string.h>
#define init() a=NULL;b=&stick[t];c=b->p//初始化
struct wood
{
int l,w;
//在链表的就是母集,不在的都为子集
struct wood *p=NULL;
}stick[5005];
int cmp(struct wood a,struct wood b)//比较a与b的关系
{
if(a.l>b.l&&a.w>b.w) return 1;
else if(a.l<=b.l&&a.w<=b.w) return -1;
return 0;
}
int T,n,i,j,k;
int main()
{
int time,t;//time为所需时间 t为链表头
struct wood *a,*b,*c;
scanf("%d",&T);
while(T--)
{
//------------------------------------------//初始化
memset(stick,0,sizeof(stick));
time=1;
t=0;
//------------------------------------------//
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&stick[i].l,&stick[i].w);
init();
for(i=1;i<n;i++)
{
//判断当前木棍与母级关系
while(1)
{
if(cmp(*b,stick[i])==0)
{
if(c==NULL)//该木棍无相关全部母级(链表末尾都无相关)
{
//创造新母集
b->p=&stick[i];
time++;
//链表初始化
init();
break;
}
else
{
//跳转到下一母级
a=b;
b=b->p;
c=b->p;
}
}
else if(cmp(*b,stick[i])==1)
{
//让位母级 (比当前木棍小的不止一个子集)
if(a==NULL)//为链表首位
{
stick[i].p=stick[t].p;
t=i;
}
else
{
a->p=&stick[i];
stick[i].p=c;
}
while(b->p!=NULL)
{
//跳转到下一母级
a=b;
b=b->p;
c=b->p;
if(cmp(stick[i],*b)==-1)
{
time--;a->p=c;b=a;
}
}
init();
break;
}
else
{
init();break;
}
//该木棍为子集不做处理
}
}
printf("%d\n",time);
}
}