51nod1205(johnson)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1205

题意:中文题诶~

思路:johnson模板题

流水作业调度问题的Johnson算法:

    (1)令N1={i|ai<bi}, N2={i|ai>=bi};

    (2)将N1中作业按ai的非减序排序;将N2中作业按bi的非增序排序;

    (3)N1中作业接N2中作业构成满足Johnson法则的最优调度。

关于johnson算法详细讲解:http://blog.csdn.net/liufeng_king/article/details/8678316

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int MAXN = 5e4+10;
 7 struct node{
 8     int x, y, cnt;
 9 }gg[MAXN];
10 
11 bool cmp(node a, node b){
12     return a.cnt < b.cnt;
13 }
14 
15 bool cmp1(node a, node b){
16     return a.x < b.x;
17 }
18 
19 bool cmp2(node a, node b){
20     return a.y > b.y;
21 }
22 
23 int main(void){
24     int n;
25     scanf("%d", &n);
26     for(int i=0; i<n; i++){
27         scanf("%d%d", &gg[i].x, &gg[i].y);
28         gg[i].cnt = gg[i].x-gg[i].y;
29     }
30     sort(gg, gg+n, cmp);
31     int index = 0;
32     while(gg[index].cnt < 0){
33         index++;
34     }
35     sort(gg, gg+index, cmp1);
36     sort(gg+index, gg+n, cmp2);
37     int ans = gg[0].x + gg[0].y;
38     int sum = gg[0].x;
39     for(int i=1; i<n; i++){
40         sum += gg[i].x;
41         ans = max(sum, ans) + gg[i].y;
42     }
43     printf("%d
", ans);
44     return 0;
45 }
View Code
原文地址:https://www.cnblogs.com/geloutingyu/p/6927818.html