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

   题目大意:

   在规定的时间内上交作业,让损失的成绩最少.

   注意时间问题,后面的作业可以提前做,如果提前做可以顶掉前面得分少的作业,毕竟一天只能做一次作业

   用贪心解决,这个算法类似dijkstra算法类似(实际dijkstra算法用的就是是贪心的一种策略)

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

struct node
{
    int x,y;
}p[1024];

bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y>b.y;
    else return a.x<b.x;
}

using namespace std;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        int a[1024]={0},b[1024]={0},c[1024]={0},mini,mins,sum=0,maxs=0,sum2=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            maxs=max(maxs,a[i]);
            p[i].x=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            sum+=b[i];
            p[i].y=b[i];
        }
        sort(p+1,p+n+1,cmp);  ///长的和dijkstra一个样子
        for(int j=1;j<=n;j++)
        {
            if(c[p[j].x]==0){   ///如果这个时间没有作业,呢么当前时间先保存这个作业
                c[p[j].x]=p[j].y;
                continue;
            }
            mins=p[j].y,mini=0;
            for(int i=1;i<=p[j].x;i++)///找到之前的时间的一个比这个作业分数少的作业标记
                if(c[i]<mins)
                    mins=c[i],mini=i;
            if(mini)
                c[mini]=p[j].y;       ///用成绩高的代替原来成绩低的
        }
        for(int i=1;i<=maxs;i++)
            sum2+=c[i];
        printf("%d\n",sum-sum2);
    }
    return 0;
}

ac:

#include<bits/stdc++.h>
#define MAXN 10005
using namespace std;

struct node
{
    int a,b;
}e[MAXN],a[MAXN];

bool cmp(node x,node y)
{
    if(x.a==y.a)
        return x.b>y.b;
    else return x.a<y.a;
}

int main()
{
    int t,n;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            int qsum=0;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
                scanf("%d",&e[i].a);//时间
            for(int i=0;i<n;i++)
            {
                scanf("%d",&e[i].b);//分数
                qsum+=e[i].b;
            }
            sort(e,e+n,cmp);
            int k=1,v;
            a[1].a=e[0].a;
            a[1].b=e[0].b;
            for(int i=1;i<n;i++)
            {
                if(e[i].a>k)
                    k++,a[k].a=e[i].a,a[k].b=e[i].b;
                else{
                    int mins=99999999;
                    for(int j=1;j<=k;j++)
                    {
                        if(a[j].b<mins)
                        {
                            mins=a[j].b;
                            v=j;
                        }
                    }
                    if(e[i].b>mins)
                        a[v].b=e[i].b;
                }
            }
            int sum=0;
            for(int i=1;i<=k;i++)
                sum+=a[i].b;
            printf("%d\n",qsum-sum);
        }
    }
    return 0;
}

优先队列写法,类似dijstra堆优化,ac:

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
    int a,b;
    friend bool operator <(node x,node y)
    {
        if(x.b==y.b)
            return x.a>y.a;
        else return x.b<y.b;
    }
}a[1024];

priority_queue<int,vector<int>,greater<int> > que;

int main()
{
    int n,x,y,t;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%d",&n);
            int qsum=0;
            for(int i=0;i<n;i++)
                scanf("%d",&a[i].b);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i].a);
                qsum+=a[i].a;
            }
            int sum=0,bb=0;
            sort(a,a+n);
            que.push(a[0].a);
            for(int i=1;i<n;i++)
            {
                if(que.top()<a[i].a||a[i].b>que.size())
                {
                    que.push(a[i].a);
                    if(que.size()>a[i].b)
                        que.pop();
                }
            }
            while(que.size())
            {
                sum+=que.top();
                que.pop();
            }
            printf("%d\n",qsum-sum);
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值