• 2.最佳调度问题


    2.最佳调度问题
    (machine.pas/c/cpp)
    【问题描述】
    假设有n个任务由k个可并行工作的机器完成。完成任务i需要的时间为ti。试设计一个算法找出完成这n个任务的最佳调度,使得完成全部任务的时间最早。
    【编程任务】
    对任意给定的整数n(<=20)和k(<=10),以及完成任务i需要的时间为ti,i=1~n。编程计算完成这n个任务的最佳调度。
    【输入格式】
    由文件machine.in给出输入数据。第一行有2 个正整数n和k。第2 行的n个正整数(<=10000)是完成n个任务需要的时间。
    【输出格式】
    将计算出的完成全部任务的最早时间输出到文件machine.out。
    【输入样例】
    7 3
    2 14 4 16 6 5 3
    【输出样例】
    17

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[25],b[15],ans;
    int n,k;
    bool cmp1(int a,int b)
    {
        return a>b;
    }
    int greedy(){
        sort(a+1,a+1+n,cmp1);
        for(int j=1;j<=n;j++)
        {
            b[1]+=a[j];
            sort(b+1,b+1+k);        
        }    
        return ans=b[k];
    }
    void dfs(int s,int t)
    {
        if(t>=ans)return;
        if(s>n){
             ans=min(ans,t);return;
        }
        for(int i=1;i<=k;i++)
        {        
            b[i]+=a[s];
            dfs(s+1,max(b[i],t));
            b[i]-=a[s];
        }
    }
    int main()
    {
        freopen("machine.in","r",stdin);
        freopen("machine.out","w",stdout);
        
        cin>>n>>k;
        for(int i=1;i<=n;i++)cin>>a[i];
        greedy();
        memset(b,0,sizeof(b));
        dfs(1,0);
        cout<<ans<<endl;
        
        
            
    }
  • 相关阅读:
    Java中IO流的总结
    Java常用集合体系以及相互区别
    TreeMap集合特点、排序原理
    HashMap集合
    TreeSet集合
    redis 数据类型详解 以及 redis适用场景场合
    You need tcl 8.5 or newer in order to run the Redis test
    PHP 获取二维数组中某个key的集合
    Linux 定时任务
    phpmailer邮件类
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/7632985.html
Copyright © 2020-2023  润新知