POJ3045+3258+2231

来源:互联网 发布:秦时明月动漫知乎 编辑:程序博客网 时间:2024/06/05 04:50

有点堕落 

好几天没写了  

先补上练习三的

A - Cow Acrobats

 POJ - 3045 

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
310 32 53 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头牛,还有牛的体重和力量   玩叠罗汉

风险就是当前牛头顶上牛的总体重减去他的力量

所有方案中,输出它风险最大的最小值

思路:猛一看以为是二分  其实不然  所有的牛都有份,体重重,力量大的在下面是必然的

根据题意,当前牛的风险值就等于头顶牛的总重量-改牛的力量,而且重的肯定在下面

所以就有了,求出全部重量,风险就成了  总重量-当前重量-当前力量   根据重量和力量的和排序即可

  • Source Code
    #include<iostream>#include<stdio.h>#include<algorithm>using namespace std;struct cow{    int w;    int s;    }a[50001];bool cmp(cow A,cow B){    return A.w+A.s>=B.w+B.s;    }int main(){    int i,j;    int n;    while(cin>>n)    {        long long sum=0;        for(i=0;i<n;i++)        {            scanf("%d %d",&a[i].w,&a[i].s);            sum+=a[i].w;            }        sort(a,a+n,cmp);        //for(i=0;i<n;i++)        //cout<<a[i].w<<" "<<a[i].s<<endl;        long long ans=-0x3f3f3f3f;        for(i=0;i<n;i++)        {            sum-=a[i].w;            ans=max(ans,sum-a[i].s);            }        cout<<ans<<endl;        }    return 0;    }

B - River Hopscotch

 POJ - 3258 

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, Lunits away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to rocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input
Line 1: Three space-separated integers: LN, and M 
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2214112117
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).


题意:一条小河长度l,和中n快石头,可以挪动m快石头,每块石头距离岸边的距离知道(距离左岸 即0位置),问你
移除m快石头之后,相邻两块石头之间的距离的最小值最大是多少
思路:二分时候的例题
每次都枚举一个值,看看这个mid能够去掉多少石头,即相邻两块石头之间的值是否大于这个数mid,
如果>m块则说明当前mid的值大了,反之则小
  • Source Code
    #include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;int main(){    long long a[50001];    int i,j;    long long l;    int n,m;    while(cin>>l>>n>>m)    {        //memset(a,0x3f3f3f,sizeof(a));        for(i=1;i<=n;i++)        scanf("%lld",&a[i]);        a[0]=0;        a[n+1]=l;        sort(a,a+n+2);        //for(i=0;i<=n+1;i++)        //cout<<a[i]<<endl;        long long low=0,mid,high=l;        while(low<high)        {            mid=(low+high)/2;            int num=0;            j=0;            for(i=1;i<n+2;i++)            {                if(a[i]-a[j]<=mid)                num++;                else                j=i;                }            if(num>m)            high=mid;            else low=mid+1;            }        cout<<low<<endl;        }    return 0;    }

F - Moo Volume

 POJ - 2231 
Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise. 

FJ's N cows (1 <= N <= 10,000) all graze at various locations on a long one-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.
Input
* Line 1: N 

* Lines 2..N+1: The location of each cow (in the range 0..1,000,000,000).
Output
There are five cows at locations 1, 5, 3, 2, and 4.
Sample Input
515324
Sample Output
40
Hint
INPUT DETAILS: 

There are five cows at locations 1, 5, 3, 2, and 4. 

OUTPUT DETAILS: 

Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at 4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.
题意:给出n个数,求每个数到其他数之间的距离
思路:一看懂题目的时候,啥也没想,直接暴力写了个,tle
在网上看了一种思路,很机智,因为所有的牛都要求出他们之间的距离
这样 暴力写的话 无用功很多,或者说重复很多,
这种方法就是把他们从小到大排序,然后机智的地方来了,
滴i头牛(不是第一头)距离前一头牛的距离都要算i遍 举个例子
1 2 3 4 5
2+1=1+1*2
第一头牛到第三头牛的距离,你是直接减法运算,而是经过第二头中转下,这样第三头牛到第二头牛
的距离就用了两次 具体见代码
  • Source Code
    #include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;long long ans[100001],a[10001];int main(){    int i,j;    int n;    long long sum;    while(scanf("%d",&n)!=EOF)    {        memset(a,0,sizeof(a));        sum=0;        for(i=0;i<n;i++)        {            scanf("%lld",&a[i]);            }        sort(a,a+n);        for(i=0;i<n;i++)        {            ans[i]=ans[i-1]+(a[i]-a[i-1])*i;            sum+=ans[i];            }        cout<<2*sum<<endl;        }    return 0;    }