hdu 3413

来源:互联网 发布:网络情感诈骗的方法 编辑:程序博客网 时间:2024/06/05 14:16

                                      Single CPU, multi-tasking

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 786    Accepted Submission(s): 260


Problem Description
Tuntun is a big fan of Apple Inc. who owns almost all kinds of products the company published. Fascinated by the amazing user experience and fabulous user interface, he spent every nickel of his pocket money on his iMac and iPhone. A few days ago, Apple released their latest iPhone OS 4.0. Tuntun noticed that the most significant new feature of iPhone OS 4.0 is multi-tasking support. He was curious about why the same device with a single core CPU can support multi-tasking under the new operating system. With his clever head, he found out a simple solution. The OS doesn’t have to let the CPU run several tasks exactly at the same time. What the OS should do is just to let the user feel that several tasks are running at the same time. In order to do that, the OS assigns the CPU to the tasks in turn. When the acts of reassigning a CPU from one task to another occur frequently enough, the illusion of parallelism is achieved. Let’s suppose that the OS makes each task run on the CPU for one millisecond every time in turn, and when a task is finished, the OS assigns the CPU to another task immediately. Now if there are 3 tasks which respectively need 1.5, 4.2 and 2.8 millisecond to complete, then the whole process is as follows:

At 0th millisecond, task 1 gets the CPU. After running for 1 millisecond, it still needs 0.5 milliseconds to complete.

At 1st millisecond, task 2 gets the CPU. After running for 1 millisecond, it still needs 3.2 milliseconds to complete.

At 2st millisecond, task 3 gets the CPU. After running for 1 millisecond, it still needs 1.8 milliseconds to complete.

At 3rd millisecond, task 1 comes back to CPU again. After 0.5 millisecond of running, it is finished and will never need the CPU.

At 3.5 millisecond, task 2 gets the CPU again. After running for 1 millisecond, it still needs 2.2 milliseconds to complete.

At 4.5 millisecond, it’s time for task 3 to run. After 1 millisecond, it still needs 0.8 milliseconds to complete.

At 5.5 millisecond, it’s time for task 2 to run. After 1 millisecond, it still needs 1.2 milliseconds to complete.

At 6.5 millisecond, time for task 3. It needs 0.8 millisecond to complete, so task 3 is finished at 7.3 milliseconds.

At 7.3 millisecond, task 2 takes the CPU and keeps running until it is finished.
At 8.5 millisecond, all tasks are finished.

Tuntun decided to make a simple iPhone multi-tasking OS himself, but at first, he needs to know the finishing time of every task. Can you help him?
 

Input
The first line contains only one integer T indicates the number of test cases.
The following 2×T lines represent T test cases. The first line of each test case is a integer N (0<N <= 100) which represents the number of tasks, and the second line contains N real numbers indicating the time needed for each task. The time is in milliseconds, greater than 0 and less than 10000000.
 

Output
For each test case, first output “Case N:”, N is the case No. starting form 1. Then print N lines each representing a task’s finishing time, in the order correspondent to the order of tasks in the input. The results should be rounded to 2 digits after decimal point, and you must keep 2 digits after the decimal point.
 

Sample Input
231.5 4.2 2.853.5 4.2 1.6 3.8 4.4
 

Sample Output
Case 1:3.508.507.30Case 2:14.1017.107.6015.9017.50题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3413题意: so easy! 就是给你一组任务完成所需时间,你必需从第一个任务开始,每个任务只能做一秒钟,依次做下去,直至做完。问每个任务完成的时间。思路: 随便都可以,但是你要能分析出可能出错的点。 我是用排序做的,先分析出任务做完的顺序,再一个一个求出答案。易错点: 1.当一个任务是整数时间时,要特别考虑清楚。 2.stl 库里的sort是稳定排序,但是此处的排序还是要特别注意;代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=111;double num[maxn],w[maxn];int t[maxn],x[maxn],y[maxn];double ans[maxn];bool cmp(int a,int b){    return x[a]<x[b];}int main(){    int T;    while(scanf("%d",&T)!=EOF)    for(int k=1;k<=T;k++)    {        int n;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%lf",&num[i]);            x[i]=num[i];        }        memset(y,0,sizeof(y));        memset(ans,0,sizeof(ans));        for(int i=1;i<n;i++)        for(int j=0;j<i;j++)        if(x[j]>x[i])            y[i]++;        for(int i=0;i<n;i++)            t[i]=i;        sort(t,t+n,cmp);        double total=0;        for(int i=0;i<n;i++)        {            double a=num[t[i]];            if((a-(int)a<1e-12))                ans[t[i]]=total+a+(n-i-1)*((int)a-1)+y[t[i]];            else                ans[t[i]]=total+a+(n-i-1)*(int)a+y[t[i]];            total+=a;        }        printf("Case %d:\n",k);        for(int i=0;i<n;i++)            printf("%.2lf\n",ans[i]);    }    return 0;}


0 0
原创粉丝点击