日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Magic Powder - 1,2

發布時間:2024/1/1 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

InputcopyOutputcopy
3 1 2 1 4 11 3 16 4

Sample 2

InputcopyOutputcopy
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

InputcopyOutputcopy
1 1000000000 1 1000000000 2000000000

Sample 2

InputcopyOutputcopy
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

InputcopyOutputcopy
3 1 2 1 4 11 3 16 4

Sample 4

InputcopyOutputcopy
4 3 4 3 5 6 11 12 14 20 3
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> #include<ctype.h> using namespace std; typedef long long ll;ll n,m; ll g[100010],k[100010],p[100010];ll check(ll x) {ll res=0;for(ll i=0; i<n; i++){if(g[i]*x>k[i]) res=res+g[i]*x-k[i];if(res>m) return 0;}return 1; }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];ll l=0,r=2000000000;while(l<r){ll mid=(l+r+1)>>1;if(check(mid)) l=mid;else r=mid-1;}cout<<l;return 0; }

和前面那個一樣,注意數據范圍,還有check函數里if判斷條件不能像第一個那樣寫,數據太大,乘法和減法一塊會容易出錯,最好像第二個代碼那樣寫

總結

以上是生活随笔為你收集整理的Magic Powder - 1,2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。