Cow Acrobats - POJ 3045 排序

本文探讨了一种解决牛群困境的排序算法优化方法,通过合理安排牛群的体重和力量来最小化最大的风险度,实现高效有序的排列。

Cow Acrobats
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2508 Accepted: 985

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

题意:让你给n个牛排序,使这些牛最大的危险度最小。

思路:让体重大的,承受大的往下排,你要是问为什么这样,看看我排序的那句大概就明白了。

AC代码如下:

#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{ int w;
  int s;
}cow[50010];
bool cmp(node a,node b)
{ if(a.w-b.s<b.w-a.s)
   return true;
  else if(a.w-b.s==b.w-a.s)
   return a.w<b.w;
  else return false;

}
int main()
{ int n,i,j,k,ans=0,sum=0,ret;
  scanf("%d",&n);
  for(i=1;i<=n;i++)
   scanf("%d%d",&cow[i].w,&cow[i].s);
  sort(cow+1,cow+1+n,cmp);
 // for(i=1;i<=n;i++)
  // printf("%d %d\n",cow[i].w,cow[i].s);
  ans=0-cow[1].s;
  sum+=cow[1].w;
  for(i=2;i<=n;i++)
  { ret=sum-cow[i].s;
    if(ret>ans)
     ans=ret;
     //printf("%d %d\n",sum,ret);
    sum+=cow[i].w;

  }
  //if(ans<0)
  // ans=0;
  printf("%d\n",ans);
}




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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值