HDU1789 doing homework again(贪心)

Doing Homework again
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11806    Accepted Submission(s): 6948
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 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
这道题就用到贪心策略,我们把分数从大到小排个序,每次我们分数最高的那天的作业先做,然后标记该天已经做过,然后继续循环,找第2高分数的天数,如果与第一个高分数的天数是同一天的话,那就降到前一天,比较大小,先做这一天分数最大的,一直下去,求出最小扣分。
eg:
1 4 6 4 2 4 3
3 2 1 7 6 5 4
排序:
4 2 4 3 1 4 6 天数
7 6 5 4 3 2 1 分数
先做第4天,继续,做第2天,继续,第3大的分数与第1大的分数同是第4天,那就把第3大分数的天数降到第3天,
即 4 5 变成 3 5 ...
最后所以降到第0天的分数相加起来就是最后的答案。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 35;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int main()
{
    int n;
    ll sum;
    ll m;
    cin >> n;
    while(n--){
        bool visit[1001];
        Pii p[1001];
        cin >> m;
        sum = 0;
        mes(visit, false);
        for(int i = 0; i < m; ++i){
            p[i].sec = read();
        }
        for(int i = 0; i < m; ++i){
            p[i].fir = read();
        }
        sort(p, p+m);
        for(int i = m-1; i >= 0; --i){
            if(visit[p[i].sec]==false){
                visit[p[i].sec] = true;
                p[i].fir = 0;
            }
            if(visit[p[i].sec]&&p[i].fir!=0){
                while(visit[p[i].sec]){
                    --p[i].sec;
                }
                if(0 == p[i].sec){
                    sum+=p[i].fir;
                    continue;
                }
                visit[p[i].sec] = true;
            }
        }
        cout << sum << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值