BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花【排序】

1634: [Usaco2007 Jan]Protecting the Flowers 护花

【题目描述】
传送门

【题解】

我们考虑i和i+1那个放前面更优秀,这两个换一个顺序对其他元素没有影响。
i在前Di+1Ti
i+1在前DiTi+1
那么就用这个进行排序就可以了。

【代码如下】

#include<cstdio>
#include<algorithm>
using namespace std;
int n;long long Ans;
struct xcw{
    int T,D;
    bool operator <(const xcw b)const{return b.D*T<D*b.T;}
}a[100005];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",&a[i].T,&a[i].D);
    sort(a+1,a+1+n);
    long long Alt=0;
    for(int i=1;i<=n;i++) Ans+=Alt*a[i].D,Alt+=2*a[i].T;
    printf("%lld\n",Ans);
    return 0;
}
posted @ 2018-05-18 20:18  XSamsara  阅读(130)  评论(0编辑  收藏  举报