poj 3045 Cow Acrobats(数学题)

题目链接:http://poj.org/problem?id=3045

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

题意:这道题居然和今年成都赛区的倒数第二题一模一样。。。或者说该反过来说、、给你n头牛叠罗汉,每头都有自己的重量w和力量s,承受的风险数就是该牛上面牛的总重量减去它的力量,题目要求设计一个方案使得所有牛里面风险最大的要最小。

题解:按照w+s贪心叠,越大的越在下面。如果最优放置时,相邻两头牛属性分别为w1,s1,w2,s2,第一头牛在第二头上面,sum为第一头牛上面的牛的体重之和,那么

第一头牛风险:a=sum-s1;第二头牛风险:b=sum+w1-s2;交换两头牛位置之后

a‘=sum+w2-s1,b‘=sum-s2,

由于是最优放置,所以w2-s1>=w1-s2,即w2+s2>=w1+s1,所以和最大的就该老实的在下面呆着= =!

以上转载!

思路:

先按照w+s的和的大小排序,大的在下面,最后在把过程中最大的危险值求出来!

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 50017;
#define INF 0x3f3f3f3f
typedef struct
{
    int w, s;
    int ss;
} COW;
COW cow[MAXN];
bool cmp(COW a, COW b)
{
    return a.ss < b.ss;
}
int main()
{
    int n;
    int sum[MAXN];
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d",&cow[i].w,&cow[i].s);
            cow[i].ss = cow[i].w+cow[i].s;
        }
        sort(cow+1,cow+n+1,cmp);
        int ans = -INF;
        for(int i = 1; i <= n; i++)
        {
            sum[i] = sum[i-1]+cow[i].w;
            ans = max(ans,sum[i-1]-cow[i].s);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-11 06:33:01

poj 3045 Cow Acrobats(数学题)的相关文章

POJ 3045 Cow Acrobats (想法题)

题目链接:POJ 3045 Cow Acrobats 题意:有n只牛叠罗汉,危险指数的计算是 该层牛以上的牛重量总和减去这层牛的强度,求使最大的危险指数中的最小值. 思路:根据w+s排序,最大的在最下面,道理很简单,危险指数: sum-(w+s),(sum该层牛以上的牛重量总和). AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #define ll __int64 using namespa

poj 3045 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 ca

POJ 3045 Cow Acrobats

传送门:http://poj.org/problem?id=3104 题意: 烘干所有的衣服,在自然晾干每分钟可以减少1单位的水分,在烘干机里面每分钟减少k单位的水分, 一件衣服可以烘干一部分水分,也可以自然晒干一部分水分. 解题思路: 在代码中. 实现代码: #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int MAXN=100000; con

POJ 3045 Cow Acrobats (最大化最小值)

题目链接:click here~~ [题目大意] 给你n头牛叠罗汉.每头都有自己的重量w和力量s,承受的风险数rank就是该牛上面全部牛的总重量减去该牛自身的力量,题目要求设计一个方案使得全部牛里面风险最大的要最小. [解题思路]:依照w+s贪心放置,越大的(注意是w+s之和)越在以下.不难证明:假设最优放置时.相邻两头牛属性分别为w1,s1,w2,s2,第一头牛在第二头上面,sum为第一头牛上面的牛的体重之和.那么第一头牛风险:rank1=sum-s1;第二头牛风险:rank2=sum+w1-

POJ - 3045 Cow Acrobats 贪心

题目大意:有N头牛要叠罗汉,每头牛都有相应的重量和力量. 叠罗汉是有危险的,每头牛的危险系数为该牛上面的牛的重量的和减去该牛的力量 问如何安排这个叠罗汉顺序,使得危险系数最大的那头牛的危险系数最小 解题思路:最大值的最小值,用二分?二分当然也可以,但是有更简便的方法 假设第i头牛的重量为wi,力量为si,第j头牛的重量为wj,力量为sj,第i头牛上面的牛的重量和sum 先考虑第一种情况,第i头牛叠到第j头牛的上面 那么 a1 = sum -si, b1 = sum + wi -sj 考虑第二种情

POJ3045 Cow Acrobats —— 思维证明

题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5713   Accepted: 2151 Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. The

[USACO2005][POJ3045]Cow Acrobats(贪心)

题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风险值 分析:这就是noip2012 T2的来源= =只不过这里是加,noip里是乘 不妨设所有牛都按最优顺序排好了,考虑相邻的两头牛i和i+1,如果交换他们的位置,那么对前面和后面的结果都无影响,只是他们两个的风险值变化了(变大了),于是我们可以得到这个时候i和i+1的关系 设w1+w2+...+wi-1

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 3613 Cow Relays 恰好n步的最短路径

http://poj.org/problem?id=3613 题目大意: 有T条路,从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]) i到j的最短路是i到j的直接路径或者经过k点的间接路径,但是矩阵的更新总是受到上一次更新的影响 如果每次的更新都存进新矩阵,那么edge[i][k]+edge[k][j]是不是表示只经过三个点两条边的路径呢? min(edge[i