ãã¨ã¡ãã£ã¨ã§easyã解ãããã ã£ããã©ãæ®å¿µãªããä»åã1åã解ããorz
230ä½, 0.0ç¹, +0/-0 challenge
Volatility: 218->201
250: Coversta
åãã¹ã«\(0\)ãã\(9\)ã®æ°åãæ¸ãããåè§å½¢ã®ç¤é¢ã¨ãæ大ã®é·ãã\(10\)ã®é
å\(x, y\)ãä¸ãããã
ç¤é¢ã®ãã¡\(2\)ã¤ã®ä½ç½®ãæå®ãã
æå®ããä½ç½®ã«é
å\(x, y\)ã®ããããã®è¦ç´ \((x[k], y[k])\)ã足ããããããã®ä½ç½®ã«ãããã¹ãã«ãã¼ããã
é©å½ãª\(2\)ã¤ã®ä½ç½®ãæå®ãã¦ãã«ãã¼ããããã¹ã®æ°å¤ã®åè¨ã®æ大å¤ãæ±ãã(å
±éãã¦ã«ãã¼ãã¦ãããã¹ã¯\(1\)ååããæ°ããªã)
解æ³
ä½ç½®\(a\)ãåç¬ã§æå®ããæã®ã«ãã¼ããåè¨å¤ã\(score_a\)ã¨ãã
\(a, b\)ãå
±éã®ã«ãã¼ãããã¹ãæã¤ä½ç½®ã§æ大å¤ã«ãªããã¢ã§ã\(a, c\)ãã¾ã£ããå
±éã®ã«ãã¼ãããã¹ãæããªãä½ç½®ã§æ大å¤ã«ãªããã¢ã¨èãã
ä½ç½®\(a, b\)ãå
±éã§ã«ãã¼ãããã¹ã®åè¨å¤ã\(score_{a\cap b}\)ã¨ç½®ãã¨ãåè¨ãæ大ã«ãªãå ´åã¯ä»¥ä¸ã®2éã
- ã«ãã¼ãããã¹ãå ±éãã¦ããå ´å: \(score_a + score_b - score_{a\cap b}\)
- ã«ãã¼ãããã¹ãã¾ã£ããå ±éãã¦ããªã: \(score_a + score_c\)
以ä¸ã®ãã¨ããã«ãã¼ãããã¹ãå
±éãã¦ããã¨ãã«æ大ã«ãªãå ´åãèããã¨ã\(score_a + score_b - score_{a\cap b} > score_a + score_c\)
ãã®æ\(score_b > score_c\)ã¨ãªãã®ã§ã\(1\)ç®æãæå®ããææ®ãã®\(1\)ç®æã¯ã\(score_c\)ãã大ãããªã
ã«ãã¼ãããã¹ãã¾ã£ããå
±éãã¦ããªãã¨ãã«æ大ã«ãªãå ´åã¯\(score_c\)ãã®ãã®ãªã®ã§ãçµå±\(score_c\)以ä¸ã®ä½ç½®ã«ã¤ãã¦ã ãã調ã¹ãã°ãã
é
å\(x, y\)ã®é·ãã\(K\)ã¨ãã
éè¤ããå¯è½æ§ããããã¹ã®æ°ãè¦ç©ããã¨ãé
å\(x, y\)ã§ä¸ããããä½ç½®ã®ãã¹ã¦ã®ãã¢ã«ã¤ãã¦èããã°ããã®ã§\(K(K-1)\)åããã(ãã£ã¨å°ããã§ããï¼
åä½ç½®ãåç¬ã§æå®ããæã®ã«ãã¼ããåè¨å¤ãè¨ç®ãã¦ã½ã¼ããã¦ãã
åç¬ã§è¦ãæã®åè¨å¤ãå¤ãé ã«\(K(K-1)+2\)åãè¦ãã°ã\(score_c\)ãå¿
ãå«ã¾ãã¦ãã(\(K=1\)ã®æã®ããã«\(+2\)ãã¦ãããã\(K>1\)ã®ã¨ãã¯\(K(K-1)+1\)ã§ããã¯ã)
ãªã®ã§ä¸ä½\(K(K-1)+2\)åã®åè¨å¤ã«ãªãä½ç½®ã®ããããã®ãã¢ã«ã¤ãã¦æ大å¤ãæ±ããã°æ£ããçããå¾ããã
ç¤é¢ã®ç¸¦å¹ ãæ¨ªå¹ ã\(N, M\)ã¨ããã¨è¨ç®éã¯\(O(NMK + NM \log NM + K^5)\)ããã
\(2\)ã¤ã®ä½ç½®ãåç¬ã§è¦ãæã®åè¨å¤ã®åãç¾å¨ã®æ大å¤ããå°ãªãå ´åã¯æåããã¦ããããã
# -*- coding: utf-8 -*- import math,string,itertools,fractions,heapq,collections,re,array,bisect class Coversta: def place(self, a, x, y): n = len(a) m = len(a[0]) K = len(x) temp = [] for i in xrange(n): for j in xrange(m): cum = 0 for k in xrange(K): p = j + y[k] q = i + x[k] if 0 <= q < n and 0 <= p < m: key = (p, q) cum += int(a[q][p]) temp.append((cum, i, j)) temp.sort(reverse=True) result = 0 for score1, ti, tj in temp[:K * (K - 1) + 2]: i, j = ti, tj visited = set() for k in xrange(K): p = j + y[k] q = i + x[k] if 0 <= q < n and 0 <= p < m: key = (p, q) if key not in visited: visited.add(key) tcum = score1 for score2, i, j in temp[:K * (K - 1) + 2]: if score1 + score2 <= result: break v2 = visited.copy() cum = 0 if ti == i and tj == j: continue for k in xrange(K): p = j + y[k] q = i + x[k] if 0 <= q < n and 0 <= p < m: key = (p, q) if key not in v2: v2.add(key) cum += int(a[q][p]) cum += tcum if result < cum: result = cum return result