D. Freddy and minifier

D. Freddy and minifier
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Freddy has a very big elephant, Simon. He is flying to Gabon in order to let Simon meet his brothers and sisters, however he is very big, which means he doesn't really fit in the plane. In order to make him fit Freddy needs to minify his size. In order to do that he has N minifying guns (they make stuff smaller). However, each gun can only be used once. Each gun has a cost of using it c*w, where c is unique per gun and w is the weight of the thing they are minifying. Additionally each gun has a minify ratio called r, which means that the size and weight get reduced to r percent of the original value. Given that the initial weight of Simon is W and he is going to get shrunk by each of the N guns exactly once, what is the minimum cost of doing this?

Input
Two integers, N and W (1<=N<=105,1<=W<=104). Followed by N lines each with an integer, c (1<=c<=104) and a real number r (0<=r<=1), the c and r values of the ith gun.

Output
A real number, the minimum cost of minifying Simon with each of the N guns exactly once. Your answer will be considered correct if its relative or absolute error doesn't exceed 10−6
Examples
inputCopy
2 1
7 0.5
9 0.2
outputCopy
10.400000000000
inputCopy
2 1
7 0.5
90 0.2
outputCopy
52.000000000000

题目描述:

有n支缩小枪,每支都要用一次,每次使用花费w*c,用后使w变成w*r。问最小花费多少。

分析:

主要使通过排序,达到一个使花费最小的顺序。排序方式如下:

struct cd
{
    int c;
    double r;
}a[maxn];
bool cmp(struct cd a,struct cd b)
{
    return a.c+b.c*a.r<b.c+a.c*b.r;
}

比如:c1,r1    c2,r2    c3,r3。那么答案就是c1*w+c2*w*r1+c3*w*r1*r2=w*(c1+c2*r1+c3*r1*r2)=w*((c1+c2*r1)+c3*r1*r2)=w*(c1+r1*(c2+c3*r2))。因为乘r使变小,所以这样排可以让大的尽可能多乘r,使得答案最小。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
#define sd(x) scanf("%d",&x)
#define ms(x) memset(x,0,sizeof x)
#define fr(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
const int maxn=1e5+6;
const int INF=0x4f4f4f4f;
struct cd
{
    int c;
    double r;
}a[maxn];
bool cmp(struct cd a,struct cd b)
{
    return a.c+b.c*a.r<b.c+a.c*b.r;
}
int main()
{
    int n;
    double w;
    cin>>n>>w;
    double ans=0;
    fr(i,n)
    {
        scanf("%d%lf",&a[i].c,&a[i].r);
    }
    sort(a+1,a+n+1,cmp);
    double t=w;
    fr(i,n)
    {
        ans+=a[i].c*t;
        t*=a[i].r;
    }
    printf("%.9lf\n",ans);
    return 0;
}

 

posted on 2020-05-23 11:17  Aminers  阅读(148)  评论(0编辑  收藏  举报

导航