心急的C小加

来源:互联网 发布:hifiraver 动作数据 编辑:程序博客网 时间:2024/06/02 15:35
心急的C小加时间限制:1000 ms  |  内存限制:65535 KB难度:4描述C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?输入第一行是一个整数T(1<T<1500),表示输入数据一共有T组。每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示木棒的长度和质量。输出处理这些木棒的最短时间。样例输入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 样例输出213

个人理解:

使用sort函数求解


代码:

#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;struct node{    int l;    int w;    int c;}t[5010];bool cmp(node a,node b){    if(a.l<b.l) return 1;    else if(a.l==b.l&&a.w<b.w) return 1;    return 0;}int main(){    int T,i,j,count;    scanf("%d",&T);    while(T--)    {        int N;        scanf("%d",&N);        memset(t,0,sizeof(t));        count=0;        for(i=0;i<N;i++)            scanf("%d%d",&t[i].l,&t[i].w);        sort(t,t+N,cmp);        for(i=0;i<N;i++)        {            if(t[i].c==0)            {                count++;                int last=t[i].w;                for(j=i+1;j<N;j++){                    if(t[j].c==0&&t[j].w>=last){                        t[j].c=1;                        last=t[j].w;                    }                }            }       }       printf("%d\n",count);    }    return 0;}