题目链接:http://codeforces.com/problemset/problem/670/D1
题目:Magic Powder - 1
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 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.
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 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.
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
3 1 2 1 4 11 3 16
4
4 3 4 3 5 6 11 12 14 20
3
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 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.
思路:数据要求不大,直接进行暴力求解
代码:
#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;
}