Cow Acrobats(贪心)

有N头牛,每头牛都有重量W和力量S。为了参加马戏团表演,需要将牛叠放起来,每头牛的风险值为其上方所有牛的W之和减去自己的S。目标是找到一种叠放顺序,使得所有牛的最大风险值最小。
Cow Acrobats
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 2336  Accepted: 929

Description

Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts. 

The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack. 

Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.

Input

* Line 1: A single line with the integer N. 

* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i. 

Output

* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.

Sample Input

3
10 3
2 5
3 3

Sample Output

2

Hint

OUTPUT DETAILS: 

Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.

 

       题意:

       给出 N (1 ~ 50000),代表有 N 头牛,每头牛都有一个 W (1 ~ 1000)和 S (1 ~ 1000000000)。现有个马戏团表演,要求把牛搭牛,所以对应每头牛都会有一个风险值。这个风险值等于这头牛以上所有牛的 W 之和 - 这头牛的 S。现要求你设计叠放的顺序,使最大值最小化,输出这个最小化最大值。

 

       思路:

       贪心。先对总和(S + W)由小到大排序,后算出最大值即为最小化的最大值。风险值 = sum(W) - (Wi + Si ),故要求最大值最小,那么要求 ( Wi + Si ) 最大。

 

       AC:

#include <cstdio>
#include <algorithm>

using namespace std;

const int INF = 1000000005;

typedef struct { int w, s, n;} node;
node no[50005];

int cmp(node a, node b) { return a.n < b.n; }

int main () {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
                scanf("%d%d", &no[i].w, &no[i].s);
                no[i].n = no[i].w + no[i].s;
        }

        sort(no, no + n, cmp);

        int sum = 0, max_num = -INF;
        for (int i = 0; i < n; ++i) {
                sum += no[i].w;
                if (max_num < sum - no[i].n)
                    max_num = sum - no[i].n;
        }

        printf("%d\n", max_num);
        return 0;
}

 

 

c++ 对于两个序列 $a_1, \ldots, a_n$ 与 $b_1, \ldots, b_m$,定义 $a + b$ 表示将 $a, b$ **向右按位对齐** 后对位相加,得到的长度为 $\max(n, m)$ 的新序列。形式化地,若 $n \geq m$,则有 $$ a + b = [a_1, a_2, \ldots, a_{n-m}, a_{n-m+1} + b_1, a_{n-m+2} + b_2, \ldots, a_n + b_m]; $$ 若 $n < m$,则由对称性 $a + b$ 等于 $b + a$,可以给出类似的形式化定义。 BY 认为一个序列是 *别样的*,当且仅当这个序列可以被表示为至少 $1$ 个 **单调不增的** 正整数序列依次进行加法运算的结果。 他有多组询问。每组询问给出非负整数序列 $x_1, \ldots, x_n$,你需要判断序列 $x$ 是否为 *别样的*。 ## 输入格式 **本题有多组测试数据。** 输入的第一行包含一个正整数 $T$,表示测试数据组数。接下来依次输入每组测试数据。对于每组测试数据: - 第一行包含一个正整数 $n$,表示序列 $x$ 的长度。 - 第二行包含 $n$ 个非负整数 $x_1, \ldots, x_n$,表示序列 $x$。 ## 输出格式 对于每组测试数据,输出一行一个字符串: - 若序列 $x$ 是 *别样的*,则输出 `Yes`; - 否则,输出 `No`。 ## 输入输出样例 #1 ### 输入 #1 ``` 3 6 6 4 3 5 4 4 3 3 2 1 4 1 3 4 2 ``` ### 输出 #1 ``` Yes Yes No ``` ## 说明/提示 #### 样例解释 对于第一组测试数据,可以选择 $a_1 = [6, 4, 3, 2, 2, 1], a_2 = [3, 3, 3]$,容易验证 $a_1 + a_2 = [6, 4, 3, 2+3, 2+3, 1+3] = [6, 4, 3, 5, 4, 4] = x$,因此序列 $x$ 是 *别样的*。 对于第二组测试数据,序列 $x$ 本身是单调不增序列,因此其是 *别样的*。 对于第三组测试数据,可以证明不存在任何选取单调不增序列的方案,满足其和为 $[1, 3, 4, 2]$。 #### 子任务与数据范围 **本题采用子任务捆绑测试,你需要通过一整个子任务的所有测试点才能获得对应的分数**。 对于所有测试数据,保证: - $1 \le T \le 10^6$; - $1 \le n \le 10^6$,$1 \le \sum n \le 10^6$; - $0 \le x_i \le 10^9$。 | 子任务编号 | $\sum n \leq$ | $x_i \leq$ | 特殊性质 | 分数 | | :--------: | :-----------: | :--------: | :----------: | :--: | | $1$ | $8$ | $8$ | 无 | $27$ | | $2$ | $10^6$ | $10^9$ | $x_n = 1$ | $24$ | | $3$ | ^ | ^ | $x$ 单调递增 | $13$ | | $4$ | ^ | ^ | 无 | $36$ |
最新发布
11-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值