贪心:磁带最优存储与磁盘文件最优存储

1、磁带最优存储问题

 

Input:

5
71 872
46 452
9 265
73 120
35 87

Output:

  85.6193

 

Code:

 

#include <bits/stdc++.h>
using namespace std;

void Merge(double* arr, int p, int q, int r){
    int Llen = q - p + 1;
    int Rlen = r - q;
    double Larr[Llen + 1];
    double Rarr[Rlen + 1];

    for(int i=0; i<Llen; i++){
        Larr[i] = arr[p + i];
    }
    Larr[Llen] = INT_MAX;
    for(int i=0; i<Rlen; i++){
        Rarr[i] = arr[q + 1 + i];
    }
    Rarr[Rlen] = INT_MAX;
    int Lindex=0;
    int Rindex=0;
    for(int i=0; i<r-p+1; i++){
        if(Larr[Lindex] < Rarr[Rindex]){
            arr[p + i] = Larr[Lindex];
            Lindex++;
        }
        else{
            arr[p + i] = Rarr[Rindex];
            Rindex++;
        }
    }
}

void mySort(double* arr,int p, int r){
    if(p<r){
        int q = (p+r)/2;
        mySort(arr, p, q);
        mySort(arr, q+1, r);
        Merge(arr, p, q, r);
    }
}

double paixu(int n, int* len, double* rate){
    double Result[n];
//  1、求出 读取概率 ×长度 
    double rateSum = 0;
    for(int i=0; i<n; i++){
        rateSum += rate[i];
    }
    for(int i=0; i<n; i++){
        rate[i] = rate[i]/rateSum;
    }
    for(int i=0; i<n; i++){
        Result[i] = len[i] * rate[i];
    }
//  2、对result进行排序,算每个程序的访问时间。
    mySort(&Result[0], 0, n-1);
//  3、求出n个程序的平均读取时间 
    double result = 0;
    for(int i=0; i<n; i++){
        result += Result[i]*(n-i);
    }
    return result;
}

int main(){
    int n,len[999];
    double rate[999];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>len[i]>>rate[i];
    double result = paixu(n, &len[0], &rate[0]);
    cout<<result<<endl;
    return 0;
}

 

 

2、磁盘文件最有存储问题

 

 

Input:

 

5

33 55 22 11 9

 

Output:

 

0.547396

 

 

 

Code:

 

#include <bits/stdc++.h>
using namespace std;

void Merge(double* arr, int p, int q, int r){
    int Llen = q - p + 1;
    int Rlen = r - q;
    double Larr[Llen + 1];
    double Rarr[Rlen + 1];
    for(int i=0; i<Llen; i++){
        Larr[i] = arr[p + i];
    }
    Larr[Llen] = INT_MIN;
    for(int i=0; i<Rlen; i++){
        Rarr[i] = arr[q + 1 + i];
    }
    Rarr[Rlen] = INT_MIN;
    int Lindex=0;
    int Rindex=0;
    for(int i=0; i<r-p+1; i++){
        if(Larr[Lindex] > Rarr[Rindex]){
            arr[p + i] = Larr[Lindex];
            Lindex++;
        }else{
            arr[p + i] = Rarr[Rindex];
            Rindex++;
        }
    }
}

void mySort(double* arr,int p, int r){
    if(p<r){
        int q = (p+r)/2;
        mySort(arr, p, q);
        mySort(arr, q+1, r);
        Merge(arr, p, q, r);
    }
}

double paixu(double* rate, int n){
    mySort(rate, 0, n-1);
    double rateSum = 0;
    for(int i=0; i<n; i++){
        rateSum += rate[i];
    }
    for(int i=0; i<n; i++){
        rate[i] = rate[i]/rateSum;
    }
    double tmp[n];
    if(n%2 ==1){
        int k = (n-1)/2;
        tmp[k] = rate[0];
        for(int i=k+1; i<n; i++){
            tmp[i] = rate[2*(i-k)];
        }
        for(int i=0; i<k; i++){
            tmp[i] = rate[2*(k-i) - 1];
        }
    }else if(n%2==0){
        int k = (n-1)/2;
        for(int i=k+1; i<n; i++){
            tmp[i] = rate[2*(i-k) - 1];
        }
        for(int i=0; i<=k; i++){
            tmp[i] = rate[2*(k-i)];
        }
    }
    double result = 0;
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            result += tmp[i]*tmp[j]*(j - i);
        }
    }
    return result;
}

int main(){
    int n;
    double rate[9999];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>rate[i];
//    double rate[5] = {33, 55, 22, 11, 9};
    double num = paixu(&rate[0], n);
    cout<<num<<endl;
}

 

 

 

参考自:https://www.jianshu.com/p/d3618f030b87

posted @ 2019-06-14 10:12  cruelty_angel  阅读(2591)  评论(1编辑  收藏  举报