åé¡è¨å®
\(n\)åã®ä¼ç¤¾ããããã«ãã¨ã³ã¸ãã¢ã®äººæ°\(q\)ã¨ä¾¡æ ¼\(r\)ãä¸ãããã
ä¼ç¤¾ãããã¤ãé¸ãã§å¥ç´ããæã«ãã¨ã³ã¸ãã¢ã®äººæ°ã®åè¨ãããå¤\(m\)以ä¸ã«ãªãã¨ãã®æå°ã®ã³ã¹ã(ä¾¡æ ¼ã®åè¨)ãæ±ãã
ãã ãããããã®ä¼ç¤¾ã®äººæ°ã®ä¸é¨ã ããéããã¨ã¯ã§ãããä¼ç¤¾ãã¨ã«ã¾ã¨ãã¦å¥ç´ãããã¨ããã§ããªã
解æ³1 - åçè¨ç»æ³
ã©ãã¿ã¦ããããã¶ãã¯åé¡ã ï¼ã¨ãããã¨ã§åçè¨ç»æ³ãæ¸ãã¦ã¿ãã¨ãããã¹ãã±ã¼ã¹7ã§3.5ç§ãããããã£ã¦ãã¾ãã¾ãã
è¨ç®éã¯\(O(mn)\)ããã
m = input() n = input() qr = [map(int, raw_input().split()) for i in xrange(n)] MAX = n * 10000 + 20000 MAX2 = n * 10000 dp = [5000000 * 100] * (MAX + 1) dp[0] = 0 qr.sort(key=lambda x: float(x[1]) / x[0]) result = 5000000 * 100 candidate = set([0]) for i in xrange(n): new = set() next = dp[:] for j in candidate: t = dp[j] + qr[i][1] k = j + qr[i][0] if next[k] > t: next[k] = t if k >= m and result > t: result = t if k <= MAX2 and next[k] < result: new.add(k) candidate |= new dp = next print result
解æ³2 - æ·±ãåªå æ¢ç´¢ + æåã
Pythonã§æéã®ã³ã¼ãã¯0.01ç§ã¨ã®ãã¨ãªã®ã§ãâã®ã³ã¼ããåèã«ãã¤ã¤æ·±ãåªå
æ¢ç´¢ã¨æç©ãã®ã³ã¼ããæ¸ãã¾ãã
æ®éã«æ·±ãåªå
æ¢ç´¢ããã¨é常ã«æéãããããã©ãéä¸ã§å¿
è¦ãªãæ¢ç´¢ãæã¡åããã¨ã§é«éã«è¨ç®ã§ãã
ãã£ã¡ã®æ¹æ³ã¯ç°¡åã«0.01ç§ãéæã§ãã¾ã
æåãã§ã©ããããè¨ç®éãæ¸ãã®ãè¦ç©ãããé£ããããææªã®å ´åã¯ãã£ã¡ã®ã»ããåçè¨ç»æ³ãããéãããªæ°ãããªãã§ããªãã§ããâ¦â¦
ãã£ã¡ã®æ¹æ³ã§éè¦ãªã®ã¯æåãã«ä½¿ãããã®ã³ã¹ãã®ä¸éãæ±ãããã¨ã§ã
ä¸äººãããã®å価ãå°ããé ã«ä¼ç¤¾ãã½ã¼ããã¦ãããã¨ã§ãé çªã«è¶³ãã¦ããã°å¿
è¦ãªäººæ°åã®ã³ã¹ãã®ä¸éãè¨ç®ã§ãã¾ãã
ãã ã人æ°ãä¸åº¦ã«ãªããªãå ´åãããã®ã§ããã®å ´åã¯å端ãªäººæ°åã®ä¾¡æ ¼ã足ãã¾ã
ãã¨ãã°ãæ®ãå¿
è¦ãªäººæ°ã150人ã®æã100人ã§ä¾¡æ ¼1000ã¨100人ã§ä¾¡æ ¼500ã®ä¼ç¤¾ããããªããå価ã®å®ãä¾¡æ ¼500ã®æ¹ãã100人ãä¾¡æ ¼1000ã®æ¹ãã50人åã§\(500+1000 \times \frac{50}{100}=1000\)ã¨ãã¦è¨ç®ãã¾ã
端æ°ãå«ãã¦å¹çã®ããä¼ç¤¾ããé ã«ã³ã¹ããè¨ç®ãã¦ããã®ã§ãããã§è¨ç®ããåè¨ã®ã³ã¹ãã¯æçµçã«æ±ããã¹ãã³ã¹ãã¨çãããããããããå°ãããªãã¾ã(ä¸é)
ããã«ããè¨ç®éä¸ã§ãç¾å¨ã¾ã§ã®æå°å¤ã¨ã³ã¹ãã®ä¸éãæ¯è¼ãã¦ãä¸éãæå°å¤ããã大ããå ´åã«ã¯æå°å¤ãä¸åãå¯è½æ§ããªãã®ã§æ¢ç´¢ãæã¡åããã¨ãã§ãã¾ã
ã³ã¹ãã®ä¸éã®è¨ç®ã¯ä¸ã®ã³ã¼ãã§ã¯lower_boundé¢æ°ã§è¡ã£ã¦ãã¾ã
ã¡ã¢åããã¦ããã®ã§å¤å°éããªã£ã¦ããã¨æãã¾ã(ä½ãããªãã§ã0.01ç§ã§ããã
m = input() n = input() company = [] for i in xrange(n): q, r = map(int, raw_input().split()) company.append((float(r) / q, q, r)) company.sort() best = [5000000 * 50] class memoize(dict): def __init__(self, func): self.func = func def __call__(self, *args): return self[args] def __missing__(self, key): result = self[key] = self.func(*key) return result @memoize def lower_bound(i, rest): unit, num, price = company[i] cost = price if rest >= num else rest * unit rest -= num if rest <= 0: return cost elif i + 1 >= n: return 100000000 else: return cost + lower_bound(i + 1, rest) @memoize def dfs(i, rest, cost): rest_new = rest - company[i][1] cost_new = cost + company[i][2] if rest_new <= 0: if best[0] > cost_new: best[0] = cost_new elif i + 1 < n and best[0] >= cost + lower_bound(i + 1, rest_new): dfs(i + 1, rest_new, cost_new) if i + 1 < n and best[0] >= cost + lower_bound(i + 1, rest): dfs(i + 1, rest, cost) dfs(0, m, 0) print best[0]