bzoj5106 [CodePlus2017]汀博爾 二分

[CodePlus2017]汀博爾

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 202  Solved: 75
[Submit][Status][Discuss]

Description

有n棵樹,初始時每棵樹的高度爲Hi,第i棵樹每個月都會長高Ai。如今有個木料長度總量爲S的訂單,客戶要求每塊
木料的長度不能小於L,並且木料必須是整棵樹(即不能爲樹的一部分)。如今問你最少須要等多少個月才能知足
訂單。

 

Input

第一行3個用空格隔開的非負整數n,S,L,表示樹的數量、訂單總量和單塊木料長
度限制。
第二行n個用空格隔開的非負整數,依次爲H1,H2,...,Hn。
第三行n個用空格隔開的非負整數,依次爲A1,A2,...,An。
1<=N<=200000,1<=S,L<=10^18,1<=Hi,Ai<=10^9

 

Output

輸出一行一個整數表示答案。

 

Sample Input

3 74 51
2 5 2
2 7 9

Sample Output

7
【 Hints】
對於樣例,在六個月後,各棵樹的高度分別爲 14, 47, 56,此時沒法完成訂單。在七個月後,各棵樹的高度分別
爲 16, 54, 65,此時能夠砍下第 2 和第 3 棵樹完成訂單了。

HINT

 

來自 CodePlus 2017 11 月賽,清華大學計算機科學與技術系學生算法與競賽協會 榮譽出品。

Credit:idea/鄭林楷 命題/鄭林楷 驗題/王聿中

Git Repo:https://git.thusaac.org/publish/CodePlus201711

本次比賽的官方網址:cp.thusaac.org

感謝騰訊公司對這次比賽的支持。

 

Source

 

題解:php

  就是裸的二分答案。ios

 1 #include<cstring>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<cstdio>
 6 
 7 #define N 200007
 8 #define ll long long
 9 using namespace std;
10 inline int read()
11 {
12     int x=0,f=1;char ch=getchar();
13     while(ch>'9'||ch<'0'){if (ch=='-') f=-1;ch=getchar();}
14     while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
15     return x*f;
16 }
17 
18 int n;
19 ll a[N],h[N],S,L,mx;
20 
21 bool check(ll day)
22 {
23     ll ans=0;
24     ll now;
25     for(int i=1;i<=n;i++)
26     {
27         now = a[i]*day+h[i];
28         if(now>=L) ans+=now;
29         if(ans>=S) return true;
30     }
31     return false;
32 }
33 int main()
34 {
35     cin>>n>>S>>L;
36     for(int i=1;i<=n;i++) cin>>h[i];
37     for(int i=1;i<=n;i++) cin>>a[i],mx=max(mx,max(L,S)/a[i]);
38     ll l=0,r=mx,ans=0;
39     while(l<=r)
40     {
41         ll mid=(l+r)>>1;
42         if(check(mid)) r=mid-1;
43         else l=mid+1;
44     }
45     printf("%lld",l);
46 }
相關文章
相關標籤/搜索