ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1425:【例题4】加工生产调度

2019-06-08 14:01:57  阅读:387  来源: 互联网

标签:例题 机器 10001 加工 int 调度 最短 1425 include


 

1425:【例题4】加工生产调度

题解

求一个加工顺序使得加工总用时最短,就是让机器的空闲时间最短。

一且A机器开始加工,则A机器将会不停地进行作业,

关键是B机器在加工过程中有可能要等待A机器。很明显第一个部件在A机器上加工时,B机器必须等待,最后一个部件在B机器上加工时,A机器也在等待B机器的完工

可以大胆猜想,要使机器总的空闲时间最短,就要把在A机器上加工时间最短的部件最先加工,这样使得B机器能在最短的空闲时间内开始加工;把在B机器上加工时间最短的部件放在最后加工,这样使得A机器用最短时间等待B机器完工

于是我们可以设计出这样的贪心策略:

将M按照从小到大的顺序排序,然后从第1个开始处理,若Mi=ai,则将它排在从头开始的作业后面,若Mi=bi,则将它排在从尾开始的作业前面。

 

 

代码

1.(选择排序版本,蓝书上的)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>

using namespace std;

int n, ans[10001],a[10001],b[10001],m[10001],s[10001];

void read() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
}

void solve() {
    for (int i = 1; i <= n; i++) {
        m[i] = min(a[i], b[i]);
        s[i] = i;
    }

    for(int i=1;i<=n-1;i++)
      for(int j=i+1;j<=n;j++)
      {
        if(m[i]>m[j])
        {
            swap(m[i],m[j]);
            swap(s[i],s[j]);
        }
      }

    int k = 0, t = n + 1;
    for (int i = 1; i <= n; i++) {
        if (m[i] == a[s[i]]) {
            ++k;
            ans[k] = s[i];
        } else {
            --t;
            ans[t] = s[i];
        }
    }

    int t1 = 0, t2 = 0;
    for (int i = 1; i <= n; i++) {
        t1 += a[ans[i]];
        if (t2 < t1)
            t2 = t1;
        t2 += b[ans[i]];
    }
    printf("%d\n", t2);

    for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}

int main() {
    read();
    solve();
    return 0;
}

 

2.(结构体+sort版本)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>

using namespace std;

int n, ans[10001];

struct node {
    int a, b, m, num;
} train[10001];

bool cmp(node x, node y) { return x.m < y.m; }

void read() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &train[i].a);
    for (int i = 1; i <= n; i++) scanf("%d", &train[i].b);
}

void solve() {
    for (int i = 1; i <= n; i++) {
        train[i].m = min(train[i].a, train[i].b);
        train[i].num = i;
    }

    sort(train + 1, train + n + 1, cmp);

    int k = 0, t = n + 1;
    for (int i = 1; i <= n; i++) {
        if (train[i].m == train[i].a) {
            ++k;
            ans[k] = i;
        } else {
            --t;
            ans[t] = i;
        }
    }

    int t1 = 0, t2 = 0;
for (int i = 1; i <= n; i++) { 
 t1 += train[ans[i]].a;
if (t2 < t1)
  t2 = t1;
 t2 += train[ans[i]].b;
 }
 printf("%d\n", t2);

for (int i = 1; i <= n; i++) printf("%d ", train[ans[i]].num);
}

int main() {
    read();
    solve();
    return 0;
}

 

 请问一本通内部的两个oj是不是质壁分离了??

https://loj.ac(https://loj.ac/submission/478220)

 

http://ybt.ssoier.cn:8088/statusx.php?runidx=3734616

 

QAQ请告诉我错在哪里谢谢

标签:例题,机器,10001,加工,int,调度,最短,1425,include
来源: https://www.cnblogs.com/xiaoyezi-wink/p/10986817.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有