其他分享
首页 > 其他分享> > Protecting the flowers 贪心

Protecting the flowers 贪心

作者:互联网

题目:

https://ac.nowcoder.com/acm/problem/25043

//思路:需要推出贪心的式子,后面的和接水一个原理

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+7;
struct no
{
int d,t;
};
int cmp(no a,no b)
{
return a.t*b.d<a.d*b.t;
}
int main()
{struct no n[maxn];
int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
scanf("%d %d",&n[i].t,&n[i].d);
sort(n,n+q,cmp);
long long sum=0;
long long ans=0;
for(int i=0;i<q;i++)
{ ans+=sum*n[i].d;
sum+=n[i].t;
}
printf("%lld\n",2*ans);
}

标签:struct,no,int,sum,long,ans,Protecting,flowers,贪心
来源: https://www.cnblogs.com/aacm/p/14843563.html