D - Wooden Sticks
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
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct hanshu
{
int a;
int b;
}c[10008];
int cmp(struct hanshu q,struct hanshu w)
{
if(q.a==w.a)
return q.b<w.b;
else
return q.a<w.a;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int t,flag[10008];
scanf("%d",&t);
int i,sum=0,j,z;
for(i=0;i<t;i++)
{
scanf("%d%d",&c[i].a,&c[i].b);
}
sort(c,c+t,cmp);
memset(flag,0,sizeof(flag));
for(i=0;i<t;i++)
{
if(flag[i]==0)
{
for(j=i+1,z=c[i].b;j<t;j++)
{
if((c[j].b>=z)&&(flag[j]==0))
{
z=c[j].b;
flag[j]=1;
}
}
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
百度翻译:
有一堆木棍。每根棍子的长度和重量都事先知道。这些木棒将由木工机械一个一个地处理。它需要一些时间,称为安装时间,为机器准备处理棒。安装时间与清洗操作和更改机床中的工具和形状相关联。木工机床的安装时间如下:
(a)第一根木棒的放置时间为1分钟。
(b)在加工一根长L和重量W之后,如果L = L和W = W,机器将不需要安装长度为L的长度和重量w的时间。否则,它将需要1分钟设置。
你要找到最小的安装时间来处理给定的一堆N木棍。例如,如果你有五根的长度和重量的双(4)、(5),(2,1),(3,5),和(1,4),那么最小建立时间应该是2分钟后有一个序列对(1,4),(二),(4),(2,1),(5)。
输入
输入由t测试用例。在输入文件的第一行中给出了测试用例(t)的数目。每个测试案例是由两行:第一行有一个整数n,1≤N≤5000,表示测试用例的木棍的数量,和第二行包含2个正整数n的L1,L2,W1,W2,…,LN,WN,每个最多10000的大小,而丽和Wi的长度和第i个木棒的重量,分别。2n整数由一个或多个空间分隔。
输出
输出应该包含最小设置时间在分钟,每行一个。
题目大意:
给n根木棍的长度和重量。根据要求求出制作木棍的最短时间。建立第一个木棍需要1分钟,若是接着要制作的木棍重量和长度都比此木棍长就不需要建立的时间,若是没有,则再需要建立时间。求时间最小为多少。
题目分析:
一道贪心题,利用有序化数据进行贪心选择。其实就是求长度和重量都严格递增的数字序列的个数。
先按长度进行排序。排序完后的不一定都是下一根木棍重量和长度都大于前一根的。