题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789

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

解题思路:典型的贪心策略。因为要使得扣分最少,所以要先排个序,规则是:如果期限相同,对扣分多的从大到小排列,如果扣分相同,则将期限从小到大排列。最优策略:每门功课最好在给定的deadline当天就完成,如不能完成,只能往前找哪一天还没使用,尽量使得做这门功课的日期越大越好,即从其截止日期到第一天,如果一路遍历都已经被标记过了,到最后j==0说明已经没有足够的一天时间给他做这门功课,那么就将这门功课扣的分数加到ans中,最终的ans即为最小扣分值。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
struct NODE
{
int deadline,reduce;
}node[];
bool cmp(NODE a,NODE b)
{
if(a.reduce!=b.reduce)return a.reduce>b.reduce;//reduce越多的越靠前,先解决扣分多的
return a.deadline<b.deadline;//reduce相同时,deadline越早越靠前,从小到大
}
bool vis[];//如果当天没用过,值为false;否则为true
int main()
{
int T,N,ans,j;//ans是保存减少的reduce
cin>>T;
while(T--){
memset(vis,false,sizeof(vis));
cin>>N;
for(int i=;i<N;i++)cin>>node[i].deadline;
for(int i=;i<N;i++)cin>>node[i].reduce;
sort(node,node+N,cmp);//按规则排序
ans=;
for(int i=;i<N;i++){
for(j=node[i].deadline;j>;j--)//从截止时间开始往前推,如果有一天没用过,这一天就做这一门课,那么这门课不扣分
if(!vis[j]){vis[j]=true;break;}
if(j==)ans+=node[i].reduce;//如果j=0,表明从deadline往前的每一天都被占用了,这门课完不成
}
cout<<ans<<endl;
}
return ;
}

最新文章

  1. 敏捷遇上UML-需求分析及软件设计最佳实践(郑州站 2014-6-7)
  2. T-SQL优化
  3. iOS 时间处理(转)
  4. 简单的css居中问题(日常记录)
  5. java二维数组的定义
  6. dedecms手机站要同步pc站的图片
  7. 如何使用数据卷在宿主机和docker容器之间共享文件
  8. ArcEngine查询、添加、删除要素的方法
  9. 《CSS网站布局实录》学习笔记(四)
  10. d3可视化实战04:事件绑定机制
  11. [STL] day 1~2 Problem Set
  12. JavaScript学习笔记(十)——高阶函数之map,reduce,filter,sort
  13. FPGA与MATLAB数据交互高效率验证算法——仿真阶段
  14. Java I/O流详解与应用
  15. XSS 绕过技术
  16. Array.find()和Array.findIndex()
  17. ~/Library/MobileDevice/Provisioning Profiles
  18. node爬取html乱码
  19. maya中MFnMesh.h使用说明的翻译
  20. http错误代码提示

热门文章

  1. ZOJ 3632 Watermelon Full of Water (线段树 区间更新 + dp)
  2. 迅雷CTO李金波:致创业者的一封信
  3. url优化|隐藏index.php
  4. Idea SpringMVC+Spring+MyBatis+Maven整合
  5. POJ训练计划2299_Ultra-QuickSort(线段树/单点更新)
  6. 【bzoj1015】【JSOI2008】【星球大战】【并查集+离线】
  7. asp.net mvc + javascript导入文件内容
  8. Tomcat9无法启动
  9. linux kernel编译配置相关
  10. AWS携手上海嘉定政府推出首个联合孵化器 为创业公司拓展AWS云服务可用资源