HDU 1789 - Doing Homework again

本文介绍了一个通过合理安排作业提交顺序来最小化扣分的算法。该算法采用贪心策略,优先处理分数较高的作业,并确保在截止日期前完成尽可能多的任务。
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和对应分数,问你怎样扣分最少;

解题思路:

  贪心,先做分数多的,再做分数少的,若发现deadline之前的日期全被占满了,那就扣分呗;

  以下有两个代码:(可能第二个看起来短一点) 

  代码一:

    按照时间处理,从最后一天开始往前分配,依次分配给满足deadline的分数最大的作业;

    最后扫一遍,没被分配的都扣分;

  代码二:

    按照作业处理,按 分数由大到小,同分数deadline由小到大 排序;

    然后从 分数最大的作业 开始分配,分配给离 deadline 最近的没有被占的日期;

    如果日期全被占了,就扣分咯;

 

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 struct P
 5 {
 6     int e;//deadline 
 7     int s;//score
 8 }s[1005];
 9 bool cmp(P x,P y){return x.e<y.e;}
10 int main()
11 {
12     int t,n;
13     cin>>t;
14     while(t--)
15     {
16         int flag[1005]={0},last=0,ans=0;
17         cin>>n;
18         for(int i=0;i<n;i++)
19         {
20             cin>>s[i].e;
21             if(s[i].e>last) last=s[i].e;
22         }
23         for(int i=0;i<n;i++)
24             cin>>s[i].s;
25 
26         for(int i=last;i>0;i--)//时间从后往前 
27         {
28             int max=0,temp;
29             for(int j=0;j<n;j++)
30             {
31                 if(s[j].e>=i && flag[j]==0 && max<s[j].s)//满足条件的最大 
32                 {
33                     temp=j;
34                     max=s[j].s;
35                 }
36             }
37             flag[temp]=1;
38         }
39         for(int i=0;i<n;i++)
40             if(flag[i]==0)
41                 ans+=s[i].s;
42         cout<<ans<<endl;
43     } return 0;
44 }
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 struct data{int time,score;}a[100001];
 7 int day[100001];
 8 int cmp(data a,data b){return (a.score>b.score||a.score==b.score&&a.time<b.time);}
 9 int main(){
10     int t,n,i;
11     cin>>t;
12     while(t--){
13         cin>>n;
14         memset(day,0,sizeof(day));
15         for(i=1;i<=n;i++) cin>>a[i].time;
16         for(i=1;i<=n;i++) cin>>a[i].score;
17         sort(a+1,a+n+1,cmp);
18         int ans=0;
19         for(i=1;i<=n;i++){//分配作业 
20             int j; 
21             for(j=a[i].time;j>=1;j--)
22                 if(!day[j]) {day[j]=1;break;}
23             if(j==0) ans+=a[i].score;
24         } cout<<ans<<endl; 
25     } return 0;
26 }

 

转载于:https://www.cnblogs.com/nicetomeetu/p/5158843.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值