Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10423 Accepted Submission(s): 4287
(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).
#include <iostream> using namespace std;
struct stick{
int l,w;
}s[];
int main()
{
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>s[i].l>>s[i].w;
//对l排序
for(int i=;i<=n-;i++)
for(int j=;j<=n-i;j++){
if(s[j].l>s[j+].l){
int t;
t=s[j].l;s[j].l=s[j+].l;s[j+].l=t;
t=s[j].w;s[j].w=s[j+].w;s[j+].w=t;
}
}
int sum=; //递增序列的个数
//对w序列进行筛选
while(){
int i;
for(i=;i<=n;i++){ //如果全部为-1,则退出循环
if(s[i].w!=-)
break;
}
if(i>n) break;
sum++;
int num=;
for(i=;i<=n;i++){
if(num== && s[i].w!=-){
num=s[i].w;
s[i].w=-;
}
else if(s[i].w!=- && s[i].w>=num){
num=s[i].w;
s[i].w=-;
}
}
}
cout<<sum<<endl; }
return ;
}
Freecode : www.cnblogs.com/yym2013