ZBLOG

Tape Scheduling Limits: 5 sec., 1024 MiB Tape storage or magnetic tape data storage is a system that is used for storing digital information on magnetic tape using digital recording. With data volumes growing rapidly worldwide, tape storage is the...

以下是给出的C++代码,实现了针对磁带调度问题的请求顺序优化:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Request {
    int wrap;
    double distance;
};

double getTimeToMove(int w1, double d1, int w2, double d2) {
    double time = 0.0;
    
    // Step 1: Change band if necessary
    if (w1 / 52 != w2 / 52) {
        time += 4.0;
    }
    
    // Step 2: Change wrap if necessary
    if (w1 != w2) {
        time += 1.0;
        
        // Step 3: Move to the closest anchor position
        int anchorW2 = (w2 / 26) * 26 + ((w2 % 26) % 2 == 0 ? (d2 >= 480.0 ? 25 : 0) : (d2 >= 480.0 ? -1 : -26));
        
        // Calculate time to move to the anchor position
        double distanceToAnchor = fabs(d1 - anchorW2 * 30);
        time += distanceToAnchor / 8.0;
        
        // Check if direction change is needed
        bool directionChange = false;
        if ((d1 > d2 && anchorW2 < w2) || (d1 < d2 && anchorW2 > w2)) {
            directionChange = true;
            time += 2.5; // Direction change penalty
        }
        
        // Step4: Slowly move to the position
        double slowDistance = fabs(anchorW2 *30 - d2);
        time += slowDistance / (directionChange ? 4.0 : 2.0);
    }
    
    return time;
}

bool compareRequests(const Request& r1, const Request& r2) {
    double time1 = getTimeToMove(0, 0.0, r1.wrap, r1.distance);
    double time2 = getTimeToMove(0, 0.0, r2.wrap, r2.distance);
    
    return time1 < time2;
}

int main() {
    int n;
    cin >> n;
    
    vector<Request> requests(n);
    for (int i = 0; i < n; i++) {
        cin >> requests[i].wrap >> requests[i].distance;
    }
    
    // Sort the requests based on the optimized order
    sort(requests.begin(), requests.end(), compareRequests);
    
    // Print the order of execution
    for (int i = 0; i < n; i++) {
        cout << i << " ";
    }
    
    cout << endl;
    
    return 0;
}

希望以上代码能够满足你的需求,如有其他问题,请随时提问。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=5060

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?