POJ 1065 Wooden Sticks#贪心+qsort用法

(~ ̄▽ ̄)~*

这道题用到了cstdlib库的qsort()函数

用法链接:http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;

struct stick
{
    int l,w;
    int flag;
}st[5005];

int cmp(const void *u,const void *v)
{
    stick *a=(stick *)u;
    stick *b=(stick *)v;
    if(a->l == b->l)
        return a->w - b->w;
    else
        return a->l - b->l;
}

int main()
{
    int cas,n,minT,W;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&st[i].l,&st[i].w);
            st[i].flag=0;
        }
        qsort(st,n,sizeof(st[0]),cmp); //从小到大排序,如果l相等,则w小的排前

        minT=0;
        for(int i=0;i<n;i++)
        {
            if(!st[i].flag)
            {
                minT++;
                W=st[i].w;
                for(int j=i+1;j<n;j++)
                {
                    if((!st[j].flag)&&st[j].w>=W)
                    {
                        st[j].flag=1;
                        W=st[j].w;
                    }
                }
            }
        }
        printf("%d\n",minT);
    }
    return 0;
}
时间: 2024-08-08 09:36:43

POJ 1065 Wooden Sticks#贪心+qsort用法的相关文章

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 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 woodworkin

poj 1065 Wooden Sticks 贪心

题目链接:http://poj.org/problem?id=1065 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1051 贪心 先按l升序再按w稳定升序 然后从头扫到尾取可以取的 然后再从头 直到全部取完 一年前第一次写这道题的时候写了两百行Orz 其中有70行归并排序自己手敲 刚刚翻看老代码真是感慨... #include <cstdio> #include <cstdlib> #include <ctime> #

POJ 1065 Wooden Sticks Greed,DP

排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath>

POJ 1065 Wooden Sticks

POJ 1065 Wooden Sticks n根木材长l_i重w_i,前一根木材大于后一根的话要浪费一分钟准备机器,求最省方案? 我们把问题抽象出来,那就是:把一个数列划分成最少的最长不升子序列 Dilworth定理:对于一个偏序集,最少链划分等于最长反链长度. 这个问题是二元组的最少链划分,那么我们以a[]为关键字大小进行排序,如果a[]中相同就按照b[]排序,根据 Dilworth定理,然后题目就变成了求b[]序列中最长严格下降子序列长度了. 1 #include <iostream>

poj 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你n个木块,分别给出其长度和重量,然后要对这些木块进行加工,如果木块1的长度和重量都不大于木块2, 那么这两个木块可以放在一起加工,从而只用花费1个时间单位.问要如何进行加工从而能够花费最少时间单位. 知识点: 偏序集:若一个集合A为偏序集,那么满足:1.任意一个元素X属于集合,那么这个元素X≤X 2

poj 1065 Wooden Sticks 【贪心 新思维】

题目地址:http://poj.org/problem?id=1065 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 题目抽象:给你一个序列,序列的每个元素包含两个值(x,y).现在希望找到最少数目的条件序列.条件序列是这样的:cur.x<=(cur+1).x && cur.y<=(cur+1).y满足条件的序列的最少的数目是多少?代码: 1 #inclu

poj -1065 Wooden Sticks (贪心or dp)

http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都大于前一根木棍,那么这根木棍不需要生产时间,问你最少的生产时间是多少? 首先可以贪心,先按长度 l排序,如果l相同,按宽度w排序. 从i=0开始,每次把接下来的i+1  -  n-1 的没有标记并且长度和宽度大于等于i这根木棍的长度和宽度标记起来. 1 #include<cstdio> 2 #in

POJ 1065 Wooden Sticks【贪心】

题意: 有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列.问至少要分为多少个序列.且要保证排出来的子序列数最少. 思路: ( 9 , 4 ) ,( 2 , 5 ) ,( 1 , 2 ) ,( 5 , 3 ),( 4 , 1 )可以排成这样 ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 );   ( 1 , 2 ) , ( 2 , 5 ) . 其中:(4,1)<=(5,3)<=(9,4)为不降序列,(4,1)<(5,3)由于4<5&

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a