[luogu]P1248 加工生产调度

题目传送门

分析

考虑一种贪心排序,还是用 临项交换 的方法
假设产品 ij 相邻,ai,bi,aj,bj表示所需时间
不交换时,耗时

w1=ai+max(bi,aj)+bj

交换时,耗时

w2=aj+max(bj,ai)+bi

若满足不交换更优,则w1<w2,即

max(aj,bi)<max(ai,bj)min(ai,bj)<min(aj,bi)

按照此排序,同时考虑到上述柿子左边和右边相等时,特殊判断
可以参考上一篇皇后游戏
相等时优先把b更短的放在后面,加快结束

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define rg register
inline int read(){
    rg int x = 0, f = 1;
    rg char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return x * f;
}
const int N = 1e3 + 1;
int n;
struct production{
    int a, b, d, id;
    bool operator <(productioin x) const{
        if (min(a, x.b) == min(b, x.a))
            return b > x.b;
        return min(a, x.b) < min(b, x.a);
    }
    /*bool operator < (production e) const{
        if (d != e.d) return d < e.d;
        else if (d > 0) return b > e.b;
        else return a < e.a;
    }*/
}t[N];

inline void init(){
    n = read();
    for (int i(1); i <= n; ++i) t[i].a = read();
    for (int i(1); i <= n; ++i) t[i].b = read(), t[i].id = i;
}

inline void doit(){
    /*for (int i(1); i <= n; ++i)
        if (t[i].a == t[i].b) t[i].d = 0;
        else t[i].d = t[i].a > t[i].b ? 1 : -1;*/
    sort(t + 1, t + 1 + n);
    int t1 = 0, t2 = 0;
    for (int i(1); i <= n; ++i){
        t1 += t[i].a;
        t2 = max(t2, t1) + t[i].b;
    }
    printf("%d\n", t2);
    for (int i(1); i <= n; ++i)
        printf("%d ", t[i].id);
}
int main(){
    ios::sync_with_stdio(false);
    cout.tie(NULL);
    init();
    doit();
    return 0;
}
posted @   ancer  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示