D2 Magic Powder -1/- 2---cf#350D2(二分)

题目链接:http://codeforces.com/contest/670/problem/D2

 

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.

Input

The first line of the input contains two positive integers n and k  — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an , 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 , where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

 

题意:有n中材料,每种材料有b克,他想做饼干,做1个饼干需要每种材料ai克,现在有k克魔法粉,这k克魔法粉可以变成任意一种材料,求最终最多做多少个饼干;

有两道题,一个所有数据范围是10^3,这个的话我们可以直接暴力解决;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 2011
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f

int a[N], b[N], n, k;

int main()
{
    while(scanf("%d %d", &n, &k)!=EOF)
    {
        met(a, 0); met(b, 0);

        for(int i=1; i<=n; i++)
            scanf("%d", &a[i]);

        int Min = INF, sum = 0;

        for(int i=1; i<=n; i++)
        {
            scanf("%d", &b[i]);
            Min = min(Min, b[i]/a[i]);
        }
        for(int i=1; i<=n; i++)
        {
            b[i] = b[i] - a[i]*Min;
            if(b[i]<a[i])
                sum += a[i]-b[i];
        }
        while(sum<=k)
        {
            Min ++;
            k -= sum;
            sum = 0;
            for(int i=1; i<=n; i++)
            {
                b[i] -= a[i];
                if(b[i]<0)b[i] = 0;
                if(b[i]<a[i])
                    sum+=a[i]-b[i];
            }
        }
        printf("%d\n", Min);
    }
    return 0;
}
View Code

另一个所有数据范围变成10^9时我们可以想到,他最多做不超过2*(10^9)个饼干,所以我们可以二分搜索答案,一直到找到符合题意de饼干个数为止;

注意中间过程会爆int的所以用long long

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 200100
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
const int MAXN = 2*(1e9)+10;

LL a[N], b[N], k;
int n;

LL Search(LL L, LL R)
{
    while(L <= R)
    {
        LL x = (L+R)/2;

        LL sum = 0;

        for(int i=1; i<=n; i++)
        {
            if( b[i] < a[i]*x )
                sum += (a[i]*x) - b[i];
            if(sum > k)///防止爆long long;
                break;
        }
        if(sum == k)///当刚好满足题意时,返回;
            return x;
        else if(sum < k)
            L = x + 1;
        else
            R = x -1;
    }
    return L-1;
}

int main()
{
    while(scanf("%d %I64d", &n, &k)!=EOF)
    {
        met(a, 0); met(b, 0);

        for(int i=1; i<=n; i++)
            scanf("%I64d", &a[i]);

        for(int i=1; i<=n; i++)
            scanf("%I64d", &b[i]);

        LL ans = Search(1, MAXN-1);
        printf("%I64d\n", ans);
    }
    return 0;
}
View Code

 

posted @ 2016-05-06 10:24  西瓜不懂柠檬的酸  Views(293)  Comments(0Edit  收藏  举报
levels of contents