Single CPU, multi-tasking
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 93 Accepted Submission(s): 40
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?
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.
2 3 1.5 4.2 2.8 5 3.5 4.2 1.6 3.8 4.4
Case 1: 3.50 8.50 7.30 Case 2: 14.10 17.10 7.60 15.90 17.50
错误的程序竟然过了
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct T
{
int id;
int kk;
double num;
}t[101],s[101];
bool cmp(T a,T b)
{
return a.kk<b.kk;
}
bool cmp1(T a,T b)
{
return a.id<=b.id;
}
int main()
{
int T,n,i,j;
scanf("%d",&T);
int cas=1;
while(T--)
{
double max=-1;
int temp;
double sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%lf",&t[i].num);
t[i].id=i;
sum+=t[i].num;
int k=(int)t[i].num;
if(t[i].num-k<0.00000001) t[i].kk=k;//排序极其讲究,注意3.999,3.99应该作为4来处理
else t[i].kk=k+1;
if(t[i].kk>=max) max=t[i].kk,temp=i;
}
sort(t,t+n,cmp);
s[0].num=sum;
s[0].id=temp;
int k=1;
for(i=n-2;i>=0;i--)
{
sum-=t[i+1].num;
double ss=sum;
int d,dd;
int tt=(int)(t[i].num);
if(t[i].num-tt<0.000000001) d=tt-1,dd=tt;
else d=tt,dd=tt+1;
for(j=i+1;j<n;j++)
if(t[j].id>t[i].id) ss+=d;
else ss+=dd;
s[k].num=ss;
s[k++].id=t[i].id;
}
sort(s,s+n,cmp1);
printf("Case %d:/n",cas++);
for(i=0;i<n;i++)
printf("%0.2lf/n",s[i].num);
}
return 0;
}
正确的程序
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct T
{
int id;
int kk;
double num;
}t[101],s[101];
bool cmp(T a,T b)
{
if(a.kk==b.kk) return a.id<b.id;
return a.kk<b.kk;
}
bool cmp1(T a,T b)
{
return a.id<=b.id;
}
int main()
{
int T,n,i,j;
scanf("%d",&T);
int cas=1;
while(T--)
{
double max=-1;
int temp;
double sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%lf",&t[i].num);
t[i].id=i;
sum+=t[i].num;
int k=(int)t[i].num;
if(t[i].num-k<0.00000001) t[i].kk=k;
else t[i].kk=k+1;
if(t[i].kk>=max) max=t[i].kk,temp=i;
}
sort(t,t+n,cmp);
s[0].num=sum;
s[0].id=temp;
int k=1;
for(i=n-2;i>=0;i--)
{
sum-=t[i+1].num;
double ss=sum;
int d,dd;
int tt=(int)(t[i].num);
if(t[i].num-tt<0.000000001) d=tt-1,dd=tt;
else d=tt,dd=tt+1;
for(j=i+1;j<n;j++)
if(t[j].id>t[i].id) ss+=d;
else ss+=dd;
s[k].num=ss;
s[k++].id=t[i].id;
}
sort(s,s+n,cmp1);
printf("Case %d:/n",cas++);
for(i=0;i<n;i++)
printf("%0.2lf/n",s[i].num);
}
return 0;
}