C++ 磁盘调度程序扫描算法错误

C++ 磁盘调度程序扫描算法错误,c++,algorithm,scheduling,scheduler,C++,Algorithm,Scheduling,Scheduler,我有一个文件,其中有一行,我想做扫描算法(电梯)来计算总距离 队列大小为32,总数据线为35691 左端为0 右端是59999 ex: queue size is 3 start from 0 left end is 0 right end is 255 all request:30, 150, 30, 10, 70 1. head:0 quene:30, 150, 30 distance:0 2. head:30 quene:150, 30, 10 distance:30 3. h

我有一个文件,其中有一行,我想做扫描算法(电梯)来计算总距离

队列大小为32,总数据线为35691

左端为0

右端是59999

ex:

queue size is 3

start from 0

left end is 0

right end is 255

all request:30, 150, 30, 10, 70

1.
head:0
quene:30, 150, 30
distance:0
2.
head:30
quene:150, 30, 10
distance:30
3.
head:30
quene:150, 10, 70
distance:30
4.
head:70
quene:150, 10
distance:70
5.
head:150
quene:10
distance:150
6.
head:10
quene:
distance:500(150 to right end 255 and come back to 10 --> 150 + (255 - 150) * 2 + (150 - 10))
我做的是下面的代码,我使用multi-set来存储它

the first stage I fill up the queue

the second stage I insert the next element first

if the direction is right now

to see whether there is element next to the current, if not, change the direction, else keep right moving.

if the direction is left now

do the same thing above but go to left 

the third stage I will calculate the remaining element in the queue.
我遇到的问题是我在

second stage is larger the result
,所以可能是出了什么问题。我认为我所认为的是正确的,我无法理解

错误在哪里

最终结果应为33055962

以及守则:

#include <iostream>
#include <fstream>
#include <string>
#include <set>
const int dataSize = 35691;
const int queueSize = 32;
const int minNumber = 0;
const int maxNumber = 59999;
using namespace std;

int number = minNumber;
int direction = 1;// right
int distanceSum = 0;
multiset<int> myset;
multiset<int>::iterator it;
multiset<int>::iterator temp;
multiset<int>::iterator next;
void print(void);

int main(int argc, char const *argv[]){
    ifstream myfile("sort");
    if (myfile.is_open()){
// ===============================initialization===============================
        for(int i = 0; i < queueSize; i ++){
            myfile >> number;
            myset.insert(number);           
        }
        it = myset.begin();
        int last = minNumber;
        int current = *it;
// ===============================middle stage===============================
        for(int i = 0; i < dataSize - queueSize; i ++){
            myfile >> number;
            myset.insert(number);
            current = *it;
            if(direction == 1){// right
                next = it;
                next ++;
                if(next == myset.end()){// right most
                    direction = 0;// change direction
                    distanceSum += ((maxNumber - current) * 2 + (current - last));
                    temp = it;
                    it --;
                    myset.erase(temp);
                    last = current;
                }
                else{
                    distanceSum += (current - last);
                    temp = it;
                    it ++;
                    myset.erase(temp);
                    last = current;
                }
            }
            else if(direction == 0){// left
                if(it == myset.begin()){// left most
                    direction = 1;// change direction
                    distanceSum += ((current - minNumber) * 2 + (last - current));
                    temp = it;
                    it ++;
                    myset.erase(temp);
                    last = current;
                }
                else{
                    distanceSum += (last - current);
                    temp = it;
                    it --;
                    myset.erase(temp);
                    last = current;
                }
            }       
        }
// ===============================remaining===============================
        // for(int i = 0; i < queueSize; i ++){
        //  current = *it;
        //  if(direction == 1){// right
        //      next = it;
        //      next ++;
        //      if(next == myset.end()){
        //          direction = 0;
        //          if(myset.size() == 1)distanceSum += (current - last);
        //          else distanceSum += ((maxNumber - current) * 2 + (current - last));
        //          temp = it;
        //          it --;
        //          myset.erase(temp);
        //          last = current;
        //      }
        //      else{
        //          distanceSum += (current - last);
        //          temp = it;
        //          it ++;
        //          myset.erase(temp);
        //          last = current;
        //      }
        //  }
        //  else if(direction == 0){

        //      if(it == myset.begin()){
        //          direction = 1;
        //          if(myset.size() == 1)distanceSum += (last - current);
        //          else distanceSum += ((current - minNumber) * 2 + (last - current));
        //          temp = it;
        //          it ++;
        //          myset.erase(temp);
        //          last = current;
        //      }
        //      else{
        //          distanceSum += (last - current);
        //          temp = it;
        //          it --;
        //          myset.erase(temp);
        //          last = current;
        //      }
        //  }       
        // }
        myfile.close();
    }
    else cout << "Unable to open file";
    print();
    cout << "distanceSum is :" << distanceSum << endl;
    return 0;
}
void print(){
    cout << "value:" << endl;
    for(multiset<int>::iterator it = myset.begin(); it != myset.end(); it ++){
        cout << *it << "\t"; 
    }
    cout << endl;
    cout << "current point to:" << *it << endl;
    cout << "total size is:" << myset.size() << endl;
    cout << "current distance:" << distanceSum << endl;
}
#包括
#包括
#包括
#包括
常数int dataSize=35691;
常量int queueSize=32;
常量int minNumber=0;
常量int maxNumber=59999;
使用名称空间std;
整数=minNumber;
int方向=1;//正确的
int distanceSum=0;
多集myset;
multiset::迭代器it;
迭代器温度;
multiset::迭代器next;
作废打印(作废);
int main(int argc,char const*argv[]{
ifstream myfile(“排序”);
如果(myfile.is_open()){
//===========================================初始化===============================
对于(int i=0;i>编号;
myset.insert(数字);
}
it=myset.begin();
int last=minNumber;
int current=*it;
//============================================================中期===============================
对于(int i=0;i>编号;
myset.insert(数字);
电流=*它;
如果(方向==1){//右
下一个=它;
next++;
如果(next==myset.end()){//最右边
方向=0;//更改方向
距离和+=((最大数字-当前)*2+(当前-最后一次));
温度=它;
它--;
myset.erase(临时);
last=当前值;
}
否则{
距离总和+=(当前-最后);
温度=它;
it++;
myset.erase(临时);
last=当前值;
}
}
如果(方向==0){//左
如果(it==myset.begin()){//最左边
方向=1;//更改方向
距离总和+=((当前-最小数)*2+(最后一个-当前));
温度=它;
it++;
myset.erase(临时);
last=当前值;
}
否则{
距离总和+=(上次-当前);
温度=它;
它--;
myset.erase(临时);
last=当前值;
}
}       
}
//====================================================剩余===============================
//对于(int i=0;i现在我已经读了你的算法三次了,我发现的唯一错误是当你读相同的值时,考虑这个,

  • 你在300号位置向左移动
  • 您在队列中插入新元素,该队列也是位置“300”

    Now in the multiset implementation according to this SO answer
    (http://stackoverflow.com/q/2643473) depending on the implementation
    the value can either go right to your present value(C++ 0x) or can go
    anywhere(C++03). 
    
  • 因此,如果您处于位置300,并且假设新值插入到multiset中当前值的右侧

  • 检查左侧并移动到较小的元素,或者如果您位于最左侧位置,则在向下移动到位置0后开始右扫。而您应该首先访问队列中新的位置300中的值
当您位于右侧并且在当前值的左侧插入相同的值时,也会发生相同的错误

以下是使用队列大小为3的示例。
考虑你的多集队列有值(300700900)和左边的方向=0。右边的星形的NuMeBr指示迭代器在哪里。让我们假设TooStuePosieType=0此时。

  • 您当前的队列(300*、700、900),总扫描距离=0,方向=0
  • 读取300并将其插入队列(300*、300、700、900),totalSweepDistance=0,direction=0
  • 检查队列的开头,它返回true。将移动的扫描距离添加到0,将方向更改为右侧,然后达到300,这使扫描距离=600
  • 你现在