[OpenJ_Bailian - 2795] Treasure Island (greedy)

Treasure Island

Descriptions:

KID day use aircraft flew on a Treasure Island, there are many precious metal above, although more like works of art KID variety of precious stones, but do not reject such a precious metal. However, he only with a pocket, the pocket can only be loaded up to a weight of the article w. The island metals s species, different for each metal weight, respectively, n-  . 1 , n-  2 , ..., n-  s , while the total value of each type of metal or different and each is a V  . 1 , V  2 ,. .., V  S . KID want to take away as much as possible once the value of the metal, and asked him up to take away much of the value of the metal. It noted that the metal can be arbitrarily divided, and the value of the metal and its proportional weight.

Input

Line 1 is the group number k of test data, followed by k sets of inputs. 

Each test accounted for 3, line 1 is a positive integer w (1 <= w <= 10000), represents the upper limit of the pocket bearing. The second row is a positive integer s (1 <= s <= 100), represents a metal species. Line 3 has 2s positive integers, respectively, n-  . 1 , V  . 1 , n-  2 , V  2 , ..., n-  s , V  s , respectively for the first, second, ..., s-metal and the total value of the total weight of (. 1 <= n-  I  <= 10000,. 1 <V =  I  <= 10000). 
Output

k rows, each row corresponding to an input output. The output should be accurate to 2 decimal places.

Sample Input

2
50
4
10 100 50 30 7 34 87 100
10000
5
1 43 43 323 35 45 43 54 87 43

Sample Output

171.93
508.00

Topic links:

https://vjudge.net/problem/OpenJ_Bailian-2795

This problem is simply OpenJ_Bailian - 4110 of deformation, exactly the same right

Links: https://www.cnblogs.com/sky-stars/p/11073133.html

Nothing to say, directly on the code

AC Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0 (X) Memset (X, 0, the sizeof (X))
 the using  namespace STD;
 struct Gift {
     Double V, w;
     // defined in accordance v / w of a size sorting, v / w big priority, i.e. gift the average value of a large priority 
    BOOL  operator <( const Gift & c) const
    {
        return v/w-c.v/c.w > eps;
    }
};
gift a[105];
int n;
double W;
double sum=0;
int main()
{
    cin>>n>>W;
    for(int i=0; i<n; i++)//输入
        cin>>a[i].v>>a[i].w;
    sort(a,a+n);//排序
    for(int i=0; i<n; i++)
    {
        if(W>=a[i].w)
        {
            sum+=a[i].v;
            W-=a[i].w;
        }
        else if(W<a[i].w)
        {
            double t=(a[i].v/a[i].w)*W;
            sum+=t;
            W=0;
        }
        else if(W==0)
        {
            break;
        }
    }
    printf("%.1lf\n",sum);
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11073169.html