zoukankan      html  css  js  c++  java
  • HDU1789 贪心

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1789

    Doing Homework again

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4877    Accepted Submission(s): 2852


    Problem Description
    Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
     
     
    Input
    The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
    Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
     
     
    Output
    For each test case, you should output the smallest total reduced score, one line per test case.
     
     
    Sample Input
    3
    3
    3 3 3
    10 5 1
    3
    1 3 1
    6 2 3
    7
    1 4 6 4 2 4 3
    3 2 1 7 6 5 4
     


    Sample Output
    0
    3
    5

    还是要多想,想通了就好办多了

    先对扣分多的进行排序,再对deadline进行倒数,用另一数组进行标记即可

    由于结构体不熟悉,走了好多弯路,对sort的用法还不熟悉代码后有网上复制的用法

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    struct home
    {
        int de,re;
    };
    home f[10005];
    int cmp(const home x,const home y)
    {
        if(x.re!=y.re)return x.re>y.re;
        return x.de>y.de;
    }
    int main()
    {
        int n,m,i,j;
        int a[1005];
        //int de[1005],re[1005];
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d",&m);
            for(i=1;i<=m;i++)
            scanf("%d",&f[i].de);
            for(i=1;i<=m;i++)
            scanf("%d",&f[i].re);
            sort(f+1,f+m+1,cmp);
            memset(a,0,sizeof(a));
            int sum=0;
            for(i=1;i<=m;i++)
            {
                for(j=f[i].de;j>0;j--)
                {
                    if(!a[j]){a[j]=1;break;}
                }
    
              if(!j) sum+=f[i].re;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }

    C++ sort函数用法

    FROM:http://hi.baidu.com/blackdemonfish/blog/item/e2c1d655d702a45ed0090652%2Ehtml

    最近算法作业经常需要排序。偶是一个很懒的人,于是一直用C++的sort进行排序~~~不少同志对此心存疑虑,所以今天就写一写sort的用法。
    声明:此用法是从某大牛的程序中看到的,其实偶只是拿来用,不知所以然,飘走~~~~~

    MSDN中的定义:

    template<class RanIt>
    void sort(RanIt first, RanIt last); //--> 1)
    template<class RanIt, class Pred>
    void sort(RanIt first, RanIt last, Pred pr); //--> 2)


    头文件:
    #include <algorithm>
    using namespace std;

    1.默认的sort函数是按升序排。对应于1)
    sort(a,a+n);   //两个参数分别为待排序数组的首地址和尾地址
    2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
    例如:
    int cmp( const int &a, const int &b ){
        if( a > b )
           return 1;
        else
           return 0;
    }
    sort(a,a+n,cmp);
    是对数组a降序排序
    又如:
    int cmp( const POINT &a, const POINT &b ){
        if( a.x < b.x )
           return 1;
        else
           if( a.x == b.x ){
              if( a.y < b.y )
                 return 1;
              else
                 return 0;
            }
           else
              return 0;
    }
    sort(a,a+n,cmp);
    是先按x升序排序,若x值相等则按y升序排



    与此类似的还有C中的qsort,以下同附上qsort的使用方法:

    #include <stdlib.h>

             格式 qsort(array_name,data_number,sizeof(data_type),compare_function_name)       (void*)bsearch (pointer_to_key_word,array_name,find_number,

    sizeof(data_type),compare_function_name)

             e.g.

             int Cmp(const void*a,const void *b)

    {

                      int*pa=(int*)a,*pb=(int*)b;

                      if(*pa>*pb) return 1;

                 else if (*pa==*pb)    return 0;

    else   return -1;

    }

    qsort(data,N,sizeof(int),Cmp);        // 对int型数组进行快速排序(非降序排列)

    p=(int*)bsearch(&a,data,n,sizeof(int),Cmp);

    Qsort函数运用

    http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html

    #include <stdio.h>

    #include <string.h>

    #include <stdlib.h>

    int cmpInt(const void *a,const void *b)

    {

        return *(int *)a - *(int *)b;

    }

    int cmpDouble( const void *a , const void *b )

    {

        return *(double *)a > *(double *)b ? 1 : -1;

    }

    int compareLength(const void * a,const void * b)

    {

        if(strlen(*(char * *)a) > strlen(*(char * * )b))

            return 1;

        else if(strlen((*(char * *)a)) < strlen((*(char * *) b)))

            return -1;

        else return strcmp(* (char * *)a,*(char * *)b);

    }

    int main()

    {

        int i;

        int num[10]= {12, 32, 42,51,8,16,51,21,19,9};

        double in[10]= {32.1,456.87,332.67,442.0,98.12,451.79,340.12,54.55,99.87,72.5};

        char * str[] = {"enter","number","size","begin","of","cat","case","program","certain","a"};

        qsort(num,10,sizeof(num[0]),cmpInt);

        qsort(in,10,sizeof(in[0]),cmpDouble);

        qsort((void *)str,10,sizeof(str[0]),compareLength);

        for(i=0; i<10; i++)

        {

            printf("%d  ",num[i]);

        }

        printf(" ");

        for(i=0; i<10; i++)

        {

            printf("%.2f  ",in[i]);

        }

        printf(" ");

        for(int i = 0; i<10; i++)

        {

            printf("%s  ",str[i]);

        }

        printf(" ");

        return 0;

    }

    以上。希望对各位有所帮助。
    PS:FJNU OJ上是禁止使用qsort滴~~~~
  • 相关阅读:
    [python]如何理解uiautomator里面的 instance 及使用场景
    [python]如何理解uiautomator里面的 right,left,up,down 及使用场景
    [python] try......except.....finally
    [python]python官方原版编码规范路径
    [python]pip 版本9.0.1升级到10.0.1故障解决办法
    [python]如何理解uiautomator里面的 child, child_by_text, sibling,及使用场景
    [python]关于在python中模块导入问题追加总结
    [Eclipse]如何往eclipse中导入单个python文件,其它类型代码文件也可以参照该方法
    [Eclipse]在重命令文件名时,提示编码格式有问题导致修改失败,需要设置如下几个默认编码为UTF-8
    [MySQL]在安装windows版MySQL时遇到过如下问题Error Nr.1045和Error.Nr.2003,相应解决办法如下
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/HDU1789.html
Copyright © 2011-2022 走看看