CF1728 ERed-Black Pepper
CF1728ERed-Black Pepper
题目描述
Monocarp is going to host a party for his friends. He prepared dishes and is about to serve them. First, he has to add some powdered pepper to each of them — otherwise, the dishes will be pretty tasteless.
The -th dish has two values and — its tastiness with red pepper added or black pepper added, respectively. Monocarp won’t add both peppers to any dish, won’t add any pepper multiple times, and won’t leave any dish without the pepper added.
Before adding the pepper, Monocarp should first purchase the said pepper in some shop. There are shops in his local area. The -th of them has packages of red pepper sufficient for servings and packages of black pepper sufficient for servings.
Monocarp goes to exactly one shop, purchases multiple (possibly, zero) packages of each pepper in such a way that each dish will get the pepper added once, and no pepper is left. More formally, if he purchases red pepper packages and black pepper packages, then and should be non-negative and should be equal to .
For each shop, determine the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it’s impossible to purchase the packages in the said way, print -1.
输入格式
The first line contains a single integer ( ) — the number of dishes.
The -th of the next lines contains two integers and ( ) — the tastiness of the $i $-th dish with red pepper added or black pepper added, respectively.
The next line contains a single integer $m $( ) — the number of shops.
The -th of the next lines contains two integers and ( ) — the number of servings the red and the black pepper packages are sufficient for in the -th shop, respectively.
输出格式
Print integers. For each shop, print the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it’s impossible to purchase the packages so that each dish will get the pepper added once and no pepper is left, print -1.
题目翻译
有n道菜, 对于第i道菜加入一份a佐料有的价值,加入一份b有的价值,每道菜只能加入一份佐料。现在有m个商店,每个商店都按瓶售卖佐料,一个瓶中只能有佐料a或者佐料b,一瓶佐料a有x份,一瓶佐料佐料b有y份,购买的佐料必须完全用完。求分别在每个商店购买佐料能获得价值的最大值,如果不能输出-1。
题目分析
对每个商店一眼就能列出不定方程组,如果有解,有 的通解:,那么可以算出来 的取值在一个范围,而且间隔都是lcm,那么先预处理出来选择i道菜加入佐料a,n-i道菜加入佐料b的最大价值,可以发现,选择i道菜加入佐料a的价值肯定是个单峰函数,而的取值范围也已知,那么可以通过二分求解,或者直接判断在峰两侧的值和取值范围两端的最大值。
代码
1 |
|