luogu 2878 贪心

时间:2021-11-27 19:18:50

其实这题不难,只是想告诉自己:贪心不全是真的脑残拿最大就AC

此题实际上就是比较x,y优先级利用时间计算得到a[i]t/a[i].d(没错时间在上,并非惯性思维的d在上)

t*a[x].d+(t+a[x]*t)*a[y].d<=t*a[y].d+(t+a[y].t)*a[x].d分别比较不同顺序带给两者的花费

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define LL long long
using namespace std;
const int N=;
int n;LL ans,now;
inline int read(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x*f;}struct node{int t,d;double k;}a[N];
bool cmp(node a,node b){return a.k<b.k;}int main(){
n=read();rep(i,,n) a[i].t=read()*,a[i].d=read(),a[i].k=(double)a[i].t/a[i].d;
sort(a+,a++n,cmp);
for(int i=;i<=n;i++)
ans+=now*a[i].d,now+=a[i].t;
printf("%lld",ans);return ;}