Code + first monthly match B. Timberlake (two points)

Title Description

There are n trees. At the beginning, the height of each tree is Hi. The ith tree grows Ai every month. Now there is an order with the total length of wood S. The customer requires that the length of each piece of wood should not be less than
50. And the wood must be the whole tree (i.e. not part of the tree). Now ask how many months you need to wait to meet your order.

Input format

Read in data from standard input.

The first line consists of three non negative integers, n,S,L, separated by spaces, representing the number of trees, the total number of orders, and the length limit of a single piece of wood.

In the second line, n non negative integers separated by spaces are H1,H2 Hn.

In the third line, n non negative integers separated by spaces are A1,A2 An.

Output format

Output to standard output.

Output an integer line by line for the answer.

Example 1 input

3 74 51
2 5 2
2 7 9

Sample 1 output

7

Hints

For the example, after six months, the height of each tree is 14, 47, 56 respectively, and the order cannot be completed at this time.

After seven months, the height of each tree is 16, 54 and 65 respectively. At this time, the second and third trees can be cut down to complete the order.

thinking

Divide the month into two parts, and then judge whether the current month meets the conditions. Pay attention to long long

The range size is the maximum value from - 1 to the total order quantity to the individual timber limit

Code

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const ll N=200000+20;
ll a[N],h[N];
ll n,s,limit;
inline ll read()
{
    ll ret=0;
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while ('0'<=ch&&ch<='9')
    {
        ret=ret*10-48+ch;
        ch=getchar();
    }
    return ret;
}
ll judge(ll time)
{
    ll sum=0;
    for(ll i=1; i<=n; i++)
    {
        if(limit>h[i]&&time<=(limit-h[i]-1)/a[i])
            continue;
        if(time>=(s-h[i])/a[i])
            return 1;
        sum+=time*a[i]+h[i];
        if(sum>=s)
            return 1;
    }
    return 0;
}
int main()
{
    scanf("%lld%lld%lld",&n,&s,&limit);
    for(ll i=1; i<=n; i++) h[i]=read();
    for(ll i=1; i<=n; i++) a[i]=read();
    ll l=-1,r=max(limit,s);
    while(l+1<r)
    {
        ll mid=(l+r)>>1;
        if(judge(mid))
            r=mid;
        else
            l=mid;
    }
    printf("%lld\n",r);
    return 0;
}

Tags: less

Posted on Mon, 04 May 2020 01:30:15 -0400 by billf