贪心算法 WOODEN STICKS 实例代码

Problem 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 woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output
The output should contain the minimum setup time in minutes, one per line.

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

代码如下:

#include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #define N 5000;

struct node
 {
     int l;
     int w;
     int flag;
 }sticks[5000];
 int cmp(const void *p,const void *q)
 {
     struct node *a = (struct node *)p;
     struct node *b = (struct node *)q;
     if(a->l > b->l) return 1;
     else if(a->l < b->l) return -1;
     else return a->w > b->w ? 1 : -1;
 }
 int main()
 {
     int t,n,cnt,cl,cw;
     int i,j;
     scanf("%d",&t);
     while(t--)
     {
         memset(sticks,0,sizeof(sticks));
         scanf("%d",&n);
         for( i = 0; i < n; i++)
             scanf("%d %d",&sticks[i].l,&sticks[i].w);
         qsort(sticks,n,sizeof(sticks[0]),cmp);
         sticks[0].flag = 1;
         cl = sticks[0].l;
         cw = sticks[0].w;
         cnt = 1;
         for( j = 1; j < n; j++)
         {
             for( i = j; i < n; i++)
             {
                 if(!sticks[i].flag&&sticks[i].l>=cl&&sticks[i].w>=cw)
                 {
                     cl = sticks[i].l;
                     cw = sticks[i].w;
                     sticks[i].flag = 1;
                 }
             }
             i = 1;
             while(sticks[i].flag)
                i++;
             j = i;
             if(j == n) break;
             cl = sticks[j].l;
             cw = sticks[j].w;
             sticks[j].flag = 1;
             cnt++;
         }
         printf("%d\n",cnt);

}
     return 0;
 }

题意:

我们要处理一些木棍,第一根的时间是1分钟,另外的,在长度为l,重为w的木棍后面的那根的长度为l', 重量w',只要l <=l' 并且w <= w',就不需要时间,否则需要1分钟,求如何安排处理木棍的顺序,才能使花的时间最少。

思路:

贪心算法。先把这些木棍按长度和重量都从小到大的顺序排列。cl和cw是第一根的长度和重量,依次比较后面的是不是比当前的cl,cw大,是的话把标志flag设为1,并跟新cl,cw。比较完后,再从前往后扫描,找到第一个标志位为0的,作为是第二批的最小的一根,计数器加一。把它的长度和重量作为当前的cl,cw,再循环往后比较。直到所有的都处理了。

(0)

相关推荐

  • 采用C++实现区间图着色问题(贪心算法)实例详解

    本文所述算法即假设要用很多个教室对一组活动进行调度.我们希望使用尽可能少的教室来调度所有活动.采用C++的贪心算法,来确定哪一个活动使用哪一间教室. 对于这个问题也常被称为区间图着色问题,即相容的活动着同色,不相容的着不同颜色,使得所用颜色数最少. 具体实现代码如下: //贪心算法 #include "stdafx.h" #include<iostream> #define N 100 using namespace std; struct Activity { int n

  • 浅析java贪心算法

    贪心算法的基本思路 1.建立数学模型来描述问题. 2.把求解的问题分成若干个子问题. 3.对每一子问题求解,得到子问题的局部最优解. 4.把子问题的解局部最优解合成原来解问题的一个解. 实现该算法的过程: 从问题的某一初始解出发: while 能朝给定总目标前进一步 do 求出可行解的一个解元素: 由所有解元素组合成问题的一个可行解. 贪心选择性质 所谓贪心选择性质是指所求问题的整体最优解可以通过一系列局部最优的选择,换句话说,当考虑做何种选择的时候,我们只考虑对当前问题最佳的选择而不考虑子问题

  • c语言来实现贪心算法之装箱问题

    装箱问题,贪心算法求近似最优解 复制代码 代码如下: import java.util.Arrays; import java.util.Comparator; //装箱问题,贪心算法 public class Enchase {     public void test1() {         Integer[] boxs={34,6,40,2,23,12,12};         int boxCaptation=40;//箱子容量         //倒序         Arrays.

  • 贪心算法 WOODEN STICKS 实例代码

    Problem DescriptionThere 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 woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to

  • java 基本算法之归并排序实例代码

    java 基本算法之归并排序实例代码 原理:归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表, * 即把待排序序列分为若干个子序列,每个子序列是有序的.      * 然后再把有序子序列合并为整体有序序列. 实例代码: public class MergeSort { /** * * * * @param args */ public static void main(String[] args) { int a[] = { 49, 38, 65, 97, 76, 13,

  • Java算法之冒泡排序实例代码

    java算法-冒泡排序练习 所谓冒泡就是一堆数据相邻的互相比较,把大的数据往后移,小的数据往前移. 百度上找了张图 大家自己想一想这个逻辑 想明白了,直接看代码. public class Two { public static void main(String[] args) { int arg[] = {25,36,15,274}; sort(arg); } private static void sort(int[] array) { for (int j = 1; j < array.l

  • C++贪心算法实现活动安排问题(实例代码)

    贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关. 具体代码如下所示: #include <cstdio> #include <iostream> #include <ctime> #include <

  • Python贪心算法实例小结

    本文实例讲述了Python贪心算法.分享给大家供大家参考,具体如下: 1. 找零钱问题:假设只有 1 分. 2 分.五分. 1 角.二角. 五角. 1元的硬币.在超市结账 时,如果 需要找零钱, 收银员希望将最少的硬币数找给顾客.那么,给定 需要找的零钱数目,如何求得最少的硬币数呢? # -*- coding:utf-8 -*- def main(): d = [0.01,0.02,0.05,0.1,0.2,0.5,1.0] # 存储每种硬币面值 d_num = [] # 存储每种硬币的数量 s

  • 用js实现简单算法的实例代码

    一.冒泡排序 var arr1=[3,9,2,7,0,8,4]; for(var i=0;i<arr1.length;i++){ for(var j=i+1;j<arr1.length;j++){ var temp=0; if(arr1[i]>arr1[j]){ temp=arr1[i]; arr1[i]=arr1[j]; arr1[j]=temp; } } } alert(arr1); 二.快速排序 var a=[3,5,0,9,2,7,5]; function quickSort(a

  • Android中关于递归和二分法的算法实例代码

    // 1. 实现一个函数,在一个有序整型数组中二分查找出指定的值,找到则返回该值的位置,找不到返回 -1. package demo; public class Mytest { public static void main(String[] args) { int[] arr={1,2,5,9,11,45}; int index=findIndext(arr,0,arr.length-1,12); System.out.println("index="+index); } // 1

  • C++ 搬水果贪心算法实现代码

    C++ 搬水果贪心算法实现代码 (1)题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果合成一堆.每一次合并,小明可以把两堆水果合并到一起,消耗的体力等于两堆水果的重量之和.当然经过 n‐1 次合并之后,就变成一堆了.小明在合并水果时总共消耗的体力等于每次合并所耗体力之和. 假定每个水果重量都为 1,并且已知水果的种类数和每种水果的数目,你的任务是设计出合并的次序方案,使小明耗费的体力最少,并输出这个最小的体力耗费值.例如有 3 种水果,

  • Java使用异或运算实现简单的加密解密算法实例代码

    Java简单的加密解密算法,使用异或运算 实例1: package cn.std.util; import java.nio.charset.Charset; public class DeEnCode { private static final String key0 = "FECOI()*&<MNCXZPKL"; private static final Charset charset = Charset.forName("UTF-8"); pr

随机推荐