[CSP-S Simulation Test]: Merchant (binary answer)

Title Description

There $ $ n-th items of $ I $ th item has two attributes $ k_i, b_i $, $ indicates that the value of time for the X $ $ k_i \ times x + b_i $ .
$ 0 $ is currently in the moment, you can choose not to exceed $ m $ th article, so that there is some integer time $ t, t \ geqslant 0 $ , the total value of all items you choose is more than equal to $ S $.
Gives $ S $, for the minimum of $ t $.


Input Format

Read data from files $ merchant.in $ in.
The first row of three integers $ n, m, S $.
Next $ $ n-row, two rows of integers $ I $ $ k_i, b_i $.


Output Format

Output to a file in $ merchant.out $.
A row of integer answer.


Sample

Sample input 1:

3 2 100
3 9
-2 50
4 1

Sample output 1:

13

Sample input 2:

3 2 100
-1 49
-2 50
1 -998244353

Sample Output 2:

998244453


Data range and tips

Sample $ 1 $ explanation:

Select the number of items $ 1,3 $.

Sample $ 2 $ explanation:

Select $ 3 $ number items.

data range:

For all data, there is $ 1 \ leqslant m \ leqslant n \ leqslant 10 ^ 6, -10 ^ 9 \ leqslant b_i \ leqslant 10 ^ 9, -10 ^ 6 \ leqslant k_i \ leqslant 10 ^ 6,0 \ leqslant S \ leqslant 10 ^ {18} $.
Data guarantee a solution, and the answer is not more than $ 10 ^ 9 $.
$ \ bullet Subtask1 (22 \% ) $, $ n \ leqslant 20 $.
$ \ bullet Subtask2 (36 \% ) $, $ n \ leqslant 10 ^ 5,0 \ leqslant k_i \ leqslant 10 ^ 4 $.
$ \ bullet Subtask3 (8 \% ) $, $ k_i \ leqslant 0 $.
$ \ bullet Subtask4 (12 \% ) $, $ n \ leqslant 10 ^ 5 $.
$ \ bullet Subtask5 (22 \% ) $, no special constraints.


answer

Obviously we will one day we will want to buy this $ m $ items are bought at the end.

So the answer is monotone, half the number of days can be.

But sit this data range, we obviously can not use $ sort $ in $ judge $ time, otherwise the last $ 22 $ points do not run in the past, we can use the $ nth \ text {_} element $ minus a $ \ log n $.

Time complexity: $ \ Theta (n \ log 10 ^ 9) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,m;
long long S;
pair<int,int> e[1000001];
long long flag[1000001];
bool judge(int x)
{
	for(int i=1;i<=n;i++)
		flag[i]=1LL*e[i].first*x+e[i].second;
	nth_element(flag+1,flag+n-m+1,flag+n+1);
	long long res=0;
	for(int i=n;i>n-m;i--)
	{
		if(flag[i]>0)res+=flag[i];
		if(res>=S)return 1;
	}
	return 0;
}
int main()
{
	scanf("%d%d%lld",&n,&m,&S);
	long long sum=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&e[i].first,&e[i].second);
		sum+=e[i].second;
	}
	if(sum>=S)
	{
		puts("0");
		return 0;
	}
	int lft=0,rht=1000000000,ans=1000000000;
	while(lft<=rht)
	{
		int mid=(lft+rht)>>1;
		if(judge(mid)){ans=mid;rht=mid-1;}
		else lft=mid+1;
	}
	printf("%d\n",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11617594.html