from random import gauss, random, randrange try: LOCAL except NameError: LOCAL = 0 if LOCAL: import optuna DEBUG = 1 print("LOOP ?") LOOP = int(input()) else: DEBUG = 0 LOOP = 1 def receive(): global P global R global money global MI, MA global sell_count if DEBUG: sell_count = [min(R[i], int(R[i] ** 0.5 * 1.05 ** P[i] * D[i] * (0.75 + random() * 0.5))) for i in range(N)] money += 1000 * sum(sell_count) else: money = int(input()) sell_count = [int(a) for a in input().split()] for i in range(N): if R[i]: ma = (sell_count[i] + 1) / (R[i] ** 0.5 * 0.75 * 1.05 ** P[i]) mi = sell_count[i] / (R[i] ** 0.5 * 1.25 * 1.05 ** P[i]) MI[i] = max(mi, MI[i]) if sell_count[i] < R[i]: MA[i] = min(ma, MA[i]) for i in range(N): if R[i]: if sell_count[i] * 10 >= 3 * R[i]: P[i] = min(P[i] + 1, 60) elif sell_count[i] * 10 < R[i]: P[i] = max(P[i] - 1, -60) if DEBUG: for i in range(N): R[i] -= sell_count[i] else: for i, a in enumerate(input().split()): P[i] = int(a) for i, a in enumerate(input().split()): R[i] = int(a) for i in range(N): total_sell_count[i] += sell_count[i] def adj(n): if n < 4: return 3 if n % 10 < 3: return n // 10 * 10 if n % 10 < 6: return n // 10 * 10 + 3 return n // 10 * 10 + 6 def Order(f1 = 1.0, f2 = 1.0, f3 = 1.0, f4 = 1.0): global P global R global money global best_allocation_flg target = 10 buy_count = [0] * N best_allocation = [((MA[i] + MI[i]) / 2 * 1.05 ** P[i]) ** 2 for i in range(N)] su = sum(best_allocation) for i in range(N): best_allocation[i] *= 10 ** 8 best_allocation[i] = adj(int(best_allocation[i])) best_allocation_flg = [0] * N for i in range(N): if t == 0: target = 3 elif t < 5: target = adj(int((10 / 3 * 1.05 ** P[i] * (MA[i] * 0.0 + MI[i] * 1.0)) ** 2 * f1)) elif t < 20: # target = adj(int(100 / 9 * 1.05 ** P[i] * (MA[i] * 0.0 + MI[i] * 1.0) * f1)) target = adj(int((10 / 3 * 1.05 ** P[i] * (MA[i] * 0.5 + MI[i] * 0.5)) ** 2 * f2)) elif t < 30 and P[i] < 50: # target = adj(int(100 / 4 * 1.05 ** P[i] * (MA[i] + MI[i]) / 2)) target = adj(int((10 / 3 * 1.05 ** P[i] * (MA[i] * 0.5 + MI[i] * 0.5)) ** 2 * f3)) elif t < 40 and P[i] < 50: # target = adj(int(100 / 1 * 1.05 ** P[i] * (MA[i] + MI[i]) / 2)) target = adj(int((10 / 3 * 1.05 ** P[i] * (MA[i] * 0.5 + MI[i] * 0.5)) ** 2 * f4)) elif t < 100: target = best_allocation[i] best_allocation_flg[i] = 1 else: assert 0 buy_count[i] = max(0, target - R[i]) if 1: su_best = sum([a for a, f in zip(best_allocation, best_allocation_flg) if f]) su_R = sum([r for r, f in zip(R, best_allocation_flg) if f]) used = sum([max(t - r, 0) for t, r, f in zip(buy_count, R, best_allocation_flg) if not f]) avail = max(0, money // 500 - used + su_R) for i in range(N): if best_allocation_flg[i]: buy_count[i] = adj(int(best_allocation[i] / su_best * avail - R[i])) # buy_count[i] = adj(int(best_allocation[i] / su_best * avail)) while 1: r = sum(buy_count) * 500 / money if r <= 1: break for i in range(N): buy_count[i] = adj(int(buy_count[i] / r)) money -= sum(buy_count) * 500 if money < 0: while 1: pass assert money >= 0 for i in range(N): R[i] += buy_count[i] if not DEBUG: print(1, *buy_count) def Advertise(x): global money money -= 500000 << x - 1 if money < 0: while 1: pass assert money >= 0 for i in range(N): P[i] = min(P[i] + x, 60) if not DEBUG: print(2, x) def main_loop(adv_l3 = 20, adv_r3 = 40, adv_m = 2500000, f1 = 1.0, f2 = 1.0, f3 = 1.0, f4 = 1.0): global T, N, money global MI, MA, P, R, t, D, total_sell_count, LOOP global total_total ret = 0 for _ in range(1000): ret += main(adv_l3, adv_r3, adv_m, f1, f2, f3, f4) return ret / 1000 def main(adv_l3 = 20, adv_r3 = 40, adv_m = 2500000, f1 = 1.0, f2 = 1.0, f3 = 1.0, f4 = 1.0): global T, N, money global MI, MA, P, R, t, D, total_sell_count, LOOP global total_total if DEBUG: T, N, money = 52, 10, 2 * 10 ** 6 D = [random() + 0.5 for _ in range(N)] # D = [random() / 2 + 1.0 for _ in range(N)] else: T, N, money = map(int, input().split()) D = [random() + 0.5 for _ in range(N)] total_sell_count = [0] * N sell_count = [0] * N best_allocation_flg = [0] * N MA = [1.5] * N MI = [0.5] * N P = [0] * N R = [0] * N for t in range(T): if (adv_l3 < t < adv_r3 and money >= adv_m) and sorted(P)[-1] < 58: Advertise(3) elif (0 < t < 20 and money >= 1100000) and sorted(P)[-1] < 59: Advertise(2) elif (0 < t < 35 and money >= 1200000) and sorted(P)[-1] < 59: Advertise(2) elif t < 4 and money >= 6000000: Advertise(4) elif t < 4 and money >= 3000000: Advertise(3) else: Order(f1, f2, f3, f4) if DEBUG: if LOOP == 1: print("-" * 20) print("t =", t) print("Money =", money) receive() if DEBUG: if LOOP == 1: print("Money =", money) print("P =", P) print("R =", R) print("sell_count =", sell_count) print("MI =", ["{:.2f}".format(a) for a in MI]) print("MA =", ["{:.2f}".format(a) for a in MA]) print("D =", ["{:.2f}".format(a) for a in D]) print("Flg =", best_allocation_flg) print("Total =", sum(total_sell_count), total_sell_count) if LOCAL: total_total += sum(total_sell_count) if LOCAL and 4 >= LOOP > 1: print("loop =", loop, total_sell_count, sum(total_sell_count), round(total_total / (loop + 1))) return sum(total_sell_count) def objective(trial): adv_l3 = trial.suggest_float('adv_l3', 10, 30) adv_r3 = trial.suggest_float('adv_r3', 30, 50) adv_m = trial.suggest_float('adv_m', 220 * 10 ** 4, 300 * 10 ** 4) f1 = trial.suggest_float('f1', 0.6, 0.8) f2 = trial.suggest_float('f2', 0.4, 0.6) f3 = trial.suggest_float('f3', 0.2, 0.6) f4 = trial.suggest_float('f4', 0.1, 0.6) score = main_loop(adv_l3, adv_r3, adv_m, f1, f2, f3, f4) return -score OP = 0 if OP: T, N, money = None, None, None MI, MA, D = None, None, None P, R, t, total_sell_count = None, None, None, None study = optuna.create_study() study.optimize(objective, n_trials=50) print("Best Params =", study.best_params) print("Best Score =", study.best_value) else: total_total = 0 _t = 0 for loop in range(LOOP): _t += main(19, 45, 3000000, 0.670, 0.516, 0.561, 0.473) if LOCAL: print("LOOP =", LOOP, round(total_total / LOOP), round(_t / LOOP)) ''' Best Params = {'adv_l3': 19.37253008268183, 'adv_r3': 45.317372195983815, 'adv_m': 2996720.1222027894, 'f1': 0.6705334828220689, 'f2': 0.5165434545936766, 'f3': 0.5609350182215814, 'f4': 0.47319152557907895} Best Score = -176750.828 '''