B - Alcoholic Editorial by en_translator
The amount of alcohol in the \(i\)-th liquor is \(V_i*P_i/100\). So one can calculate the sum from the first and output the first index that the sum exceeds \(X\).
However, floating-point operations has errors in some environment. For example, the correct answer for the following input is -1
, but some code may output 3
, depending on implementations and environments.
3 13
30 13
35 13
35 13
The easiest way to avoid such issue is to apply appropriate transformation to equations so that only integral operations are required.
This time, we want to check if \(V_1*P_1/100+\ldots >X\), so we can multiply \(100\) to both sides to obtain \(V_1*P_1+\ldots > X*100\), in which all calculations are of integers.
Sample Code (C)
#include<stdio.h>
int main(){
int n,x;
scanf("%d%d",&n,&x);
int sum=0;
for(int i=0;i<n;i++){
int v,p;
scanf("%d%d",&v,&p);
sum+=v*p;
if(sum>x*100){
printf("%d\n",i+1);
return 0;
}
}
puts("-1");
}
Sample Code (Python)
N,X = map(int,input().split())
s = 0
for i in range(N):
v,p = map(int,input().split())
s += v*p
if s > X*100:
print(i+1)
exit()
print(-1)
posted:
last update: