题目的大意:
就是当下一个的木块的长和宽都大于前一个的时候就不算时间,因此需要找出长和宽同时递增的序列。有多少个序列就是多长时间。
解决方案:
对这一列数进行排序,以长为主,相同的长的在给编排序。然后对排过序的数列进行遍历。遍历长和宽同时递增的数列。统计数列个数即可得到正确的结果。
#include<iostream>
#include<algorithm>
using namespace std;
struct sticks_type {
int length;
int weight;
};
struct sticks_type sticks[5005];
bool shape[5005];
bool cmp_sticks(sticks_type s1,sticks_type s2){
if(s1.length<s2.length)
return true;
else if(s1.length==s2.length)
return s1.weight<=s2.weight;
else return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&sticks[i].length,&sticks[i].weight);
shape[i]=true;
}
sort(sticks,sticks+n,cmp_sticks);
int num = 1;
int minu = 0;
int now = 0;
shape[now]=false;
while(1){
minu++;
for(int i=now+1;i<n;i++)
if(shape[i]&&sticks[i].length>=sticks[now].length&&sticks[i].weight>=sticks[now].weight)
{
shape[i]=false;
num++;
now=i;
}
if(num==n) break;
now=0;
num++;
while(!shape[now])now++;
shape[now]=false;
}
printf("%d\n",minu);
}
return 0;
}

本文介绍了一种解决特定木块排序问题的算法。通过排序和遍历来找出长和宽同时递增的序列数量,以此计算总时间。具体步骤包括定义结构体存储长度和宽度信息、定制排序规则及遍历计数。
740

被折叠的 条评论
为什么被折叠?



