UVALive 6611 Alice's Print Service(二分)

本文介绍了一种利用二分查找解决特定问题的算法实现方法,通过预处理建立辅助数据结构并使用二分查找来快速响应查询请求。适用于需要高效处理大量数据及查询的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:二分二分就好了


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>

#define INF 0x3f3f3f3f
#define esp 1e-9
typedef long long LL;
using namespace std;
const int N=1e5+10;
int n,m,T,a[N],b[N];
struct node
{
	LL v;
	int i;
	bool operator < (const node&a)const
	{
		return v<a.v;
	}
};
node s[N];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++) scanf("%d%d",a+i,b+i);
		a[n]=2*1e9;
		b[n]=2*1e9;
		int k=0;
		s[0].v=(LL)a[n]*b[n];
		s[0].i=n;
		for(int i=n-1;i>=0;i--)
		{
			if((LL)a[i]*b[i]<s[k].v)
			{
				s[++k].v=(LL)a[i]*b[i];
				s[k].i=i;
			}
		}
		k++;
		sort(s,s+k);
		while(m--)
		{
			int x;
			scanf("%d",&x);
			int y=upper_bound(a,a+n+1,x)-a-1;
			LL ans=(LL)x*b[y];
			int z=upper_bound(s,s+k,node{ans,0})-s-1;
			if(s[z].i>y) ans=s[z].v;
			printf("%lld\n",ans);
		}
	}
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值