zoukankan      html  css  js  c++  java
  • HDU 1171 Big Event in HDU

    Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
    The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
     
    Input
    Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
    A test case starting with a negative integer terminates input and this test case is not to be processed.
     
    Output
    For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
     
    Sample Input
    2 10 1 20 1 3 10 1 20 2 30 1 -1
     
    Sample Output
    20 10 40 40
    //开始时候直接照搬母函数模板了,提交后发现可以改进,这样就快了近一半,不过这题可以转化成完全背包
    //(DP)来完成,那样的话速度还要更快
    #include <iostream>//改进前
    #include <cstdio>
    #include <string.h>
    #define N 250003
    using namespace std;
    int
    a[N/2],b[N/2];
    int
    r[53][2];
    int
    main()
    {

       // freopen("in.txt","r",stdin);
        int i,j,k,t,te,n;
        while
    (scanf("%d",&n),n>0)
        {
      t=0;
         memset(a,0,sizeof(a));
         memset(b,0,sizeof(b));
        for
    (i=1;i<=n;t+=r[i][0]*r[i][1],i++)
           scanf("%d%d",&r[i][0],&r[i][1]);
         te=t>>1;
        for
    (i=0;i<=r[1][1];i++)
          a[i*r[1][0]]=b[i*r[1][0]]=1;

          for
    (k=2;k<=n;k++)
          {

           for
    (j=1;j<=r[k][1];j++)
            for
    (i=0;i+j*r[k][0]<=te;i++)
               a[i+j*r[k][0]]+=b[i];
            for
    (i=0;i<=te;i++)
               b[i]=a[i];
         }

         while
    (!a[te])
            te--;
        printf("%d %d\n",t-te,te);
        }

        return
    0;
    }
    //改进后,之后复习到动态规划时,转化成完全背包来做

    #include <iostream>
    #include <cstdio>
    #include <string.h>
    #define N 250003
    using namespace std;
    int
    a[N/2];
    int
    r[53][2];
    int
    main()
    {

         int
    i,j,k,t,te,n;
        while
    (scanf("%d",&n),n>0)
        {
      t=0;
         memset(a,0,sizeof(a));
        for
    (i=1;i<=n;t+=r[i][0]*r[i][1],i++)
           scanf("%d%d",&r[i][0],&r[i][1]);
         te=t>>1;
        for
    (i=0;i<=r[1][1];i++)
          a[i*r[1][0]]=1;

          for
    (k=2;k<=n;k++)
          {

           for
    (j=1;j<=r[k][1];j++)
            for
    (i=0;i+j*r[k][0]<=te;i++)
               a[i+j*r[k][0]]=1;//在接下来做的1709时,我发现这样的改进方法是错的,但是这题AC
         }                      //可能是测试的数据不强吧,及时发现了错误

         while
    (!a[te])   //(因为在a[i]=0;时a[i+j*r[k][0]]是可能不存在的,这样的标记也就错了)
            te--;       //虽然人品还好,不过认识错误更重要
        printf("%d %d\n",t-te,te);
        }

        return
    0;
    }
  • 相关阅读:
    Intellij中的常用快捷键
    Intelij 中javax.servlet.http.HttpServlet包导不进来
    JDBC工具类与数据库建立连接
    Xms Xmx PermSize MaxPermSize 区别
    图书管理系统(SSH)
    DAO
    spring中的bean
    Intellij页面汉字乱码问题
    Dispatcher initialization failed
    用同一个类对不同表进行访问
  • 原文地址:https://www.cnblogs.com/372465774y/p/2429913.html
Copyright © 2011-2022 走看看