poj 3045 Cow Acrobats 【二分】
2015-12-20 12:04
218 查看
Cow Acrobats
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
Sample Output
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头牛,已经给出每头牛的体重和力气。现在N头牛要按顺序排列起来,其中第i头牛的承担风险为前i-1头牛的体重和减去它的力气。要求你任意选择一种序列,使得这N头牛承担的最大风险最小化。
思路:二分。对任意的两头牛i和i+1考虑二者的状态,设前i-1头牛的总体重为sum。那么排序方式为
sum + num[i].w - num[i+1].s < sum + num[i+1].w - num[i].s;
化简后为 num[i].w - num[i+1].s < num[i+1].w - num[i].s。
sort下,二分答案就可以了。
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3678 | Accepted: 1423 |
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头牛,已经给出每头牛的体重和力气。现在N头牛要按顺序排列起来,其中第i头牛的承担风险为前i-1头牛的体重和减去它的力气。要求你任意选择一种序列,使得这N头牛承担的最大风险最小化。
思路:二分。对任意的两头牛i和i+1考虑二者的状态,设前i-1头牛的总体重为sum。那么排序方式为
sum + num[i].w - num[i+1].s < sum + num[i+1].w - num[i].s;
化简后为 num[i].w - num[i+1].s < num[i+1].w - num[i].s。
sort下,二分答案就可以了。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define INF 0x3f3f3f #define eps 1e-8 #define MAXN (50000+1) #define MAXM (100000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; struct Node{ int w, s; }; Node num[MAXN]; bool cmp(Node a, Node b){ return a.w - b.s < b.w - a.s; } int sum[MAXN]; bool judge(int o, int n) { for(int i = 1; i <= n; i++) if(sum[i-1] - num[i].s > o) return false; return true; } int main() { int n; while(Ri(n) != EOF) { for(int i = 1; i <= n; i++) Ri(num[i].w), Ri(num[i].s); sort(num+1, num+n+1, cmp); sum[0] = 0; int l = INF, r = 0; for(int i = 1; i <= n; i++) { l = min(sum[i-1] - num[i].s, l); r = max(sum[i-1] - num[i].s, r); sum[i] = sum[i-1] + num[i].w; } int ans; while(l <= r) { int mid = (l + r) >> 1; if(judge(mid, n)) { ans = mid; r = mid-1; } else l = mid+1; } Pi(ans); } return 0; }
相关文章推荐
- Linux系统中查杀僵尸进程的基本方法讲解
- UVa129 相邻重复子串
- Yii2 Activefrom表单部分组件
- Range Sum Query - Immutable
- 【杭电】[2005]第几天?
- chapter22:源码与Tarball之(2)-Tarball的管理与安装
- cron的用法
- I.MX6 uSDHC SD card register
- poj 3258 River Hopscotch 【二分】
- poj 2229 Sumsets 【完全背包 or 递推】
- 这里有个坑---entity为null的问题
- swift之auto layout
- Hadoop HDFS分布式环境搭建
- poj 2385 Apple Catching 【暴力dp】
- mybatis直接执行sql语句
- 117,字符串的转换
- poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)
- C++句柄解析
- Android获取服务器Json数据与Json的解析
- KING_UNITY学习之Unity2D代码动态为Sprite Renderer的Sprite赋值sprite精灵图片