寒假作业(贪心)

nefu218

Time Limit:1000ms

Memory Limit:65536K

Description

小明玩了将近整个寒假,马上就要开学了,现在他有很多寒假作业要做。每一位老师给了他一个交作业的期限。如果在截止日期后交作业,老师就会减少他的期末考试成绩。而现在我们假设每科作业总是做一天。小明让你来帮他安排做作业的计划减少的得分达到最小。

Input

输入包含多组测试用例。输入的第一行是一个整数T,测试用例的数目。T测试用例跟随。每个测试用例开始以一个正整数N(1 < = N < = 1000),表明了寒假作业的数量。.第一行含有N整数代表每个作业应交的期限,下一行中含有N个整数表明被老师降低的分数。

Output

在每一行里,你应该降低输出最小的应被老师减少的分数。

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

这道题是一道比较简单的贪心,首先考虑分数损失的大小,我先把损失大的分数挽救回来,如果分数相等,就按截止日期从大到小排序,然后竟可能的把分数大的放进数组里。

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

using namespace std;

const int maxn = 1005;

struct node
{
    int day;
    int score;
}s[maxn];

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


int main(int argc, char *argv[])
{
    int t;
    while(cin >> t)
    {
        while(t--)
        {
            memset(s, false, sizeof(s));
            int n;
            cin >> n;
            int maxx= 0;
            for(int i = 0; i < n; i++)
            {
                cin >> s[i].day;
                maxx = maxx > s[i].day ? maxx : s[i].day;
            }
            int sum = 0;
            for(int i = 0; i < n; i++)
            {
                cin >> s[i].score;
                sum += s[i].score;
            }
            sort(s, s + n, cmp);
            int temp[maxn] = {false};
            for(int i = 0; i < n; i++)
            {
                for(int j = s[i].day; j>= 0; j--)
                {
                    if(!temp[j])
                    {
                        temp[j] = s[i].score;
                        break;
                    }
                }
            }
            int m = 0;
            for(int i = 1; i <= maxx; i++)
            {
                m += temp[i];
            }

            cout <<sum - m << endl;
        }
    }

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值