POJ 1065

http://poj.org/problem?id=1065

题目的大体意思就是给一些木头长l,重w,有一个机器,如果切割这些木头的话,在i后面切割的i+1根木头满足长度和重量都要大于等于前一根则不需要花费时间,求最少的花费时间

一个贪心加动规吧,不是很难的想法

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <stdlib.h>
 5 
 6 
 7 using namespace std;
 8 
 9 
10 struct IN      //定义一个结构体,用来存木头的重量的长度
11 {
12     int w;      
13     int l;
14 } s[5010];
15 
16 bool step[5010];    //用来标记木头是否使用过
17   
18 int cmp(const void *a,const void *b)      //排序,如果长度相等,则按重量进行排序
19 {
20     struct IN *c =(IN *)a;
21     struct IN *d=(IN *)b;
22     if(c->l!=d->l)
23         return c->l-d->l;
24     else
25         return c->w-d->w;
26 }
27 
28 
29 int main()
30 {
31     int i,n,k,time,j,last;
32     scanf("%d",&n);
33     for(i=0; i<n; i++)
34     {
35         time=0;    
36         memset(step,true,sizeof(step));    //初始化
37         scanf("%d",&k);
38         for(int j=0; j<k; j++)
39             scanf("%d%d",&s[j].l,&s[j].w);
40         qsort(s,k,sizeof(s[0]),cmp);
41         for(j=0; j<k; j++)      //用一个变量记住木头的重量
42         {
43             last=s[j].w;
44             if(step[j])    //当这个木头没用过,则可以使用这根木头
45             {
46                 for(int x=j+1; x<k; x++)
47                 {
48                     if(s[x].w>=last&&step[x])   //由于是按长度排序的,所以如果下一根木头的重量比前一根的重,则不需要时间,且更新重量
49                     {
50                         step[x]=false;
51                         last=s[x].w;
52                     }
53                 }            //这一次的木头切完了,时间加一
54                 time++;
55             }
56         }
57         printf("%d
",time);
58     } return 0;    
59 }
原文地址:https://www.cnblogs.com/Tree-dream/p/5343474.html