HDU 1789 贪心经典

题意 给出n门作业的截止时间与分数 如果不能在那天结束前做完就扣掉相应分数 问怎么安排能让扣分最少

思路 先按分数从大到小排序 先研究大的

做好标记 一开始每天都能放作业 全是true

如果这一天已经有作业了 就往前寻找true的一天

如果没有寻找到就扣分

之前wa了好多次 是因为输入n后 node a[n+1] bool b[n+1] 后来改成node a[1050] bool b[1050]就可以了

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
struct node
{
    int d;
    int f;
}a[1050];
int cmp(node a,node b)
{
    if(a.f==b.f)
        return a.d<b.d;
    else return a.f>b.f;
}
int main(){
int t;
scanf("%d",&t);
while(t--)
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].d);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].f);
    }
    bool b[1000];
    memset(b,true,sizeof(b));
    sort(a+1,a+1+n,cmp);
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        int k;
        for(k=a[i].d;k>=1;k--)
        {
            if(b[k]==true)
            {
                b[k]=false;
                break;
            }
        }
        if(k==0)
        {
            sum+=a[i].f;
        }
    }
    printf("%d
",sum);
}
}

  

原文地址:https://www.cnblogs.com/rayrayrainrain/p/5215625.html