首页 > 其他分享 >(hdu step 9.1.2)Doing Homework again(贪心——有n份作业,每份作业都有一定的完成时间及没有完成时需要付出的代价,求最小代价)

(hdu step 9.1.2)Doing Homework again(贪心——有n份作业,每份作业都有一定的完成时间及没有完成时需要付出的代价,求最小代价)

时间:2023-05-07 22:06:28浏览次数:38  
标签:again int 作业 score deadline 完成 homeworks 代价


题目:


Doing Homework again

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 63 Accepted Submission(s): 57

 

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



333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4



 

Sample Output



035



 

Author


lcy


 

Source


2007省赛集训队练习赛(10)_以此感谢DOOMIII


 

Recommend


lcy


 



题目分析:

               贪心。贪心策略为:如果完成时间不同,完成时间紧迫的排在前面。如果完成时间相同,那么付出代价大的排在前面。其实到这里,只是保证了完成的作业书尽可能的多,但是,并没有保证付出的代价最小。当一份作业可能不能完成时,我们需要在前面寻找一份代价比当前作业不做所付出的代价要小的作业舍弃掉。


代码如下:


/*
 * b.cpp
 *
 *  Created on: 2015年3月26日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <algorithm>


using namespace std;

const int maxn = 1001;

struct Homework{
	int deadline;
	int score;
	bool flag;
}homeworks[maxn];

bool cmp(Homework a,Homework b){
	if(a.deadline != b.deadline){//如果作业的完成时间不同
		return a.deadline < b.deadline;//那么完成时间紧迫的作业排在前面
	}

	return a.score > b.score;//如果完成时间相同,那么没完成时扣掉的分数多的排在前面
}


int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);

		int i;
		for(i = 0 ; i < n ; ++i){//依次读入每个作业的完成时间
			scanf("%d",&homeworks[i].deadline);
		}

		for(i = 0 ; i < n ; ++i){//一次读入每个作业没完成所需要付出的代价
			scanf("%d",&homeworks[i].score);
			homeworks[i].flag = true;//所有的作业一开始默认都是可以完成的
		}

		sort(homeworks,homeworks+n,cmp);//将作业按照优先级进行排序

		int k = 1;//当前天数
		int sum = 0;//所需要付出的代价
		for(i = 0 ; i < n ; ++i){//遍历所有的作业
			if(homeworks[i].deadline >= k){//如果当前作业的完成时间在当前天数的后面
				k++;//那么证明当前作业可以完成
				continue;//寻找下一个作业
			}
			//以下代码可以执行,佐鸣找到了一份不能完成的作业

			int p = homeworks[i].score;//标记没完成当前这份作业所需要付出的代价
			int pos = i;//标记当前作业的索引
			int j;
			//寻找一份作业,该作业如果不做的话,所付出的代价最小
			for(j = 0 ; j < i ; ++j){//遍历改作业之前的左右作业
				//如果找到一份作业如果不做的话,付出的代价<当前作业不做所付出的代价的话 && 该作业被做了
				if(homeworks[j].score < p && homeworks[j].flag == true){
					p = homeworks[j].score;//更新当前需要付出的代价
					pos = j;//记录该作业的索引
				}
			}

			sum += p;//将总代价 加上被舍弃掉的作业所需要付出的代价
			homeworks[pos].flag = false;//将该作业标记为未完成(被舍弃掉)
		}

		printf("%d\n",sum);
	}

	return 0;
}






标签:again,int,作业,score,deadline,完成,homeworks,代价
From: https://blog.51cto.com/u_5290007/6252587

相关文章

  • Robust Deep Reinforcement Learning against Adversarial Perturbations on State Ob
    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布!NeurIPS2020 ......
  • N74第四周作业
    1.总结脚本高级命令trap,install,mktemp,expect,进程优先级命令:nice,renice,进程管理工具:ps,pstree,pidstat,pgrep,pidof,uptime,mpstat,top,htop,free,pmap,vmstat,iostat,iotop,iftop,nload,nethogs,iptraf-ng,dstat,glances,cockpit,kill,jobs,任......
  • 5471: 数据结构实验--图的最小代价生成树 prim
    描述  求带权无向图的最小代价生成树。    输入  输入数据为多组,每组数据包含多行,第一行为2个整数n,e,n为图的顶点数,e为边数,接下来是e行,每行3个整数,前两个整数是一个顶点对,代表一条边所依附的两个顶点,第3个整数是边的权值。所有值不超过20。  输出......
  • 6343.前往目标的最小代价-343
    前往目标的最小代价给你一个数组start,其中start=[startX,startY]表示你的初始位置位于二维空间上的(startX,startY)。另给你一个数组target,其中target=[targetX,targetY]表示你的目标位置(targetX,targetY)。从位置(x1,y1)到空间中任一其他位置(x2,y2)......
  • 操作系统(3.1)--处理机调度和作业
    一、处理机调度层次1.高级调度(HighLevelScheduling)高级调度又称长程调度或作业调度,它的调度对象是作业。其主要功能是根据某种算法,决定将外存上处于后备队列中的哪几个作业调入内存,为它们创建进程、分配必要的资源,并将它们放入就绪队列。主要用于多道批处理系统中,而在分时和实......
  • 正则表达式引发的惨痛代价
    关注Java后端技术栈“回复“面试”获取最新资料案例在一次小型项目开发中,我遇到过这样一个问题。为了宣传新品,我们开发了一个小程序,按照之前评估的访问量,这次活动预计参与用户量30W+,TPS(每秒事务处理量)最高3000左右。这个结果来自我对接口做的微基准性能测试。我习惯使用ab工具......
  • 12、freestyle风格的流水线作业回顾
    freestyle风格的流水线作业回顾回顾:流水线作业:FreestyleJob:Jenkins1.x,开放式UI,手动MavenJobPipelineJob:Jenkins2.x,开放式编码,定义流水线maven工程spring-boot-helloworld克隆、构建、......
  • 操作系统(3.2.1)--作业调度的主要任务
    作业调度的主要功能是根据作业控制块中的信息,审查系统能否满足用户作业的资源需求,以及按照一定的算法,从外存的后备队列中选取某些作业调入内存,并为它们创建进程、分配必要的资源。然后再将新创建的进程插入就绪队列,准备执行。有时也把作业调度称为接纳调度(AdmissionScheduling)。......
  • 使用TSQL创建作业
    使用TSQL创建作业<scriptlanguage="javascript"type="text/javascript">document.title="使用TSQL创建作业-"+document.title</script>具体帖子记不清了,在原来的基础上修改了一点:ifexists(select*fromdbo.sysobjectswhereid=object_id(N......
  • Add Again UVA - 11076
     defineS,itissumofallpossiblepermutationsofagivensetofdigits.Forexample,ifthedigitsare<123>,thensixpossiblepermutationsare<123>,<132>,<213>,<231>,<312>,<321>andthesumofthemis......