Magic Powder - 2 (CF 670_D)

http://codeforces.com/problemset/problem/670/D2

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), 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 (1 ≤ bi ≤ 109), 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.

Examples

input

1 100000000011000000000

output

2000000000

input

10 11000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 10000000001 1 1 1 1 1 1 1 1 1

output

0

input

3 12 1 411 3 16

output

4

input

4 34 3 5 611 12 14 20

output

3
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

#define N 110000
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
const long long  Max = 2000000000;
typedef long long LL;

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

LL Judge(LL mid)
{
    LL i, K1=k, K2=k;

    for(i=1; i<=n; i++)
    {
        if(b[i]<a[i]*mid)
        {
            K1 -= (a[i]*mid - b[i]);
            if(K1<0)
                return -1;
        }
    }

    for(i=1; i<=n; i++)
    {
        if(b[i]<a[i]*(mid+1))
        {
            K2 -= (a[i]*(mid+1) - b[i]);
            if(K2<0)
                return 0;
        }
    }

    return 1;
}

int main()
{

    while(scanf("%I64d%I64d", &n, &k)!=EOF)
    {
        LL i;
        LL mid, L=0, R=Max, ans;

        met(a, 0);
        met(b, 0);

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

        while(L<R)
        {
            mid = (L+R)/2;
            ans = Judge(mid);
            if(ans==0)
                L = R = mid;
            if(ans>0)
                L = mid + 1;
            if(ans<0)
                R = mid - 1;
        }

        printf("%I64d\n", L);
    }

    return 0;
}
				
时间: 2024-12-29 07:08:05

Magic Powder - 2 (CF 670_D)的相关文章

CodeForces - 670D2 Magic Powder - 2 (二分&amp;模拟)

CodeForces - 670D2 Magic Powder - 2 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The term of this problem is the same as the previous one, the only exception - increased restrictions. Input Th

CodeForces - 670D1 Magic Powder - 1 (模拟)

CodeForces - 670D1 Magic Powder - 1 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description This problem is given in two versions that differ only by constraints. If you can solve this problem in large c

【机器学习算法-python实现】协同过滤(cf)的三种方法实现

(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 协同过滤(collaborative filtering)是推荐系统常用的一种方法.cf的主要思想就是找出物品相似度高的归为一类进行推荐.cf又分为icf和ucf.icf指的是item collaborative filtering,是将商品进行分析推荐.同理ucf的u指的是user,他是找出知趣相似的人,进行推荐.通常来讲icf的准确率可能会高一些,通过这次参加天猫大数据比赛,我觉得只有在数据量非

zz [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

http://yidianzixun.com/n/09vv1FRK?s=1 完全摘抄自网页 1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在Web 应用中利用集体智慧构建更加有趣的应用或者得到更好的用户体验.集体智慧是指在大量的人群的行为和数据中收集答案,帮助你对整个人群得到统计意义上的结论,这些结论是我们在单个个体上无法得到的,它往往是某种趋势或者人群

[Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web 应用中利用集体智慧构建更加有趣的应用或者得到更好的用户体验.集体智慧是指在大量的人群的行为和数据中收集答案,帮助你对整个人群得到统计意义上的结论,这些结论是我们在单个个体上无法得到的,它往往是某种趋势或者人群中共性的部分. Wikipedia 和 Google 是两个典型的利用集体智慧的 Web

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

spark MLlib 概念 4: 协同过滤(CF)

1. 定义 协同过滤(Collaborative Filtering)有狭义和广义两种意义: 广义协同过滤:对来源不同的数据,根据他们的共同点做过滤处理. Collaborative filtering (CF) is a technique used by some recommender systems.[1] Collaborative filtering has two senses, a narrow one and a more general one.[2] In general,

CF #262 (DIV2) C . Present (二分答案)

output standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on

URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set