POJ3045 Cow Acrobats【二分搜索+贪心】

2024-04-08 17:32

本文主要是介绍POJ3045 Cow Acrobats【二分搜索+贪心】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Cow Acrobats
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9208 Accepted: 3408

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.

Source

USACO 2005 November Silver

问题链接:POJ3045 Cow Acrobats
问题简述:有N头牛,给定每头牛的体重和力气。现在N头牛要按顺序排列起来,其中第i头牛的承担风险为前i-1头牛的体重和减去它的力气。要求你任意选择一种序列,使得这N头牛承担的最大风险最小化。
问题分析:最大化最小值问题,可以用二分搜索来做,也可以用贪心来做。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(二分搜索)如下:

/* POJ3045 Cow Acrobats */#include <iostream>
#include <algorithm>
#include <cstdio>using namespace std;const int INF = 0x3f3f3f3f;
const int N = 50000 + 1;
struct Node {int w, s;
} a[N];
bool cmp(Node a, Node b)
{return a.w + a.s < b.w + b.s;
}
int n, sum[N];bool judge(int mid)
{for(int i = 1; i <= n; i++)if(sum[i - 1] - a[i].s > mid) return false;return true;
}int main()
{scanf("%d", &n);for(int i = 1; i <= n; i++) scanf("%d%d", &a[i].w, &a[i].s);sort(a + 1, a + 1 + n, cmp);sum[0] = 0;int left = INF, right = 0, mid;for(int i = 1; i <= n; i++) {left = min(left, sum[i - 1] - a[i].s);right = max(right, sum[i - 1] - a[i].s);sum[i] = sum[i - 1] + a[i].w;}int ans;while(left <= right) {mid = (left + right) / 2;if(judge(mid)) ans = mid, right = mid - 1;else left = mid + 1;}printf("%d\n", ans);return 0;
}

AC的C++语言程序(贪心)如下:

/* POJ3045 Cow Acrobats */#include <iostream>
#include <algorithm>
#include <cstdio>using namespace std;const int N = 50000;
struct Node {int w, s;
} a[N];
bool cmp(Node a, Node b)
{return a.w + a.s < b.w + b.s;
}int main()
{int n;scanf("%d", &n);for(int i =0; i < n; i++) scanf("%d%d", &a[i].w, &a[i].s);sort(a, a + n, cmp);int sum = 0, ans = -a[0].s;for(int i = 0; i < n - 1; i++) {sum += a[i].w;ans = max(ans, sum - a[i + 1].s);}printf("%d\n", ans);return 0;
}

这篇关于POJ3045 Cow Acrobats【二分搜索+贪心】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/886165

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta