1. 程式人生 > >C++_FatMouse' Trade(貪心)

C++_FatMouse' Trade(貪心)

Description

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

Output

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

Sample Input

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output

13.333
31.500

思路

獲得每一個的價格,然後每次都拿最小的。一開始建立了三個陣列,然後打算自己寫一個排序演算法,根據價格的值同時修改三個陣列,後來受人指點,把三個值寫成一個結構體就行了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define N 1005

//#define DEBUG
using namespace std;

int m, n;
double ans;
struct Fat{
    double price;
    int j, f;
};

vector<Fat> good(N);

bool myCompare(Fat a, Fat b) { return a.price < b.price; }

int main()
{
    while(~scanf("%d%d", &m, &n))
    {
        ans = 0;
        if(m<0||n<0) break;
        for(int i = 0;i<n;i++)
        {
            scanf("%d%d", &good[i].j, &good[i].f);
            good[i].price = (double)good[i].f/(double)good[i].j;
        }
        sort(good.begin(), good.begin() + n, myCompare);
#ifdef DEBUG
        for(int i= 0;i<n;i++)
        {
            cout << " J : " << good[i].j << "F : " << good[i].f
             << endl << "Price : " << good[i].price << endl;
        }
#endif // DEBUG
        for(int i = 0;i<n;i++)
        {
            if(m<=0) break;
            if(m>=good[i].f)
            {
                ans+=good[i].j;
                m-=good[i].f;
            }
            else
            {
                ans+=(double)m/good[i].price;
                m = 0;
            }
        }
        printf("%.3lf\n", ans);


    }
    return 0;
}

相關推薦

C++_FatMouse' Trade貪心

Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite fo

AtCoder NIKKEI Programming Contest 2019 C. Different Strokes 貪心

scanf 貪心 size lld 題解 sign double n) ont 題目鏈接:https://nikkei2019-qual.contest.atcoder.jp/tasks/nikkei2019_qual_C 題意:給出 n 種食物,Takahashi 吃下獲

Educational Codeforces Round 49 (Rated for Div. 2)-C-Minimum Value Rectangle貪心

C. Minimum Value Rectangle time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard

Newcoder 83 C.吉姆的奇思妙想貪心

Description 給你兩個正整數M,nM, nM,n, 以及兩個長度為nnn個正整數序列deg1,deg2,...,degndeg_1,deg_2,..., deg_ndeg1​,deg2​,..

Codeforces Round #277 (Div. 2) A 水題B水題 C貪心

A. Calculating Function For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calcul

Codeforces Round #501 (Div. 3) C貪心

C. Songs Compression time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡貪心

castle con name 個數 blank 調整 can 貪心 pro 【題目鏈接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3399 【題目大意】    將一個集合調整成另一個集合中的數,把一個

C# 關鍵字explicit顯示,implicit隱式,類型的隱式和顯式轉換

tar oid bsp color col 必須 code 類型 顯示 class Program { static void Main(string[] args) { Adaptee ada = ne

第一個CGI程序-----完全就是普通的c語言嘛‘*∩_∩*

同學 pat gree ostream 出現 targe 普通 get 方便 第一個CGI程序 ————完全就是普通的C語言嘛 ‘(*∩_∩*)′ PainterQ 2017年5月14日 上一篇博文裏面敘述了Apache的安裝和配置方法,恍恍惚惚我就擁有了自

c# 進程

for 定義 blog 調用 image method 程序 leg pri 1、進程,進程和多線程的概念: 進程:一個程序運行時所有資源的統稱。 線程:每一個響應執行流就是線程。(做不同的事情) 多線程:多個線程同時工作 2、同步和異步的概念: 同步:前面的先執行,後面的

深入淺出數據結構C語言版9——多重表廣義表

不同 滿足 大學 logs 維數 我會 明顯 http 多維   在深入淺出數據結構系列前面的文章中,我們一直在討論的表其實是“線性表”,其形式如下:   由a1,a2,a3,……a(n-1)個元素組成的序列,其中每一個元素ai(0<i<n)都是一個“原子”,“

C#學習筆記7——委托

() namespace test task cnblogs [] string 命名空間 program 說明(2017-5-29 22:22:50): 1. 語法:public delegate void mydel();這一句在類外面,命名空間裏面。 2. 專門新建一

HDU 1051: Wooden Sticks貪心

memory others sticks mac resp setup cto ret cst Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768

C++中sizeofstruct怎麽計算?

color 發現 pre blank str io7 bsp com 說明 struct為空時,大小為1. 1、 sizeof應用在結構上的情況 請看下面的結構: 1 struct MyStruct 2 { 3 double dda1; 4 char dda; 5 int

讓我們把KBEngine玩壞吧!如何定制我們自己的C++函數

data ase erro glob alt ins sin 程序 all 為什麽不更新kbe warring的代碼解讀了,因為在我看來那個demo講完了實體就沒東西可講了,如果專心的看官方文檔和PPT的話demo的代碼後面沒任何難點了已經,單純的復制黏貼代碼實在太過無聊。

HDU 4415 Assassin&#39;s Creed貪心

all ria put space clas name 它的 tor problem pid=4415">HDU 4415 題意: 壯哉我Assassin! E叔有一柄耐久度為m的袖劍,以及n個目標士兵要去解決。 每解決掉一個士兵,消耗袖劍Ai的

hdu 4882 ZCC Loves Codefires貪心

string.h struct hdu cmp using return 貪心 ace algo # include<stdio.h> # include <algorithm> # include <string.h> usin

[LeetCode]Wildcard Matching 通配符匹配貪心

復雜度 能夠 sts als [0 popu tco ret lee 一開始採用遞歸寫。TLE。 class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,in

c++ 函數1

row 引用傳參 修飾 尋址 概念 pan 但是 clu 過去 函數中,數組如何傳參? 整個數組傳過去是不可以的。自然聯想到指針和引用。 因為數組指針本質上就是多重指針,所以以下三種傳參方式都是可以的: void print(const int*); v

C# SQL封裝

c# sql封裝(二)接 C# SQL封裝(一)開始操作數據庫的測試 :using System;using System.Collections.Generic;using System.Linq;using System.Text;using DBDome.com;using DBDome.model;u