📣 独立日限时特惠: VIP通行证立减$68
12
返回列表 发新帖
楼主: leoyue
跳转到指定楼层
上一主题 下一主题
收起左侧

Amazon OA 2 Round Robin 求助

🔗
pengzewen37 2016-1-5 12:36:07 | 只看该作者
全局:
leoyue 发表于 2016-1-4 23:34
不。。。是他们写的java跑出来也是1。。。但是我改写成c++就错了

额。。。那那个1.25是哪来的 诡异
回复

使用道具 举报

🔗
 楼主| leoyue 2016-1-5 13:02:02 | 只看该作者
全局:
pengzewen37 发表于 2016-1-5 12:36
额。。。那那个1.25是哪来的 诡异

我写错了。。。
回复

使用道具 举报

全局:
class Process{
  int arriveTime;
  int executeTime;
  Process(int arr, int exe){
    this.arriveTime = arr;
    this.executeTime = exe;
}
}

public static double roundRobin(int[] arrive, int[] execute, int q){
                if(arrive == null || execute == null || arrive.length != execute.length)
                        return 0.0;
                int len = arrive.length;
                int currTime = 0;
                int waitTime = 0;
                int i = 0;
                PriorityQueue<Process> pq = new PriorityQueue<Process>(len, new Comparator<Process>(){
                        @Override
                        public int compare(Process p1, Process p2){
                                return p1.arriveTime - p2.arriveTime;
                               
                        }

                       
                });
               
                while(!pq.isEmpty()  || i < len){
                        if(!pq.isEmpty()){
                                Process tmp = pq.poll();
                                waitTime += currTime - tmp.arriveTime;
                                if(tmp.executeTime > q){
                                        currTime += q;
                                        pq.offer(new Process(currTime,tmp.executeTime - q));
                                }else{
                                        currTime += tmp.executeTime;
                                }
                                for(; i < len; i++){
                                        if(arrive[i] <= currTime){
                                                pq.offer(new Process(arrive[i],execute[i]));
                                        }else
                                                break;
                                }
                        }else{
                                currTime = arrive[i];
                                pq.offer(new Process(arrive[i],execute[i]));
                                i++;
                        }
                }
                return waitTime * 1.0 / len;
               
        }

补充内容 (2016-1-5 13:15):
不知道对不对,还望楼主指出
回复

使用道具 举报

🔗
dingmyue 2016-1-5 17:26:09 | 只看该作者
全局:
BrilliantBean 发表于 2016-1-5 13:13
class Process{
  int arriveTime;
  int executeTime;

else currTime += temp.executeTime改成 curTime += Math.min(tmp.executeTime, q)试试呢  不要写else 因为不管execute多久 都是要更新curTime的
还有就是 那个if () {} 应该放在for loop的后面
回复

使用道具 举报

🔗
wbcustc 2016-1-6 07:27:07 | 只看该作者
全局:
public class Event{
            int arrive;
            int execute;
            public Event(int arrive,int execute){
                    this.arrive = arrive;
                    this.execute = execute;
            }
    }
   
    public double roundRobin(int[] arrive,int[] execute,int limit){
            int n = arrive.length;
            Event[] events = new Event[n];
            for(int i = 0;i<n;i++){
                    events[i] = new Event(arrive[i],execute[i]);
            }
            Arrays.sort(events,new Comparator<Event>(){
                    public int compare(Event e1,Event e2){
                            return e1.arrive-e2.arrive;
                    }
            });
            int nextIndex = 1,currTime = events[0].arrive,waitTime = 0;
            Queue<Event> queue = new LinkedList<>();
            queue.offer(events[0]);
            while(!queue.isEmpty()||nextIndex<n){
                    if(!queue.isEmpty()){
                            Event currEvent = queue.poll();
                            waitTime += currTime-currEvent.arrive;
                            boolean finish = true;
                            if(currEvent.execute<=limit){
                                    currTime += currEvent.execute;       
                            }else{
                                    currTime += limit;
                                    finish = false;
                            }
                           
                            for(int i = nextIndex;i<n;i++){
                                    if(events[i].arrive<=currTime){
                                            queue.offer(events[i]);
                                            nextIndex = i+1;
                                    }else{
                                            break;
                                    }
                            }
                           
                              if(!finish){
                                    Event remain = new Event(currTime,currEvent.execute-limit);
                                    queue.offer(remain);
                            }
                    }else{
                            currTime = events[nextIndex].arrive;
                            queue.offer(events[nextIndex]);
                            nextIndex++;
                    }
                     
            }
            return waitTime*1.0/n;
    }

这个代码debug过很多test case 应该没问题
回复

使用道具 举报

🔗
citynart 2016-1-26 07:34:51 | 只看该作者
全局:
你的是对的,你看的面经不对
回复

使用道具 举报

🔗
llsshh1128 2016-2-23 07:08:54 | 只看该作者
全局:
request time: [0,2,4,5]
duration: [7,4,1,4]
q=3
average waiting time : 7

地理给的面经运行出来是7,但我自己手算是6.25啊~
很捉急啊,要考oa2了,谁能给我解释一下啊,谢谢啦!!!
回复

使用道具 举报

🔗
z165153 2016-7-29 12:38:44 | 只看该作者
全局:
leoyue 发表于 2016-1-5 12:23
http://www.1point3acres.com/bbs/thread-144891-1-1.html 给的附件,还有另一个之前的总结贴吧,反正算 ...

lz你好。请问改c++的时候,数组的长度你是怎么得到的呀?还请赐教哈。
nextProIdx < arrive.length
回复

使用道具 举报

🔗
phonger 2017-1-29 16:02:27 | 只看该作者
全局:
llsshh1128 发表于 2016-2-23 07:08
request time: [0,2,4,5]
duration: [7,4,1,4]
q=3

需要注意的是: 当第一次执行完task 0的时候,task 2 和task 3还没到来,所以task0的第二次执行在task2之前,task的执行顺序如下: task queue
0
1,0
0,2,3,1
2,3,1,0
3,1,0
1,0,3
0,3
3
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表