Note that there are some explanatory texts on larger screens.

plurals
  1. POdisk scheduler SCAN algorithm bug
    primarykey
    data
    text
    <p>I have a file which have a number one line, and I want to do the scan algorithm(elevator) to calculate the total distance.</p> <p>the queue size is 32 and total data line is 35691</p> <p>left end is 0</p> <p>and right end is 59999</p> <pre><code>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 --&gt; 150 + (255 - 150) * 2 + (150 - 10)) </code></pre> <p>what I do is the following code, I use the multi set to store it, </p> <pre><code>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. </code></pre> <p>What I encounter the problem is the distance that I calculate in the </p> <pre><code>second stage is larger the result </code></pre> <p>, so it may something wrong. And I think that what I think is right and I cannot figure out </p> <p>where is the error.</p> <p>The final result should be 33055962.</p> <p>And the code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;set&gt; 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&lt;int&gt; myset; multiset&lt;int&gt;::iterator it; multiset&lt;int&gt;::iterator temp; multiset&lt;int&gt;::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 &lt; queueSize; i ++){ myfile &gt;&gt; number; myset.insert(number); } it = myset.begin(); int last = minNumber; int current = *it; // ===============================middle stage=============================== for(int i = 0; i &lt; dataSize - queueSize; i ++){ myfile &gt;&gt; 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 &lt; 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 &lt;&lt; "Unable to open file"; print(); cout &lt;&lt; "distanceSum is :" &lt;&lt; distanceSum &lt;&lt; endl; return 0; } void print(){ cout &lt;&lt; "value:" &lt;&lt; endl; for(multiset&lt;int&gt;::iterator it = myset.begin(); it != myset.end(); it ++){ cout &lt;&lt; *it &lt;&lt; "\t"; } cout &lt;&lt; endl; cout &lt;&lt; "current point to:" &lt;&lt; *it &lt;&lt; endl; cout &lt;&lt; "total size is:" &lt;&lt; myset.size() &lt;&lt; endl; cout &lt;&lt; "current distance:" &lt;&lt; distanceSum &lt;&lt; endl; } </code></pre> <p>and the test data:</p> <p><a href="https://drive.google.com/file/d/0ByMlz1Uisc9ONWJIdFFXaGdpSXM/edit?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0ByMlz1Uisc9ONWJIdFFXaGdpSXM/edit?usp=sharing</a></p> <p>you should save it as filename 'sort'</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload