20190630A (greedy)

Title Description

John left his cows N taken up the mountain wood. When he left, as usual, they graze in the pasture.
But when he came back, he saw a scene of tragedy: cow who are hiding his garden, chewing on his beloved beautiful flowers! In order to minimize the loss of flowers the next, John hasten to take action, the cattle were sent back to the bullpen.  
Cattle are from 1 to N (2≤N≤100000) number. I-only position where the cow (1≤Ti≤2000000) minutes from the bullpen Ti, and in the bullpen before John began to take her, she would nibble Di (1≤Di≤100) Flowers per minute.
No matter how hard, you can only send John back to a cattle shed. The delivery of the first cow in fact i only need 2Ti minutes, back and forth because it will take time.    
Write a program to determine the order of John transporting cows to minimize the number was eventually swallowed flowers.

Entry

N input row; 
after N lines of input two integers Ti and Di. 

Export

Output an integer, representing the minimum number of flowers swallowed.

Sample input  Copy

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample output Copy

86 
urged to change the meaning of the title face! Cows in Fj last journey was scared silly! They will not eat flowers at this time!
The optimal solution first reaction, first dp it, but found that looks like not too much trouble. . . The sample can be too greedy. But I do not understand the subject, so he fucks a pass, the sample water is not water in the past, so 0 points.
Analysis:
Continue greedy algorithm. Minimum number Fj eat flowers, assumptions: 1 bovine cattle 2, their d and t are represented by dx, tx, dy, ty.
1 is assumed to transport than prior bovine cattle transport solution 2 obtained better.
then:
xd * xt + (xt + t) * w <= w * t + (t + xt) * xd; 
xd * xt xt + * + t * corn corn <= w * t + t + xt * * xd xd; 
化简得
xt * w <= t * xd
(Dazzling) 
Anyway, this thing is sorted according to cmp, you can get the optimal sequence
of code:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200010;
int n;//2 100000
struct node
{
    long long int t,d;
}a[maxn];
long long int t;
long long int ans;
bool cmp(node x,node y)
{
    return x.d*y.t>=y.d*x.t;
}
inline long long read()
{
    long long x=0,f=1;char s=getchar();
    while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
    while(s<='9'&&s>='0'){x=x*10+s-'0';s=getchar();}
    return x*f;
}
int main()
{
    n=read();//scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        a[i].t=read();
        a[i].d=read();//scanf("%d%d",&a[i].t,&a[i].d);
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        ans+=t*a[i].d;
        t+=a[i].t*2;
    }
    printf("%lld",ans);
    return 0;
}

(Finish)

  

 

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11122577.html