HDU1789 doing homework again(贪心)

来源:互联网 发布:大数据金融的风险 编辑:程序博客网 时间:2024/05/01 19:36
Doing Homework againTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11806    Accepted Submission(s): 6948Problem DescriptionIgnatius 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.InputThe 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.OutputFor each test case, you should output the smallest total reduced score, one line per test case.Sample Input333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4Sample Output035
这道题就用到贪心策略,我们把分数从大到小排个序,每次我们分数最高的那天的作业先做,然后标记该天已经做过,然后继续循环,找第2高分数的天数,如果与第一个高分数的天数是同一天的话,那就降到前一天,比较大小,先做这一天分数最大的,一直下去,求出最小扣分。eg:1 4 6 4 2 4 33 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|1typedef 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