Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:Johnson算法模板题

Posted by buaabarty at 2012-07-26 12:05:36 on Problem 2751
In Reply To:Johnson算法模板题 Posted by:buaabarty at 2012-07-26 11:53:42
> 一水
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct data {
    int x, y;
    bool operator < (const data a) const {
        if (x < y) return ((a.x >= a.y) || (x < a.x));
        else return ((a.x >= a.y) && (y > a.y));
    }
}a[10010];
int main() {
    int n;
    while (scanf("%d", &n), n) {
        for (int i = 1; i <= n; ++i) {
            int x, y; scanf("%d%d", &a[i].x, &a[i].y);
        }
        sort(a+1, a+n+1);
        int t1 = 0, t2 = 0;
        for (int i = 1; i <= n; ++i) {
            t1 += a[i].x;
            t2 = max(t1, t2) + a[i].y;
        }
        printf("%d\n", t2);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator