题意:今天我们要来造房子。造这个房子需要n种原料,每造一个房子需要第i种原料ai个。现在你有第i种原料bi个。此外,你还有一种特殊的原料k个,
每个特殊原料可以当作任意一个其它原料使用。那么问题来了,你最多可以造多少个房子呢?
析:首先可以先把开始能造出的先处理出来,然后再进行二分,当然也可以直接进行二分。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 10; const int mod = 1000000007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn], b[maxn]; bool judge(LL mid){ LL mm = m; for(int i = 0; i < n; ++i){ if(b[i] / a[i] >= mid) continue; mm += b[i] - a[i] * mid; if(mm < 0) return false; } return true; } int main(){ scanf("%d %d", &n, &m); LL sum = 0; for(int i = 0; i < n; ++i){ scanf("%d", a+i); sum += a[i]; } int ans = INF; for(int i = 0; i < n; ++i){ scanf("%d", b+i); ans = min(ans, b[i] / a[i]); } for(int i = 0; i < n; ++i) b[i] -= a[i] * ans; LL l = 0, r = m; while(l < r){ int mid = l + (r-l+1) / 2; if(judge(mid)) l = mid; else r = mid - 1; } cout << ans + l << endl; return 0; }