poj 3779 Single CPU, multi-tasking(模拟)
来源:互联网 发布:金山数据恢复 收费 编辑:程序博客网 时间:2024/05/20 23:37
原题:http://poj.org/problem?id=3779
Single CPU, multi-tasking
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 467 Accepted: 140
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?
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 *lt;= 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.
The following 2×T lines represent T test cases. The first line of each test case is a integer N (0 < N *lt;= 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
这道题没什么好的方法,就是模拟一下过程,能优化就优化,看你的头脑风暴怎么样☺要YY哦~~
AC代码如下:
#include <cstdio>#include <cstring>#include <string>#include <iostream>using namespace std;#define max 10000000/*author:YangSirtime:2014/5/2*/int main(){int t,n,p,i,j,k,num;double a[125],b[125],sum,min;scanf("%d",&t);k=1;for(k=1;k<=t;k++){memset(b,0,sizeof(b));scanf("%d",&n);for(i=0;i<n;i++){scanf("%lf",&a[i]);}sum=0;while(1){min=max;for(j=0;j<n;j++){if(a[j]==0)continue;p=a[j];if(p==a[j])p--;if(p<min)min=p;}//求出最小的有1的情况for(j=0;j<n;j++){if(a[j]==0)continue;a[j]-=min;sum+=min;}for(j=0;j<n;j++){if(a[j]==0)continue;if(a[j]<=1)//小于1的情况{sum+=a[j];b[j]=sum;a[j]=0;}else//大于1的情况{a[j]--;sum++;}}for(i=0;i<n;i++){if(a[i])break;}if(i==n)break;//全为0}printf("Case %d:\n",k);for(i=0;i<n;i++){printf("%.2lf\n",b[i]);}}return 0;}
0 0
- poj 3779 Single CPU, multi-tasking(模拟)
- poj 3779 hdu 3413 Single CPU, multi-tasking
- hdu 3413 poj 3778 Single CPU, multi-tasking
- 3413 省赛 Single CPU, multi-tasking
- HDU 3413 Single CPU, multi-tasking
- HDU:3413 Single CPU, multi-tasking
- HDOJ 3413 Single CPU, multi-tasking
- hdu 3413——Single CPU, multi-tasking
- Linux的控制台(TTY/PTY)与多任务(MULTI-TASKING
- Multi tasking in Windows Phone 7.1
- Dr7.GD single/multi CPU version by deroko of ARTeam
- Python multi-process VS multi-thread (多核CPU利用率)
- Multi-Core CPU articles
- multi-process & cpu with multi-cores
- infineon XC2000 16位单片机的模拟EEPROM 在TASKING软件中的设置
- android multi process and single thread principle
- Multi-target tracking with Single Moving Camera
- single page app & multi page app
- Windows环境下我常用的命令
- 信息安全的深度与广度思考
- 复杂的心情
- Block知识深入
- awk命令学习
- poj 3779 Single CPU, multi-tasking(模拟)
- 手机网页插件精选--触摸日期选择、触摸列表
- 如何使用javadoc
- uva10061
- Redis各种数据结构内存占用测试
- ubuntu 12.04x下c++l连接MySQL入门,
- 微信订餐商城,微店系统.互联网第二个春天!
- ACM-SG函数之Fibonacci again and again——hdu1848
- tomcat配置共享session的Memcached