HDU1051,贪心
Wooden Sticks
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).
(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
英语菜,理解能力菜,所以只能用百度翻译,弄了好一大会才搞懂(惭愧啊,搜了题解,才懂了题意,但我没有看答案)。废话少说。
这个就是个 加工木材,然后只要l1<=l2,w1<=w2,就没必要再花一分钟了。
于是我一顿搞,结果连样例都没过(👎)无奈题解走起,才感觉有眼前一亮的感觉。每一组有两个值,l和w,由于我们只要后面的大于前面的,就可以继续工作,所以我们只要先把l排从小到大排好,只看w就可以了。
但这个根据贪心的思想,需要跳着来,所以我们需要对每一组进行标记,用完了就标记上下次就不能用了。然后每次把当前i的w记为最大值,然后往下遍历,看看谁比他大,比他大,那么就标记上已加工好,最大值更新。
第二个for用完后,ans才能加一,因为我们把比当前i的w都用完了。
下面上代码喽
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int b[10005];
struct node
{
int l,w;
}st[10005];
bool cmp(node a,node b)
{
if(a.l==b.l)
return a.w<b.w;
return a.l<b.l;
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(b,0,sizeof(b));
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>st[i].l>>st[i].w;
b[0]=0;
sort(st,st+n,cmp);
int ans=0;
for(int i=0;i<n;i++) //以当前为起点
{
if(b[i])
continue;
int max=st[i].w;
for(int j=i+1;j<n;j++) //找到比当前i的w大的,加工掉(标记掉)
{
if(!b[j]&&max<=st[j].w)
{
max=st[j ].w;
b[j]=1;
}
}
ans++;
}
cout<<ans<<endl;
}
}
记录自己的成长,加油吧!

浙公网安备 33010602011771号