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\n",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("\n");

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

    {

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

    }

    printf("\n");

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

    {

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

    }

    printf("\n");

    return 0;

}

以上。希望对各位有所帮助。
PS:FJNU OJ上是禁止使用qsort滴~~~~

转载于:https://www.cnblogs.com/ccccnzb/p/HDU1789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值