顿顿总共选中了 nn 块区域准备开垦田地,由于各块区域大小不一,开垦所需时间也不尽相同。
据估算,其中第 ii 块(1≤i≤n1≤i≤n)区域的开垦耗时为 titi 天。
这 nn 块区域可以同时开垦,所以总耗时 tTotaltTotal 取决于耗时最长的区域,即:
tTotal=max{t1,t2,…,tn}tTotal=max{t1,t2,…,tn}
为了加快开垦进度,顿顿准备在部分区域投入额外资源来缩短开垦时间。
具体来说:
- 在第 ii 块区域每投入 cici 单位资源,便可将其开垦耗时缩短 11 天;
- 耗时缩短天数以整数记,即第 ii 块区域投入资源数量必须是 cici 的整数倍;
- 在第 ii 块区域最多可投入 ci×(ti−k)ci×(ti−k) 单位资源,将其开垦耗时缩短为 kk 天;
- 这里的 kk 表示开垦一块区域的最少天数,满足 0<k≤min{t1,t2,…,tn}0<k≤min{t1,t2,…,tn};换言之,如果无限制地投入资源,所有区域都可以用 kk 天完成开垦。
现在顿顿手中共有 mm 单位资源可供使用,试计算开垦 nn 块区域最少需要多少天?
输入格式
输入共 n+1n+1 行。
输入的第一行包含空格分隔的三个正整数 n,m,kn,m,k,分别表示待开垦的区域总数、顿顿手上的资源数量和每块区域的最少开垦天数。
接下来 nn 行,每行包含空格分隔的两个正整数 titi 和 cici,分别表示第 ii 块区域开垦耗时和将耗时缩短 11 天所需资源数量。
输出格式
输出一个整数,表示开垦 nn 块区域的最少耗时。
数据范围
70%70% 的测试数据满足:0<n,ti,ci≤1000<n,ti,ci≤100 且 0<m≤1060<m≤106;
全部的测试数据满足:0<n,ti,ci≤1050<n,ti,ci≤105 且 0<m≤1090<m≤109。
输入样例1:
4 9 2
6 1
5 1
6 2
7 1
输出样例1:
5
样例1解释
如下表所示,投入 55 单位资源即可将总耗时缩短至 55 天。此时顿顿手中还剩余 44 单位资源,但无论如何安排,也无法使总耗时进一步缩短。
ii | 基础耗时 | titi 缩减 11 天所需资源 | cici 投入资源数量 | 实际耗时 |
---|---|---|---|---|
11 | 66 | 11 | 11 | 55 |
22 | 55 | 11 | 00 | 55 |
33 | 66 | 22 | 22 | 55 |
44 | 77 | 11 | 22 | 55 |
输入样例2:
4 30 2
6 1
5 1
6 2
7 1
输出样例2:
2
样例2解释
投入 2020 单位资源,恰好可将所有区域开垦耗时均缩短为 k=2k=2 天;受限于 kk,剩余的 1010 单位资源无法使耗时进一步缩短。
思路:因为答案是有顺序的,所以直接二分枚举答案就好
import java.io.*;
/**
* @Author 菜狗
*/
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
static int N = (int)1e5+7,n;
static int[] a = new int[N], b = new int[N];
public static boolean check(int t,long m){
for (int i = 1; i <= n; i++){
if((a[i]-t)>0)m-=(long)b[i]*(a[i]-t);
if(m<0)return false;
}
return true;
}
public static void main(String[] args) throws IOException {
n = nextInt();
long m = nextInt();
int k = nextInt();
boolean f = true;
for(int i = 1; i <= n; i++){
a[i] = nextInt()-k;
if(a[i]>0)f = false;
b[i] = nextInt();
}
if(f){
int max = 0;
for(int i = 1; i <= n; i++)max = Math.max(max,a[i]+k);
out.println(max);
out.flush();
return;
}
int l = 0, r = (int)1e9;
while (l<=r){
int mid = l + r >> 1;
if (check(mid,m))r = mid - 1;
else l = mid + 1;
}
out.println(l+k);
out.flush();
}
}