codeforces 670D1 Magic Powder - 1
?題目鏈接:http://codeforces.com/problemset/problem/670/D1
題目:Magic Powder - 1
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputThis 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 needsn ingredients, and for each ingredient she knows the valueai?— how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use alln ingredients.
Apollinaria has bi gram of thei-th ingredient. Also she hask grams of a magic powder. Each gram of magic powder can be turned to exactly1 gram of any of then 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.
InputThe 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 thei-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 thei-th ingredient, which Apollinaria has.
OutputPrint the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples Input 3 1 2 1 4 11 3 16 Output 4 Input 4 3 4 3 5 6 11 12 14 20 Output 3 NoteIn 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 index1 and1 gram of magic powder to ingredient with the index3. Then Apollinaria will be able to bake3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
題意:首先給出有n種原材料和k個魔法棒(一個魔法棒可變成任意原材料中任意一種中的的一份),接下來給出加工一件物品所需每種原材料的量,下面給出現在每種原材料所有量,求做多可以做出多少件物品思路:數據要求不大,直接進行暴力求解
代碼:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <cstring> #include <stack> using namespace std;int n,k; int a[1005]; int b[1005]; struct point {int a;int b;int c; } p[1005];int main() {cin >> n >> k;int maxn = 0xffffff;for(int i = 0; i < n; i++) {cin >> a[i];}for(int i = 0; i < n; i++) {cin >> b[i];p[i].a = b[i]/a[i];//獲得這一樣東西最大能夠滿足多少個p[i].b = b[i]%a[i];//獲得剩余量p[i].c = a[i]-p[i].b;//獲得補多少maxn = min(maxn,p[i].a);}while(true) {int i;for(i = 0; i < n; i++) {if(p[i].a == maxn) {//說明只有這種物品數量不夠,需要補充if(k < p[i].c) {//魔法棒的數量不足以填充break;} else {k -= p[i].c;//魔法棒數量減少多少p[i].c = a[i];//說明下一次需要補的量p[i].a = maxn+1;//補一次這種的個數就加一保證下一次仍然要繼續補}}}if(i == n) {maxn++;} else {break;}}cout << maxn << endl;return 0; }D2數據量變大以后直接對答案進行二分
AC代碼:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <cstring> #include <stack> using namespace std;int n,k; int a[100005]; int b[100005];bool ok(long long x) {long long s = k;for(int i = 0; i < n; i++) {if(x*a[i] >= b[i]) {s -= (x*a[i]-b[i]);}if(s < 0) {return false;}}return true; } int main() {cin >> n >> k;for(int i = 0; i < n; i++) {cin >> a[i];}for(int j = 0; j < n; j++) {cin >> b[j];}if(n == 1) {cout << (b[0]+k)/a[0] << endl;} else {int sum =0;long long l = 0,r = 0xffffffff;long long mid;while(l <= r) {mid=(l+r)>>1;if(ok(mid)) {l = mid+1;sum = mid;} else {r = mid-1;}}cout << sum << endl;}return 0; }總結
以上是生活随笔為你收集整理的codeforces 670D1 Magic Powder - 1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3dsMax 快捷键
- 下一篇: mysql jion on月季_月季花展