Magic Powder - 1,2
問題
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs?n?ingredients, and for each ingredient she knows the value?ai?— how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all?n?ingredients.
Apollinaria has?bi?gram of the?i-th ingredient. Also she has?k?grams of a magic powder. Each gram of magic powder can be turned to exactly?1?gram of any of the?n?ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
輸入?
The first line of the input contains two positive integers?n?and?k?(1?≤?n,?k?≤?1000)?— the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence?a1,?a2,?...,?an?(1?≤?ai?≤?1000), where the?i-th number is equal to the number of grams of the?i-th ingredient, needed to bake one cookie.
The third line contains the sequence?b1,?b2,?...,?bn?(1?≤?bi?≤?1000), where the?i-th number is equal to the number of grams of the?i-th ingredient, which Apollinaria has.
輸出?
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Sample 1
| 3 1 2 1 4 11 3 16 | 4 |
Sample 2
| 4 3 4 3 5 6 11 12 14 20 | 3 |
Note
In the first sample it is profitably for Apollinaria to make the existing?1?gram of her magic powder to ingredient with the index?2, then Apollinaria will be able to bake?4?cookies.
In the second sample Apollinaria should turn?1?gram of magic powder to ingredient with the index?1?and?1?gram of magic powder to ingredient with the index?3. Then Apollinaria will be able to bake?3?cookies. The remaining?1?gram of the magic powder can be left, because it can't be used to increase the answer.
解題思路 :
做一塊餅干需要n種材料,k個魔法材料,
ai為每種材料的克數
bi為現有每種材料的總數,魔法材料可以代替任何一種材料,問最大能做多少。
二分思想:最多能做多少個,從比題目給的范圍大的范圍里二分,比如左邊0,右邊10000,從這個區間內開始二分。比方做一個所用到材料數乘以n的結果,小于等于我們所現有的總材料了,這n個才能做出來,如果大于我們所擁有的材料總數,說明我們的材料不夠,需要從k個魔法材料里面拿,如果魔法材料被拿完了,變成負數,就意味著這n個做不成,然后縮小n的范圍,否則就增大n的范圍
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> using namespace std;int n,m; int g[1010],k[1010],p[1010];int check(int x) {int res=0;for(int i=0; i<n; i++){p[i]=k[i]-x*g[i];if(p[i]<0) res-=p[i];}if(res<=m) return 1;else return 0; }int main() {cin>>n>>m;for(int i=0; i<n; i++) cin>>g[i];for(int i=0; i<n; i++) cin>>k[i];int l=0,r=10000;while(l<r){int mid=(l+r+1)>>1;if(check(mid)) l=mid;else r=mid-1;}cout<<l;return 0; }輸入
The first line contains two positive integers?n?and?k?(1?≤?n?≤?100?000,?1?≤?k?≤?109) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence?a1,?a2,?...,?an?(1?≤?ai?≤?109), where the?i-th number is equal to the number of grams of the?i-th ingredient, needed to bake one cookie.
The third line contains the sequence?b1,?b2,?...,?bn?(1?≤?bi?≤?109), where the?i-th number is equal to the number of grams of the?i-th ingredient, which Apollinaria has.
輸出
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Sample 1
| 1 1000000000 1 1000000000 | 2000000000 |
Sample 2
| 10 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 1 1 1 1 1 | 0 |
Sample 3
| 3 1 2 1 4 11 3 16 | 4 |
Sample 4
| 4 3 4 3 5 6 11 12 14 20 | 3 |
和前面那個一樣,注意數據范圍,還有check函數里if判斷條件不能像第一個那樣寫,數據太大,乘法和減法一塊會容易出錯,最好像第二個代碼那樣寫
總結
以上是生活随笔為你收集整理的Magic Powder - 1,2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OriginLab2015安装过程中注意
- 下一篇: 2022年T电梯修理题库及模拟考试