hdu 1789 贪心 优先队列

来源:互联网 发布:淘宝答题秒杀辅助工具 编辑:程序博客网 时间:2024/04/27 13:55

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

题解:

一开始自己想到的也是贪心,但是没想出思路。参考了别人的代码,写出来了,思路是
先按照扣分从大到小排序,分数相同则按照截止日期从小到大排序。。

然后按顺序,从截止日期开始往前找没有占用掉的时间。
如果找不到了,则加到罚分里面。
另一种思路是优先队列,先对pair排序,每一天最大的Score入队,如果碰到相同的Deadline,则每次进行出队操作,很妙的算法。

代码:

#include <bits/stdc++.h>using namespace std;const int maxn=1000+10;int use[maxn];typedef pair<int,int> p;bool cmp(p a,p b){    if(a.second!=b.second)     return a.second>b.second;    else     return a.first<b.first;}int main(){    int T;    cin>>T;    int n;    while(T--)    {       p data[maxn];       cin>>n;       for(int i=0;i<n;i++)          cin>>data[i].first;       for(int i=0;i<n;i++)          cin>>data[i].second;       sort(data,data+n,cmp);       int sum = 0,j;       memset(use,0,sizeof(use));       for(int i=0;i<n;i++)       {           for(j=data[i].first;j>0;j--)           {              if(!use[j])              {                  use[j] = 1;                  break;              }           }           if(j==0)            sum+=data[i].second;       }       cout<<sum<<endl;    }    return 0;}
#include <bits/stdc++.h>using namespace std;typedef pair<int,int> p;const int maxn = 1000+10;int main(){    int T;    cin>>T;int n;    p data[maxn];    while(T--)    {       cin>>n;       for(int i=0;i<n;i++)         cin>>data[i].first;       for(int i=0;i<n;i++)        cin>>data[i].second;        sort(data,data+n);        int day=0,ans=0;        priority_queue<int,vector<int>,greater<int> > pq;        for(int i=0;i<n;i++)        {            pq.push(data[i].second);            if(day<data[i].first)            {               day++;               continue;            }            int tmp = pq.top();            ans+=tmp;            pq.pop();        }        cout<<ans<<endl;    }    return 0;}
原创粉丝点击