您的位置:首页 > 大数据 > 人工智能

ZOJ_1012_Mainframe

2015-08-01 22:08 267 查看
Mainframe

Time Limit: 10 Seconds Memory Limit: 32768 KB

Mr. Ronald is responsible for the administration of the mainframe in ACM (Agent on Computing of Mathematics). The agent undertakes the mathematical computing jobs from some companies,
and gain the rewards after has fulfilled the jobs on the mainframe. So the mainframe is very valuable to ACM. Mr. Ronald is required to arrange the order of those jobs running on mainframe. Once a job is to run, he examines the free resources for the job.
If the resources meet the job's requirement, he assigns those resources to the job. Otherwise, the job will be suspended until there are enough resources.



Because of unfamiliar with the task at first, he turned everything upside down. As time went by, he became competent on it. Moreover, he had concluded a set of byelaw as following:
1. The mainframe has M CPUs and N memories can be assigned.

2. There exists a queue for the jobs waiting to be executed. You may assume the queue is large enough to hold all the waiting jobs.

3. A job Ji which need Ai CPUs and Bi memories, reaches the queue on time Ti. The job is required to be accomplished before time Ui. After successfully completed, ACM may get Vi($) as the reward. If it finishes before the timeline, the extra bonus is Wi($)
per hour. If the job is late, the punishment is Xi($) per hour. For example, we may assume that a job's value is 10$, its timeline is 8, and the punishment is 2$ per hour. If the job is completed at time 10, ACM will get 10-(10-8)*2=6$.

4. When the job start executing, the required CPUs and memories are seized by this job, and couldn't be assigned again for the other job to be executed simultaneously. After completing the job, those resources will be released. If the resources are enough,
more jobs could be executed simultaneously.

5. For the sake of the share in the mainframe's computing capability, each job will be finished just in an hour from the start of executing. You may assume each job costs exactly one hour.

6. When there are no jobs to be executed, the mainframe will be idle until a job arrives at the job queue.

7. If there are more than one jobs arrive at the queue, the more valuable job will be executed first. You may assume the values of the jobs are always unequal (Vi��Vj).

8. If the free CPUs or memories couldn't satisfy the requirement of the job, the job will be suspended for an hour without occupying any resources. An hour later, the resources will be examined again for this job, regardless the other jobs in the queue. If
the requirement unsatisfied again, it remains suspended for the next hour, and other jobs in the queue will try to be assigned the resources. Otherwise the job will seize the required CPUs and memories and start executing.

9. When more than one jobs are suspended, the earlier arrived will try to be assigned first.
Using the byelaw, Mr. Ronald may deal with the routines very well. But now, besides the routines, ACM ask him to compute the income according to the job list. Given the timeline F, he
has to calculate the jobs that had been executed or should be executed. Of course, according to job Ji, if Ui>F and the job hadn't been executed, it shouldn't been taken into account; but those which had been executed or Ui<=F should been counted. If the job
hadn't been executed, it will not bring ACM any value, which means only punishment to the timeline should be calculated.

Indeed, his programming ability is not good enough, and he does not like to compute manually. So he is uneasy about it. Could you help him to solve this problem?


Input


The input contains several test cases, each of which describes the mainframe's resources and the job list. Each test case begins with a line containing a single integer F, 0 <= F <= 10000, the time line. The following line consists of three integers M, N and
L (M, N, L >= 0). M is the number of CPU in the mainframe, and N is the memory size. L represents the number of jobs in the job list. There will be 10000 jobs at most.

The subsequent L lines in the test case describe the information of the jobs. The data which describing job Ji consist of 7 integers Ai, Bi, Ti, Ui, Vi, Wi, Xi. Ai and Bi indicate the requirements on CPU and memory (Ai, Bi >= 0). Ti and Ui indicate the job's
arriving time and the timeline (0 <= Ti <= Ui). Vi, Wi, Xi are the reward, bonus and punishment of the job (Vi, Wi, Xi >= 0).

The input file ends with an empty test case (F=0). And this case should not be processed.

Output

Your program must compute the total income of the mainframe according to the job list. For each test case, print the case number, a colon, and a white space, then the income.

Print a blank line after each test case.

Note: Don't count the jobs which hadn't been executed, and their timelines are later than F.

Sample Input

10

4 256 3

1 16 2 3 10 5 6

2 128 2 4 30 10 5

2 128 2 4 20 10 5

0

Output for the Sample Input



Case 1: 74

Source: Asia 2001, Shanghai (Mainland China)

目前做过的最长的一个题目……

真的是看起来令人生畏……

侧面说明了还是英语不够好

翻译一发大意

1.有这样一个主机M个CPU总内存N

2.有一个足够长的程序执行队列

3.有这样一个工作Ji需要花费Ai个CPU和Bi内存在Ti的时候到达队列,允许完成的期限Ui

完成时获得Vi的钱,如果提前期限每个小时奖励Wi,拖后则每小时罚Xi

4.一个工作一旦开始执行就要占用它的资源,别的工作不能再使用

工作完成释放占用的CPU和内存

5.每个工作需要执行1小时

6.没有工作的时候主机空闲

7.当多个工作同时到达时,Vi即价值大的工作先执行

8.资源不够某个到达的工作执行时,这个工作将被暂停,不会浪费资源

将在下一个时间点重新检测资源

9.多个等待的工作优先到达时间靠前的工作检查资源

特别注意,结束时间晚于终止时间且没有做的工作不计算惩罚。

结束时间早于终止时间的没有执行的工作,计算惩罚到终止时间

输出注意每个块之后有空行。没什么好多说的。

做法就是从0小时一小时一小时刷过去就好了

看到别人有写链表的,其实这个题目的要求没有那么苛刻,写个数组判断执行过工作就可以了。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

const int M=1e4+5;

struct JOB
{
int cm,memo,st,ed;
int rew,bo,pu;
}job[M];

int isu[M];

bool cmp(JOB a,JOB b)             //比较函数不要想当然,这里符号方向错了一次……
{
if(a.st==b.st)
return a.rew>b.rew;
return a.st<b.st;
}

int main()
{
// freopen("A_in.txt","r",stdin);
int f,memo,n,l;
int money;
int ca=1;
while(1)
{
scanf("%d",&f);
money=0;
if(f==0)
break;
memset(isu,0,sizeof(isu));
scanf("%d%d%d",&n,&memo,&l);
for(int i=0;i<l;i++)
{
scanf("%d%d%d%d",&job[i].cm,&job[i].memo,&job[i].st,&job[i].ed);
scanf("%d%d%d",&job[i].rew,&job[i].bo,&job[i].pu);
}
sort(job,job+l,cmp);          //预处理按照到达时间第一优先从小到大价值第二从大到小
//        for(int i=0;i<l;i++)
//            printf("%d,",job[i].rew);
//        printf("\n");
for(int t=0;t<f;t++)           //f就结束了,最后一小时是f-1开始
{
int mme=memo,nn=n;
for(int i=0;i<l;i++)
{
if(isu[i])
continue;
if(job[i].st>t)
continue;
if(job[i].memo<=mme&&job[i].cm<=nn)
{
//cout<<"do job"<<i<<endl;
mme-=job[i].memo;
nn-=job[i].cm;
isu[i]=1;
money+=job[i].rew;
if(job[i].ed>t+1)
money+=job[i].bo*(job[i].ed-t-1);
if(job[i].ed<t+1)
money-=job[i].pu*(t+1-job[i].ed);        //这里写翻一次,注意大小关系
//cout<<money<<endl;
}
}
}
for(int i=0;i<l;i++)
{
if(isu[i])
continue;
if(job[i].ed>f)
continue;
money-=job[i].pu*(f-job[i].ed);
}
printf("Case %d: %d\n\n",ca,money);
ca++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: