hdu 1789 Doing Homework again(贪心)

题意:
N个作业,每个作业有个deadline。每个作业完成耗时一天。

如果某个作业没在deadline前完成,则要扣去一定的分数。

给出N个要扣除的分数score[1]....score[N]。

如何安排使得扣分最少?求最少扣分。

思路:

按扣分多少从大到小排序,然后一个一个放到各自的deadline前的某个位置,哪个位置?

哪个位置有空就放那儿,且必须要都靠后!也就是从后往前放。这样可以保证最优地不占用更靠前的别人的deadline前的空间。

具体看代码,,,,

代码:

struct node{
    int deadtime, score;
}
a[1005];

int b[100005];

bool cmp(node a,node b){
    if(a.score==b.score)
        return a.deadtime<b.deadtime;
    return a.score>b.score;
}

int Insert(int deadtime){
    rep2(i,deadtime,1){
        if(b[i]==-1)
            return i;
    }
    return -1;
}

int T,n;
int main(){

    int T;
    cin>>T;
    while(T--){
        scanf("%d",&n);
        rep(i,1,n) scanf("%d",&a[i].deadtime);
        rep(i,1,n) scanf("%d",&a[i].score);
        sort(a+1,a+1+n,cmp);
        int ans=0;
        mem(b,-1);
        rep(i,1,n){
            int pos=Insert(a[i].deadtime);
            if(pos!=-1){
                b[pos]=i;
            }else{
                ans+=a[i].score;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}
时间: 2024-08-10 18:19:00

hdu 1789 Doing Homework again(贪心)的相关文章

hdu 1789 Doing HomeWork Again (贪心算法)

题目链接: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): 7903 Accepted Submission(s): 4680 Problem Description Ignatius has just c

HDU 1789 - Doing Homework again - [贪心+优先队列]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem DescriptionIgnatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Eve

HDU 1789 Doing Homework again(贪心)

Doing Homework again 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 deadlin

HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序

题目中因为天数和分数是对应的,所以我们使用一个结构体来存分数和截止如期. 一开始做这道题的时候,很自然的就想到对天数排序,然后天数一样的分数从大到小排序,最后WA了之后才发现没有做到"舍小取大"的贪心.所以改变一下策略,对分数排序,如果分数一样的话,时间从小到大排序(因为我们的目的就是先做分多的作业,所以分数一样的得放到前几天去做). (具体sort排结构体知识见代码里面,其实也可以写两次for来排序): 思路:排好序之后,从小到大遍历,每找到一个分数,去寻找对应的天数到第一天中有没有

贪心/hdu 1789 Doing Homework again

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int n,ans; 6 struct node 7 { 8 int score; 9 int ddl; 10 }; 11 node a[1010]; 12 bool day[1010]; 13 bool cmp(node x,node y) 14 { 15 return x.score>y.s

HDU 1789 Doing Homework again【贪心】

题意:给出n个作业的截止时间,和该作业没有完成会被扣掉的分数.问最少会被扣掉多少分. 第一次做这一题是好久之前,当时不会(不会处理两个关键字关系@[email protected])---现在还是不会---看了题解---原来是这样的--- 因为要使得扣的分数尽可能少,那就先把扣分多的作业做了,即按照扣分降序排序,再遍历看该份作业能不能完成,不能完成则扣去相应的分数 #include<iostream> #include<cstdio> #include<cstring>

hdu 1257 &amp;&amp; hdu 1789(简单DP或贪心)

第一题;http://acm.hdu.edu.cn/showproblem.php?pid=1257 贪心与dp傻傻分不清楚,把每一个系统的最小值存起来比较 1 #include<cstdio> 2 using namespace std; 3 int a[30001],b[30001]; 4 int main() 5 { 6 int n,i,j; 7 while (~scanf("%d",&n)) 8 { 9 int ans=0; 10 b[0]=-1; 11 f

HDU 1789 Doing Homework again

在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案直接交换就可以了. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; struct HomeWork { int de

HDU 1789 Doing Homework again(馋)

意甲冠军  参加大ACM竞争是非常回落乔布斯  每一个工作都有截止日期   未完成必要的期限结束的期限内扣除相应的积分   求点扣除的最低数量 把全部作业按扣分大小从大到小排序  然后就贪阿  能完毕前面的就完毕前面的  实在不能的就扣分吧~ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 1005; int dli[N], red[N],