Algoogle

Algorithm for Programming Contest

PKU 3045 Cow Acrobats

Category: PKU Tag: greedy

Cow Acrobats

問題概要


解法


下から順に重くて力持ちな牛を置けば最適

コード


(3045.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i,a) for(int i = 0;i < (a); i++)
#define repd(i,a,b) for(int i = (a); i >= (b); i--)
#define all(u) (u).begin(),(u).end()
#define pb push_back
#define INF 1e9
using namespace std;

int N;
struct S{
    int w, s;
    S(int w, int s):w(w),s(s){}
    bool operator < (const S &t) const{ return w + s < t.w + t.s;}
};

int main(){
    cin >> N;
    vector<S> cows;
    rep(i,N){
        int w, s; scanf("%d %d", &w, &s);
        cows.pb(S(w,s));
    }
    sort(rall(cows));
    int sum = 0, ans = -INF;
    repd(i,N-1,0){
        ans = max(ans, sum - cows[i].s);
        sum += cows[i].w;
    }
    cout << ans << endl;
    return 0;
}

Comments