Doing Homework again (贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chimchim04/article/details/81774346

Doing Homework again 

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

题目大意:

小明有n项作业,完成一门作业需要一天,然后有两行数据,第一行是每项作业交的最晚时间,第二行是每一项作业如果完不成小明就要被扣的分数,问小明最少被扣多少分

思路:从扣分多的开始写

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int d,f;
}a[1005];
bool cmp(node a,node b)
{
    if(a.f==b.f)
    return a.d<b.d;
    return a.f>b.f;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i].d);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i].f);
        sort(a,a+n,cmp);
        int vis[1005]={0},flag=0,k,sum=0;
        for(int i=0;i<n;i++)
        {
            flag=0;
            k=a[i].d;
            while(k--)
            {
                if(!vis[k])
                {
                    vis[k]=1;
                    flag=1;
                    break;
                }
            }
            if(!flag)
                sum+=a[i].f;
        }
        printf("%d\n",sum);
    }
    return 0;
}

代码:

 

阅读更多
换一批

一道acm的题目doing homework

03-19

题目地址:rnhttp://acm.hdu.edu.cn/showproblem.php?pid=1074rn我的想法说这样的,假如已经得到 A,B,C, D,科目的完成顺序,再有一个 科目E,分别插入他们的中间,看看哪一个是最少的rn,一样一个一个的插入进去,最后得到的就是要的最少完成顺序:rn我的代码:rn数组cnta是记录当前的完成顺序,cntb是记录前面以得到的最少完成顺序,f是存放在完成的时间。rn#include rnrnusing namespace std;rnstruct pprnrn char name[20];rn int end;rn int time;rnkm[20];rnint main()rnrn int t;rn cin >> t;rn while(t--)rn rn int n;rn cin >> n;rn int i,j,k;rn int count;rn int p=0;rn int cnta[1<<15],cntb[1<<15],cntc[1<<15],f[1<<15];//用来保存存好的顺序和上一个完成的时间,rn f[0] = 0 , cntb[1] = 1;rn for(i=1;i<=n;i++)rn rn cin >> km[i].name >> km[i].end >> km[i].time;rn rnrn int ffg , flag ;rn int min = 0xfffffff;rn for( i = 2 ; i <= n ; i++)rn rn min = 0xfffffff;rn for( j = 1 ; j <= i ; j++)rn rn flag = 0;rn ffg = 1;rn for( k=1 , count = 1 ; k < j ; k++ , count++ )rn rn cnta[ffg++] = cntb[k];rn if( km[cntb[k]].end - km[cntb[k]].time - f[count-1] < 0 )rn flag += ( f[count-1] + km[cntb[k]].time - km[cntb[k]].end );rn f[count] = f[count-1] + km[cntb[k]].time;rn rn cnta[ffg++] = i;rn if( km[i].end - km[i].time - f[count-1] < 0 )rn flag += ( f[count-1] + km[i].time - km[i].end );rn f[count] = f[count-1] + km[i].time;rn count++;rn rn for( ; k < i ; k++ , count++ )rn rn cnta[ffg++] = cntb[k];rn if( km[cntb[k]].end - km[cntb[k]].time - f[count-1] < 0 )rn flag += ( km[cntb[k]].time + f[count-1] - km[cntb[k]].end ); rn f[count] = f[count-1] + km[cntb[k]].time;rn rn if( flag <= min )rn rn min = flag;rn for(int ii = 1 ; ii <= ffg ; ii++)rn cntc[ii] = cnta[ii];rn rn rn for(int jj = 1 ; jj <= ffg ; jj++)rn rn cntb[jj] = cntc[jj];rn rn rn cout << min << endl;rnrn for( i = 1 ; i <= n ; i++)rn cout << km[cntb[i]].name << endl;rn rn return 0;rn rnrnrn我提交过了是错的,我自己试了几组数据是对的,我就想不出是哪里错了,难道我的想法有错?

没有更多推荐了,返回首页