Protecting the Flower(贪心)
Protecting the Flowers
https://ac.nowcoder.com/acm/problem/25043
一开口就是老贪心题了
题意要求牛吃掉的最少的花,
我们知道
1.如果把运输时间短的牛放在前面先运输过去,可以减少牛吃花的数量
2.如果把牛每分钟吃花多的放在前面 也可减少牛吃花的数量
定义r=t/d
以上两种情况 t越大越靠前, d越小越靠前, **
**综上 所以r越大越靠前
#include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstdio> #include <bitset> #include <vector> #include <iomanip> #include <sstream> #include <cstring> #include <iostream> #include <algorithm> #include <unordered_map> #define UpMing main #define re register #pragma GCC optimize(2) #define Accept return 0; #define lowbit(x) ((x)&(-(x))) #define mst(x, a) memset( x,a,sizeof(x) ) #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define dep(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; typedef long long ll; typedef pair<int,int> PII; typedef unsigned long long ull; const int inf =0x3f3f3f3f; const int maxn=2e5+7; const ll mod = 1e9+7; const int N =1e6+7; inline ll read() { ll x=0; bool f=0; char ch=getchar(); while (ch<'0'||'9'<ch) f|=ch=='-', ch=getchar(); while ('0'<=ch && ch<='9') x=x*10+ch-'0',ch=getchar(); return f?-x:x; } void out(ll x) { int stackk[20]; if(x<0) { putchar('-'); x=-x; } if(!x) { putchar('0'); return; } int top=0; while(x) stackk[++top]=x%10,x/=10; while(top) putchar(stackk[top--]+'0'); } ll n; struct wmy { double t,d; } a[maxn]; bool cmp(wmy x,wmy y) { return (x.d/x.t)<(y.d/y.t); } int UpMing() { n=read(); for(int i=1 ; i<=n ; i++) cin>>a[i].t>>a[i].d; sort(a+1,a+1+n,cmp); ll ans=0,tt=a[1].t*2; for(int i=2 ; i<=n ; i++) { ans+=tt*a[i].d; tt+=a[i].t*2; } cout<<ans; printf("\n"); Accept; }