in this round the question was
java solution will be preferred.
programmar will get benefits.
Finding data on a huge hard disk has been a difficult task for computer manufacturers. In the constant quest to speed up the seek-time to find data, they've come up with a new algorithm. A simplistic view of a hard disk is as follows:
A hard disk consists of several platters, each of which has a head for reading and writing data. Each platter is divided into 80 tracks, numbered 1 to 80. Initially all the heads are on track 1. Once a head reaches a track, it can read all the data requests that have come in so far for that track, in one read-operation, which takes one unit of time. A head takes one unit time to move from one track to the neighbouring track. To minimize the seek-time, after every read operation, the head will move in the direction of the nearest pending request. That is, if the head was moving toward track 80 and while it was on track 5, two requests came, one each for track 2 and track 18. The head would continue to move in the same direction, read the data on track 18, re-evaluate the nearest track to be 2 and start moving toward track 2. If two competing requests for different tracks are equidistant from the head, then it will move in the direction of track 1. Initially the head of all platters is at rest. The head does not move if there are no requests for any track at the particular time.
The head on each platter follows this sequence during each time unit: -
1.All the requests for the current time unit are noted.
2.If there are any requests for the current track, read occurs in the same time unit.
3.If there were no reads, the head moves, if necessary.
4.Decides the direction of movement, if necessary.
You have to write a program to calculate the number of fulfilled requests per platter, after the specified time. If a particular platter has had no read-requests throughout the simulation, its count of fulfilled requests is '0'. The simulation starts at the beginning of time unit 1 and finishes at the end of time unit T
Input specification:
The first line of input will be an integer P (0
<= 20), signifying the number of platters.
The second line of input will be an integer T, specifying the time unit after which the output is to be printed.
The third line of input will be an integer N (0<=N<= 100), specifying the number of read requests that are coming in.
The next N lines will contain the read requests in the following format:
<time-of-request><space><platter-number><space><track-number>
Platter numbers start from 1 to P
All of the above will be integers separated by a single space.
Output specification:
Your program has to print N lines of output in ascending order of platter number in the following format:
<platter-number><hyphen><requests-fulfilled>
Sample Input and Output:
Input:
2
5
4
1 1 1
1 1 2
3 2 3
2 1 3
Output:
1-3
2-0
Input:
3
53
7
2 3 50
51 1 2
1 1 3
51 1 2
1 2 80
2 2 5
2 2 1
Output:
1-3
2-2
3-1
import java.util.ArrayList;
- Achilles May 25, 2012import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Platter{
static int globalTime = 1;
static boolean moveForward = true;
static int head = 1;
public static void main(String[] args) {
int numberOfPlatters = 3;
int finalTime = 53;
List<Request> inputList = new ArrayList<Request>();
inputList.add(new Request(2,3,50));
inputList.add(new Request(51,1,2));
inputList.add(new Request(1,1,3));
inputList.add(new Request(51,1,2));
inputList.add(new Request(1,2,80));
inputList.add(new Request(2,2,5));
inputList.add(new Request(2,2,1));
Map<Integer, Integer> resultSet = new HashMap<Integer, Integer>();
List<Request> requestList = new ArrayList<Request>();
Collections.sort(inputList, new Comparator<Request>() {
@Override
public int compare(Request o1, Request o2) {
return o1.timeOfArrival - o2.timeOfArrival;
}
});
for (int j=1; j <= finalTime; j++) {
if (!inputList.isEmpty() && finalTime >= inputList.get(0).timeOfArrival ){
Request first = inputList.remove(0);
Platter.globalTime = first.timeOfArrival;
boolean requestProcessed = false;
requestList.add(first);
while (!requestProcessed && !inputList.isEmpty()) {
Request req = inputList.get(0);
if (req.timeOfArrival <= globalTime) {
requestList.add(inputList.remove(0));
} else {
requestProcessed = true;
}
}
}
for(Iterator <Request> iter =requestList.iterator();iter.hasNext();) {
Request request = iter.next();
if (request.trackNumber == head) {
Integer plat = resultSet.get(request.platterNumber);
if (plat==null) {
plat = 1;
} else {
plat++;
}
resultSet.put(request.platterNumber, plat);
iter.remove();
}
}
int trackNumber = 0;
for (Request request : requestList) {
int minRequest = 9999;
if (Math.abs(Platter.head- request.trackNumber) < minRequest) {
minRequest = Math.abs(Platter.head-request.trackNumber);
trackNumber = request.trackNumber;
}
if (Math.abs(Platter.head-request.trackNumber) == minRequest) {
if ((moveForward && Platter.head < request.trackNumber) || (!moveForward && Platter.head > request.trackNumber)) {
minRequest = Math.abs(Platter.globalTime-request.trackNumber);
trackNumber = request.trackNumber;
}
}
}
if (trackNumber == 0) {
continue;
}
if (trackNumber > Platter.head) {
Platter.head++;
moveForward = true;
} else if (trackNumber < Platter.head) {
Platter.head--;
moveForward = false;
}
}
printVals(resultSet);
}
private static void printVals(Map<Integer, Integer> resultSet) {
for (Map.Entry<Integer, Integer> iter : resultSet.entrySet()) {
System.out.println(iter.getKey() + " " + iter.getValue());
}
}
private static class Request {
int timeOfArrival;
int platterNumber;
int trackNumber;
public Request(int timeOfArrival, int platterNumber, int trackNumber) {
this.timeOfArrival = timeOfArrival;
this.platterNumber = platterNumber;
this.trackNumber = trackNumber;
}
}
}
Modifications for input reading and check on the track number can be added.