using
System;
using
System.Collections.Generic;
using
System.Linq;
class
Program
{
static
int
BinarySearch(
int
x, List<List<
int
>> product)
{
int
l = 0, h = product.Count - 1;
while
(l < h)
{
int
m = (l + h + 1) / 2;
if
(product[m][0] == x)
{
if
(m == product.Count - 1 || product[m][0] != product[m + 1][0])
return
m;
else
l = m;
}
else
if
(product[m][0] < x)
l = m;
else
h = m - 1;
}
return
l;
}
static
List<
int
> MaximumProfitProduct(List<List<
int
>> product, List<
int
> budget)
{
int
n = product.Count;
product = product.OrderBy(p => p[0]).ToList();
for
(
int
i = 1; i < n; i++)
product[i][1] = Math.Max(product[i - 1][1], product[i][1]);
List<
int
> ans =
new
List<
int
>();
for
(
int
i = 0; i < budget.Count; i++)
{
if
(budget[i] < product[0][0])
ans.Add(0);
else
if
(budget[i] >= product[n - 1][0])
ans.Add(product[n - 1][1]);
else
ans.Add(product[BinarySearch(budget[i], product)][1]);
}
return
ans;
}
static
void
Main()
{
List<List<
int
>> product =
new
List<List<
int
>> {
new
List<
int
> { 2, 3 },
new
List<
int
> { 3, 6 },
new
List<
int
> { 4, 1 },
new
List<
int
> { 7, 12 },
new
List<
int
> { 4, 7 }
};
List<
int
> budget =
new
List<
int
> { 2, 3, 4, 6, 7, 8 };
List<
int
> answer = MaximumProfitProduct(product, budget);
foreach
(
var
i
in
answer)
Console.Write(i +
" "
);
Console.ReadKey();
}
}