zoj 1012 Mainframe

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


作业分配,


#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
    int a,b;
    int t,u;
    int v,w,x;
};
int cmp(const void *a,const void *b)   //按时间到达排序,时间相同报酬搞得优先
{
    node *a1=(node*)a;
    node *b1=(node*)b;
    if(a1->t!=b1->t)
    {
        return a1->t-b1->t;
    }
    else
    {
        return b1->v-a1->v;
    }
}
int main()
{
    int F,money;;
    int M,N,L,ans=0;
    node job[10001];
    bool finished[10001];     //标记作业完成情况
    while (cin>>F && F)
    {
        ans++;
        money=0;
        memset(finished, 0, sizeof(finished));
        cin>>M>>N>>L;
        for (int i=0; i<L; i++)
        {
            cin>>job[i].a>>job[i].b>>job[i].t>>job[i].u>>job[i].v>>job[i].w>>job[i].x;
        }
        qsort(job, L, sizeof(node), cmp);
        for (int i=0; i<F; i++)
        {
            int cpu=M;
            int memory=N;
            for (int j=0; j<L; j++)
            {
                if(job[j].t>i)
                {
                    break;    //作业还没有到达
                }
                if(!finished[j] && cpu>=job[j].a  && memory>=job[j].b)
                {
                    cpu-=job[j].a;
                    memory-=job[j].b;
                    finished[j]=1;
                    money+=job[j].v;
                    if(i+1<=job[j].u)
                    {
                        money+=(job[j].u-i-1)*job[j].w;   //计算奖金
                    }
                    else
                    {
                        money-=(i+1-job[j].u)*job[j].x;   //计算罚款
                    }
                }
            }
        }
        for (int i=0; i<L; i++)     //在时限F之前到达的作业没有运行的要罚款。
        {
            if(!finished[i]  && job[i].u<=F)
            {
                money-=(F-job[i].u)*job[i].x;
            }
        }
        cout<<"Case "<<ans<<": "<<money<<endl<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值