Wooden Sticks (模拟)

还是感觉蛮神奇的,用sort函数对木棍的两个属性进行排列,然后用了一个标记数组。
题目大意:
给你一堆木棒,每个木棒都有自己的长度和重量,装一个木棒需要一分钟,如果下一个木棒的长度和重量均小于这个,那接下来的这个就不需要花费时间,问最少需要花多久……

题目链接: 点击打开链接

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;
int vis[5500];
struct node
{
    int len;
    int weight;
}e[5500];

int cmp(struct node a,struct node b)    //排序
{
    if(a.len!=b.len)
        return a.len<b.len;
    else
        return a.weight<b.weight;
}
int main(void)
{
    int t,n,sum,maxwei;
    scanf("%d",&t);
    while(t--)
    {
        memset(e,0,sizeof(e));
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&e[i].len,&e[i].weight);
        }
        sort(e,e+n,cmp);
        //printf("%d  %d\n",e[0].len,e[0].weight);
        //printf("%d  %d\n",e[1].len,e[1].weight);
        maxwei=e[0].weight;
        sum=0;
        for(int i=0;i<n;i++)     //因为每次只把不花时间的给标记了,所以要从未被标记的再来一次循环2333
        {
            if(!vis[i])
            {
                maxwei=e[i].weight;
                vis[i]=1;
                sum++;
                for(int j=i+1;j<n;j++)
                {
                    if(!vis[j]&&e[j].weight>=maxwei)
                    {
                        vis[j]=1;
                        maxwei=e[j].weight;
                    }
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/destiny1507/article/details/79846555