public class ThreeIncreasing {
public int minEaten(int a, int b, int c) {
int b_change = max(0, b - (c - 1)); // decrease b to c-1
b -= b_change;
int a_change = max(0, a - (b - 1)); // decrease a to b-1
a -= a_change;
if(a <= 0)
return -1;
return a_change + b_change;
}
}
public class SellingFruits {
public int maxDays(int x, int f, int d, int p) {
int low = 0, high = d;
while(low < high) {
int days = (int) (((long) low + high + 1) / 2);
long needMoney = (long) days * x + (long) p * max(0, days - f);
if(needMoney <= d)
low = days;
else
high = days - 1;
}
return low;
}
}
public class MappingABC2 {
public int countStrings(int[] t) {
final int MOD = 1000 * 1000 * 1000 + 7;
int n = t.length;
int MAX_VAL = 0;
for(int i = 0; i < n; ++i) {
MAX_VAL = max(MAX_VAL, t[i]);
--t[i];
}
boolean appears[] = new boolean[MAX_VAL];
for(int i = 0; i < MAX_VAL; ++i)
appears[i] = false;
for(int x : t)
appears[x] = true;
int answer = 0;
for(int a0 = 0; a0 < n; ++a0)
for(int b0 = a0 + 1; b0 < n; ++b0)
for(int c0 = b0 + 1; c0 < n; ++c0) {
boolean a[] = new boolean[MAX_VAL];
boolean b[] = new boolean[MAX_VAL];
boolean c[] = new boolean[MAX_VAL];
for(int x = 0; x < MAX_VAL; ++x)
a[x] = b[x] = c[x] = true;
for(int i = 0; i < a0; ++i)
a[t[i]] = false; // no 'A'
b[t[a0]] = c[t[a0]] = false; // first 'A'
for(int i = a0 + 1; i < b0; ++i)
b[t[i]] = false; // no 'B'
a[t[b0]] = c[t[b0]] = false; // first 'B'
for(int i = b0 + 1; i < c0; ++i)
c[t[i]] = false; // no 'C'
a[t[c0]] = b[t[c0]] = false; // first 'C'
int product = 1;
for(int i = 0; i < MAX_VAL; ++i) if(appears[i]) {
int ways = (a[i]?1:0) + (b[i]?1:0) + (c[i]?1:0);
product = (int) ((long) product * ways % MOD);
}
answer = (answer + product) % MOD;
}
return answer;
}
}