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).
(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
思路:这道题。。困扰我好久。看了网上的思路和同学的想法,终于AC了。。
首先,想让扣的时间最少,所以必须想到,长度和重量最好都要先保证比较小,然后逐渐做质量和长度稍大的。
先对长度进行升序,如果长度相同的话,就对重量进行升序。(长度可能会相同,所以比较时候要注意,我当时没有想到,错了2次)。例如 1 4 1 3 应该输出1,但是如果只看长度的话,可能会输出2。
长度非递减排列的情况下,判断重量,如果重量也是前者小于等于紧挨的后者,就标记为true(开始这么多产品都标记为false),如果不满足不处理。。进行了一次总体的遍历。然后再来“一遍”,从左往右,使被标记为false 的产品进行比较,开始要加一个时间,最左边的false那个产品标记为true,如果前者的重量“紧挨着”小于等于后者,后者就变成true,否则不处理。继续进行循环。。。
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct stick
{
int length;
int weight;
};
bool cmp(stick a,stick b)
{
if(a.length!=b.length)
return a.length<b.length;
else
return a.weight<b.weight;
}
int main()
{
int T,n,i,j,k;
stick a[10000];
bool b[10000];
cin>>T;
while(T--)
{
int sum=1;
memset(b,false,sizeof(b));
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i].length>>a[i].weight;
}
sort(a,a+n,cmp);
int count=0;
for(i=0;i<n;i++)
{
if(b[i]==false)
{
count++;
b[i]=true;
j=i;
for(k=j+1;k<n;k++)
{
if(b[k]==false)
{
if(a[j].weight<=a[k].weight)
{
b[k]=true;
j=k;
}
}
}
}
}
cout<<count<<endl;
}
return 0;
}