codeforces 670D1 Magic Powder - 1
来源:互联网 发布:大数据预测彩票 编辑:程序博客网 时间:2024/05/16 23:53
题目链接: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 12 1 411 3 16
4
4 34 3 5 611 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 remaining1 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;}
- codeforces 670D1 - Magic Powder - 1
- D - Magic Powder - 1 codeforces 670d1
- codeforces 670D1 Magic Powder - 1
- CodeForces - 670D1 Magic Powder - 1 (模拟)
- Codeforces 670 D1. Magic Powder - 1【二分查找】
- Codeforces 670D2 Magic Powder
- codeforces 670D2. Magic Powder - 2
- codeforces 670D2 - Magic Powder - 2
- CodeForces 670D2 Magic Powder - 2
- 【CodeForces】[670D2]Magic Powder - 2
- CodeForces 670D2 Magic Powder - 2
- Codeforces 670D2 Magic Powder-2
- CodeForces 670D2 Magic Powder - 2(二分+贪心)
- Codeforces 670D2 Magic Powder - 2 二分答案
- CodeForces - 670D2 Magic Powder - 2 (二分&模拟)
- 【CodeForces】670D2 - Magic Powder - 2(二分)
- CodeForces 670D Magic Powder - 2 (二分)
- Codeforces 670D2:Magic Powder - 2(二分)
- 如果使用编程判断闰年和平年,大神勿喷!
- 剑指offer面试题2(单例设计模式)
- VM虚拟机常见问题之-虚拟机操作系统安装问题
- 蓝牙中心开发
- 屏幕截图实现
- codeforces 670D1 Magic Powder - 1
- 1017 Problem Q
- KMP,深入讲解next数组的求解
- POJ 2392-Space Elevator(多重部分和-多重背包)
- iOS 数据存储方式(XML属性列表-plist)
- 九度OJ 1000:计算A+B
- BZOJ2780——[Spoj]8093 Sevenk Love Oimaster
- Light oj 1387 - Setu【字符串】
- CRT与Windows的关系【转】