zoukankan      html  css  js  c++  java
  • vjudge题库A-A 分组背包+上升子序列

    vjudge题库 A-A

    题目–>

    • 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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

    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 2n 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
    题目大意理解>
    谷歌好评在这里插入图片描述
    一堆n根木棍。每个棒的长度和重量是预先已知的。这些木棒将由木工机械一一加工。机器需要准备一些时间(称为准备时间)来准备处理木棍。设置时间与清洁操作以及更换机器中的工具和形状有关。木工机的设置时间如下:
    (a)第一个木棍的准备时间为1分钟。
    (b)在处理长度为l和重量为w的棒之后,如果l <= l’并且w <= w’,则机器将不需要设置长度为l’和重量为w’的棒的设置时间。否则,将需要1分钟进行设置。
    您将找到处理给定的n根木棍的最短准备时间。例如,如果您有五根长度和重量对分别为(9,4),(2,5),(1、2),(5、3)和(4,1)的摇杆,则最小设置时间应该是2分钟,因为有对(4,1),(5,3),(9,4),(1,2),(2,5)对的序列。
    输入值
    输入包含T个测试用例。在输入文件的第一行中给出了测试用例的数量(T)。每个测试用例由两行组成:第一行具有整数n,1 <= n <= 5000,代表测试例中木棍的数量,第二行包含2n个正整数l1,w1,l2, w2,…,ln,wn,每个大小最大为10000,其中li和wi分别是第i个木棍的长度和重量。 2n个整数由一个或多个空格分隔。
    输出量
    输出应包含以分钟为单位的最短建立时间,每行一条。

    第一开始想的当然是要如何排序,有l和w两个量开两个数组分开算的话,中间就会漏掉太多情况。 所以和最初的背包有些像,可以先固定l,然后比较w,(嗅到了一丝板子的香气),所以—>let us see see

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int maxn=5000+5;
    int t,n,ans1,f[maxn];
    struct Senorita{
    	int l,w;
    }a[maxn];
    bool comp1(Senorita A,Senorita B) {
    	if(A.l==B.l) return A.w<B.w;
    	return A.l<B.l;
    }
    int main() {
    	scanf("%d",&t);
    	while(t--) {
    		memset(a,0,sizeof(a));
    		memset(f,0,sizeof(f));
    		ans1=1;
    		scanf("%d",&n);
    		for(int i=1;i<=n;i++) {
    			scanf("%d%d",&a[i].l,&a[i].w);
    			f[i]=1;
    		}
    		sort(a+1,a+n+1,comp1);
    		for(int i=2;i<=n;i++) {
    			f[i]=1;
    			for(int j=1;j<i;j++) {
    				if(a[i].w<a[j].w&&f[i]<f[j]+1) {
    					f[i]=f[j]+1;
    					ans1=max(ans1,f[i]);
    				}
    			}
    		}
    		printf("%d
    ",ans1);
    	}
    	return 0;
    }
    
     

    But

    这些是我想说的吗,当然不是
    最初测试的时候为了省略代码,直接在memset初始化的时候把f数组初始化成1,就不用了后面的赋值f了。然而问题就出在这里, 百度memset函数的时候发现好久没写把sizeof和赋的值写反了,设置的是全局变量没有影响。搜索联想里面写到memset只能初始化0和-1

    轰!!

    所以,再让我们回顾一下这个函数,康康百度释义
    在这里插入图片描述一般情况初始化字符数组,依靠二进制赋值,int是四个字节,把每个位置都赋值,只支持0和1,故只能同时初始化 [00000000 00000000 00000000 00000000] 为0和 [11111111 11111111 11111111 11111111] 也就是-1。

    完毕

    又是快快乐乐玩耍的一天在这里插入图片描述

    6.27:最近memset初始化总是写成memset(vis,0,sizeof(0))//要注意

  • 相关阅读:
    如何使用 vimdiff 来 git diff /svn diff
    nginx https ssl 设置受信任证书[原创]
    Nginx配置proxy_pass转发的/路径问题
    116、Android获取Manifest中<meta-data>元素的值
    Android Studio系列教程四--Gradle基础(转载)
    Android Studio系列教程六--Gradle多渠道打包(转载)
    1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco
    Java数组转成list,list转数组
    115、定时器(TimerTask+Timer+Handler)
    114、Android禁止ViewPager的左右滑动
  • 原文地址:https://www.cnblogs.com/1999-09-21Karry-erfs/p/12695408.html
Copyright © 2011-2022 走看看