COCI '08 Contest 2 #3 Perket
Vote on problem points
Voting statistics
阅读题解
分数:7
时间限制:0.6s
内存限制:32M
题目类型
“Perket” is a widely known and delicious meal. For perket to be what it is, cooks must carefully choose the ingredients to get the fullest taste possible while keeping the meal traditional.
You have ingredients at your disposal. For each we know its sourness and bitterness . When using multiple ingredients, the total sourness is the product of sourness amounts of all ingredients, while the total bitterness is the sum of bitterness amounts of all ingredients.
As everyone knows, perket is supposed to be neither sour nor bitter; we want to choose the ingredients so that the absolute difference between sourness and bitterness is the smallest.
Also, it is necessary to use at least one ingredient; you can’t serve water as the main course.
Input Specification
The first line contains the integer , the number of ingredients at our disposal. Each of the next lines contains two integers separated by a space, the sourness and bitterness of each ingredient.
The input data will be such that, if we make a meal with all ingredients, both the sourness and bitterness will be less than .
Output Specification
Output the smallest possible difference between sourness and bitterness.
Sample Input 1
1
3 10
Sample Output 1
7
Sample Input 2
2
3 8
5 8
Sample Output 2
1
Sample Input 3
4
1 7
2 6
3 8
4 9
Sample Output 3
1
In the third example, we choose the last three ingredients. The total sourness is then and bitterness is . The difference is .
翻译:
Perket 是一种流行的美食。为了做好 Perket,厨师必须谨慎选择食材,以在保持传统风味的同时尽可能获得最全面的味道。你有 nn 种可支配的配料。对于每一种配料,我们知道它们各自的酸度 ss 和苦度 bb。当我们添加配料时,总的酸度为每一种配料的酸度总乘积;总的苦度为每一种配料的苦度的总和。
众所周知,美食应该做到口感适中,所以我们希望选取配料,以使得酸度和苦度的绝对差最小。
另外,我们必须添加至少一种配料,因为没有任何食物以水为配料的。
输入:
第一行包含整数N,即我们可以使用的成分数量。接下来的每一行都包含两个用空格分隔的整数,即每种成分的酸苦程度。
输入数据将是这样的,如果我们用所有的食材做一顿饭,酸酸和苦味都将小于1000000000。
输出:
输出酸味和苦味之间的最小差异。
import sys
from itertools import combinations
input = sys.stdin.readline
ans = float('inf')
sour = []
bitter = []
N = int(input())
for i in range(N):
s, b = map(int, input().split())
sour.append(s)
bitter.append(b)
temp = [x for x in range(N)]
for i in range(1, N+1):
for c in combinations(temp, i):
sness = 1
bness = 0
for pos in c:
sness *= sour[pos]
bness += bitter[pos]
ans = min(ans, abs(sness - bness))
print (ans)