Cpp環境【POJ3045】【Vijos2982】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.


  FJ養了N頭牛,他們按照1到N依次編上了號。FJ所不知道的是,他的所有牛的夢想是從農場逃走,去參加馬戲團的演出。可奶牛門很快發現他們那麼笨拙的蹄子根本無法在鋼絲或晃動的鞦韆上站穩(他們還嘗試過把自己裝在大炮裏發射出去,但可想而知,結果是悲慘的)。最終,他們決定練習一種最簡單的雜技:把所有牛都摞在一起,比如說,第一頭牛站在第二頭牛的身上,同時第二頭牛有站在第三頭牛的身上……,最底下的是第N頭牛(牛果然沒什麼創造力)。
  每頭牛都有自己的體重以及力量,編號爲i的牛的體重爲Wi,力量爲Si。當某頭牛身上站着另一些牛時它會在一定程度上被壓扁,我們不妨把它被壓扁的程度叫着壓扁指數。對於任意的牛,它的壓扁指數等於摞在它上面所有牛的總重量(當然不包括自己)減去它的力量。奶牛們按照一定的順序摞在一起後,他們的總壓扁指數就是被壓得最扁的那頭牛的壓扁指數。你的任務就是幫助奶牛們找出一個摞在一起的順序,使得總壓扁指數最小。

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.

第 1 行:一個單獨的正整數N。
第 2到第 N+1 行:第 i+1 行給出編號爲i的奶牛的體重於力量 Wi 和 Si ,用一個空格分開。

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.


【數據範圍】

1<=N<=50 000  1<=Wi<=10 000 1<=Si<=1 000 000 000

【思路梳理】

  貪心地確定牛的疊放順序,並且相當方便。顯然只考慮力量或者是體重來確定順序是不可以的,因此兩者需要同時進行考慮。顯然,牛的力量越大、體重越大就應該放在越下面,否則就會浪費他的力氣。
  先考慮最下面的一頭牛a[1]:顯然他的壓扁指數爲:risk=(tot_weight-a[1].weight)(總承重)-a[i].strength(他的力氣)。顯然tot_weight會從下往上遞減,那麼對於a[i].weight+a[i].strength越大的牛,他就應該放在越下面。
  那麼由此可以推出所有的牛的比較函數:

bool cmp(data a,dat b)
{
   return a.weight+a.strength

#include<cstdio>
#include<iostream>
#include<algorithm>
#define maxn 50005
#define inf 1000000005
using namespace std;
struct data
{
    int weight,strength,id;
}a[maxn];
int n;
long long d[maxn];

bool cmp(data a,data b)
{
    return a.strength-b.weight<b.strength-a.weight;
    //移了一個項,實質上與上面比較函數相同
}

void solve()
{
    long long pressure=0,ans=-inf;//pressure統計第i頭牛所受到的壓力,顯然壓扁指數最大的牛不一定在最下面,所以應該從所有牛的壓扁指數中選擇一個最大的
    for(int i=1;i<=n;i++)//順序是從上往下考慮
    {
        d[i]=pressure-a[i].strength;
        pressure+=a[i].weight;
        ans=max(ans,d[i]);
    }
    cout<<ans;
}

int main()
{
//  freopen("cow.in","r",stdin);
//  freopen("cow.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)   scanf("%d%d",&a[i].weight,&a[i].strength),a[i].id=i;
    sort(a+1,a+1+n,cmp);
    solve();
    return 0;
}
發佈了59 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章