Google Code Jam Round1B
A, B ã® small, large ãæåºã㦠56 ç¹ 411 ä½ã§éé.
å»å¹´ã¯ Round1 ã§æéããã®ã§, ä»å¹´ã¯å°ãé²æ©ããã¨ãããã¨ã ?
A ã¯åé¡æãé·ãã®ã§, å
ã« B ãè¦ã. B ã® small ã®æ¹ãç¹æ°ãä½ã.
B 㯠std::next_permutation 使ã. æå¾ã®é åã ã£ãå ´åã¯, ã½ã¼ããã¦å
é ã®æå㨠0 ã§ãªãæå°ã®æåãå
¥ãæ¿ãã¦, ä¸æåç®ã« 0 ãæ¿å
¥ãã. ç¥ã£ã¦ã¦ããã£ã STL.
A ã¯, åé¡æããäºåæ¨ä½ãåé¡. std::istream ã®æååãåºãã使ãããã®ã ã, ) ã®åã¯ç©ºç½ããªãå ´åãããã¨ã®èª¬ææ¸ã. ) ã®åã«ã¯ç©ºç½ãæ¿å ¥ãã¦æ§æ解æãã¾ãã.
C ã¯, ä¸ã¤ãã¤å¼ãå¢ããã¦ã¯ã¨ãªã®æ°åã¨çãåãããããããããæãã¤ãã. å¾ã§ä½ãã¾ããã, small ã¯éãã¾ããã large ã¯å ¨ç¶ã ãã§ãã.
3 åå²ã® Round1 㧠400çªå°ã ã¨æ¬¡ã® Round2 çªç ´ã¯ããªãå³ãã. ä»å¹´ã¯æ¥½ãã¿ã¾ãã.
ã¢ã«ã´ãªãºã ã¤ã³ãããã¯ã·ã§ã³ ç« æ«åé¡ 15-7 å©çæ大åã¹ã±ã¸ã¥ã¼ã«
1 å°ã®æ©æ¢°ã« n åã®ä»äº a[1], a[2], ..., a[n] ã®ä»äºãããã¾ã. åä»äºã«ã¯å¦çæé t[ j ], å©ç p[ j ], ç´æ d[ j ] ãä¸ãããã¦ãã¾ã. æ©æ¢°ã¯åæã«äºã¤ä»¥ä¸ã®ä»äºããããã¨ã¯ã§ããªãã, éä¸ã§æ¢ãããã¨ãã§ãã¾ãã. ä»äº a[ j ] ãç´æã¾ã§ã«å®äºããã°å©ç d[ j ] ãå¾ããã¾ã. ç´æãè¶ããã¨ãã®ä»äºã«å¯¾ããå©ç㯠0 ã«ãªãã¾ã.
å©çãæ大ã«ãããã¨ã¹ã±ã¸ã¥ã¼ã«ã, ãã®æ大ã®å©çãä¸ããããã°ã©ã ãèãã¾ã.
ããã§ã¯, 1 <= n <= 1000, 1 <= max(d[ j ]) <= 1000 ã¨ãã¦ããã¾ã.
ç´æãã£ã¡ãã§çµããããå¿ è¦ã¯ããã¾ãããã, ç´æã®æ©ãé ã«, ããã¦ä»äºãç´æãè¶ããªãããã«ä»äºã並ã¹ã¦ãã£ãã¨ãã®æ大ã®å©çãæ±ãããã¨ãèãã¾ã. ç´æã®æ©ãé ã«ä»äºãã½ã¼ããã¦ããã¾ã. ããã§ã¯ a[1], a[2], ..., a[n] ããã§ã«ä¸¦ã³æ¿ãããã¦ããã¨èãããã¨ã«ãã¾ã.
æ大ã®ç´æã dmax, æå» t ã®æç¹ã§, ä»äº a[ j ] ã¾ã§èæ
®ããã¨ãã®æ大ã®å©çã profit[ t ][ j ] ã¨ãã¾ã. profit[ t ][ j ] ã¯, a[j] ã®ä»äºãããã¨ãã®å©çã, ããªãã£ãã¨ãã®å©ç (profit[ t-1 ][ j ] 㨠profit[ t ][ j-1]) ã®ä¸ã§ã®æ大å¤ã«ãªãã¾ã.
ä»äº a[ j ] ãããã«ã¯, t[ j ] <= t ã§ãªãã¨ããã¾ãã.
ã¾ã, ç´æãè¶ãããå©ç 0 ã§ããã, d[ j ] >= t ã®ã¨ãã ã, ä»äºããã価å¤ãããã¾ã.
profit[t][j] = max( p[j]+profit[t-t[j]][j-1] if t[j] <=t and d[j] >= t, profit[t-1][j], profit[t][j-1] ) profit[0][k] = 0 (0 <= k <= n) profit[k][0] = 0 (0 <= k <= dmax)
ã½ã¼ã㧠O(n*logn) ããã, ä¸è¨ã®æ¼¸åå¼ãåçè¨ç»æ³ã§ O(n*dmax) ã§è¨ç®ãã¾ããã, å ¨ä½ã§ã¯ O(n*dmax) ã®ã¢ã«ã´ãªãºã ã¨ãªãã¾ã.
#include <cstring> #include <iostream> #include <algorithm> #include <vector> struct Job { int i,t,p,d; Job(int i, int t, int p, int d):i(i), t(t), p(p), d(d) {} bool operator<(const Job& job) const { return d < job.d; } }; int main() { int C; std::cin >> C; for(int c = 1; c <= C; ++c) { int n; std::cin >> n; std::vector<Job> jobs(n+1, Job(0,0,0,0)); for(int i = 1; i <= n; ++i) { int t,p,d; std::cin >> t >> p >> d; jobs[i] = Job(i,t,p,d); } std::sort(jobs.begin()+1, jobs.end()); int profit[1001][1001]; memset(profit, 0, sizeof(profit)); int tmax = jobs.back().d; for(int t = 1; t <= tmax; ++t) { for(int i = 1; i <= n; ++i) { int p = 0; if(jobs[i].d >= t && jobs[i].t <= t) { p = jobs[i].p+profit[t-jobs[i].t][i-1]; } p = std::max(p, profit[t-1][i]); p = std::max(p, profit[t][i-1]); profit[t][i] = p; } } std::cout << "Case #" << c << ": " << profit[tmax][n] << std::endl; } }
Google Code Jam Qualification Round 2009: äºé¸ä¼
å»å¹´ã«å¼ãç¶ãä»å¹´ãåå ãã¾ãã. C ã® large ãè½ã¨ããã®ã§, 76 ç¹.
A - Alien Language
æ£è¦è¡¨ç¾ã§ä¸æã§è§£ããããã§ãã, ãããªãã¨ã«ã¯æ°ã¥ããªãç§ã¯, å ¨ãã¿ã¼ã³ãæ¢ç´¢ãã¦æ±ããæ¹æ³ãã¨ãã¾ãã.
åç´ã«å ¨æ¢ç´¢ãã㨠small ã§ããéããªãã®ã§, è¾æ¸ã®ã¯ã¼ãã®å ¨æ¥é èªãè¾æ¸ã«è¿½å ãã¦, æåãããå°æå ã®ç´°å·¥ã§, large ã 49 ç§ã§éãã¾ãã.
å ¨æ¢ç´¢ãªã©ããªãã¦ã, ãã¿ã¼ã³ãç°¡åãªã®ã§, è¾æ¸ã®ã¯ã¼ãããããã«å¯¾ãã¦, ä¸æåã¥ã¤ç §åããã°ããã ãã ã£ãã®ã§ãã.
#include <iostream> #include <vector> int main() { size_t L, D, N; std::cin >> L >> D >> N; std::vector<std::string> dic(D, std::string()); for(size_t i = 0; i < D; ++i) { std::cin >> dic[i]; } for(size_t i = 1; i <= N; ++i) { std::string pattern; std::cin >> pattern; std::vector<std::string> pv; for(size_t index = 0; index < pattern.size();) { if('(' == pattern[index]) { std::string::size_type endparen = pattern.find(")", index); pv.push_back(pattern.substr(index+1, endparen-index-1)); index = endparen+1; } else { pv.push_back(pattern.substr(index,1)); ++index; } } int total = 0; for(size_t d = 0; d < D; ++d) { const std::string& word = dic[d]; bool found = true; for(size_t i = 0; i < word.size(); ++i) { if(pv[i].find(word[i]) == std::string::npos) { found = false; break; } } if(found) { ++total; } } std::cout << "Case #" << i << ": " << total << std::endl; } }
ã¢ã«ãã¡ãããå°æåã«éå®ããã¦ããã®ã§, 'a' ããã¿ã¼ã³ã«ãããªã, 1 ãããç®ãã»ãã, 'b' ããããªã, 2 ãããç®ãã»ããã¨è¨ã風ã«ããããã¿ã¼ã³ã«ãã¦, ç §åããããæ¼ç®ã«ããã¨éã解ãã¾ã. i çªãã®æåãã¿ã¼ã³å p[i] ãããããã¿ã¼ã³ã«ããã¨
int bitpattern[i] = 0; for(size_t k = 0; k < p[i].size(); ++k) { bitpattern[i] |= 1 << (p[i][k]-'a'); }
è¾æ¸å ã®ã¯ã¼ã W ã¨ç §åããã«ã¯, W[i]&bitpattern[i] ã 0 ãªãç §å失æ, 1 ãªã次ã®æåã®ç §åã«é²ã¿ã¾ã.
#include <iostream> #include <vector> int main() { size_t L, D, N; std::cin >> L >> D >> N; std::vector<std::string> dic(D, std::string()); for(size_t i = 0; i < D; ++i) { std::cin >> dic[i]; } for(size_t i = 1; i <= N; ++i) { std::string pattern; std::cin >> pattern; std::vector<unsigned int> pv; for(size_t index = 0; index < pattern.size();) { if('(' == pattern[index]) { std::string::size_type endparen = pattern.find(")", index); std::string p = pattern.substr(index+1, endparen-index-1); unsigned int x = 0; for(size_t k = 0; k < p.size(); ++k) { x |= 1 << (p[k]-'a'); } pv.push_back(x); index = endparen+1; } else { pv.push_back(1 << (pattern[index]-'a')); ++index; } } int total = 0; for(size_t d = 0; d < D; ++d) { const std::string& word = dic[d]; bool found = true; for(size_t i = 0; i < word.size(); ++i) { if(!(pv[i] & (1 << (word[i]-'a')))) { found = false; break; } } if(found) { ++total; } } std::cout << "Case #" << i << ": " << total << std::endl; } }
B - Watersheds
ç§ã«ã¨ã£ã¦ã¯ãããä¸çªç°¡åã§ãã. åé¡èªãã®ãé¢åã§ããã. 'drainage basins' ã£ã¦ãªããã, ã£ã¦æãã§.
å·¦ä¸ã®ãã¹ããé ã«, ãã¹ã¦ã®ãã¹ãèãã¦, æµããæ¹åã決ãã¦ããã¾ã.
次ã«, ãã§ã«ã©ãã«ä»ãããã¦ãããã¹ã¯ã¹ãããããªãã, å·¦ä¸ã®ãã¹ããé ã«, æµããæ¹åããã©ã£ã¦, sink ãæ¢ãã¾ã. ããã¦, sink ããæµããéé ã«è¾¿ã£ã¦, ã©ãã«ä»ããã¾ã. æå¾ã«ã©ãã«ãæ´æ°ãã¾ã.
C - Welcome to Code Jam
æåºãããã®ã¯, å帰ã§è§£ãããã®ã§, æç« ã®ä½ç½®ã¨ "welcome to .." ã®ä½ç½®ã®ãã¢ããã¼ã«ãã¦, ãã®æç¹ã§ã®åæ°ãã¡ã¢ãã¾ãã. æ®å¿µãªãã large ã失æãã¦ãã¾ãã.
ããã§ã¯è¡¨ãç¨ãã¦è§£ãã¦ã¿ã¾ã. T ãæç« , W ã "welcome to code jam" ã¨ãã¾ã.
t ã T ã®é·ã, w ã W ã®é·ãããã¾ã.
C[ i ][ j ] ã W[ i ], T[ j ] ã¾ã§è¦ãã¨ãã®å ´åã®æ°ã¨ãã¾ã.
W[ i ] == T[ j ] ã®æ, C[ i-1 ][ j-1 ] ã ã, å ´åã®æ°ãå¢ãããã¨ã«ãªãã¾ã.
W[ i ] != T[ j ] ã®æã¯, å ´åã®æ°ã¯å¢ããªãã®ã§, C[ i ][ j-1 ] ã¾ã§ã®å ´åã®æ°ã¨åãã«ãªãã¾ã. ããã漸åå¼ã«ããã¨,
C[i][j] = C[i][j-1]+C[i-1][j-1] if W[i] == T[j] C[i][j-1] if W[i] != T[j]
i = 0 ã®ã¨ãã« W[ i ] == T[ j ] ã¨ãªã£ãã, å ´åã®æ°ã 1 å¢ããã®ã§,
C[0][j] = 1, 0 <= j <= t C[i][0] = 0, 0 <= i <= w
ã¨ãã¦ããã¾ã. ããã§ããã°ã©ã ãç°¡åã«ãªãã¾ã.
ãã¨ã¯, O(W*T) ã®æéãããã¦, C[ w ][ t ] ãè¨ç®ãã¾ã. C[ w ][ t ] ãçãã§ã.
#include <cstring> #include <iostream> #include <algorithm> #include <iomanip> int main() { const std::string W = "welcome to code jam"; int N; std::cin >> N; std::string line; getline(std::cin, line); for(int n = 1; n <= N; ++n) { std::string T; getline(std::cin, T); int C[20][501]; memset(C, 0, sizeof(C)); std::fill(&C[0][0], &C[0][501], 1); for(size_t i = 1; i <= W.size(); ++i) { for(size_t j = 1; j <= T.size(); ++j) { C[i][j] = C[i][j-1]; if(W[i-1] == T[j-1]) { C[i][j] += C[i-1][j-1]; } C[i][j] %= 10000; } } std::cout << "Case #" << n << ": " << std::setfill('0') << std::setw(4) << C[W.size()][T.size()] << std::endl; } }
ãããæ¬çªã«ã§ããã°ããã®ã§ãã. ç ãã¨ãæ·±å¤ã¨ãææ¥æ©ãã¨ãããããè¨ã訳ã¯ã§ãããã§ãã.
ã¢ã«ã´ãªãºã ã¤ã³ãããã¯ã·ã§ã³ ç« æ«åé¡ 15-2 ããªã³ã¿ã«ããæµæ¸
å
¥åæç« ã¯æåæ°ã l1, l2, ..., ln ã® n åã®åèªã®åã§ã.
ãã®æç« ã 1 è¡æ大 M æåã®è¡ã«å°åãããã¨ãã¾ã.
åãè¡ã®åèªã¨åèªã®éã«ã¯ç©ºç½ãä¸ã¤å
¥ãã¾ã.
æçµè¡ãé¤ã, åè¡ã®è¡ç«¯ã®ç©ºç½ã®ä¸ä¹åãæå°ã«ããããã«å°åããã¢ã«ã´ãªãºã ãèãã,
ã¨ããåé¡ã§ã.
åé¡æã«ãããããã«åçè¨ç»æ³ã«ã¦è§£ããã¨ãèãã¾ã.
ããã§ã¯è¡ç«¯ã®ç©ºç½ã®ä¸ä¹åã®æå°å¤ãåºåããããã°ã©ã ãä½æãã¾ãã.
S[i,j] ãåèª i ããåèª j ã使ã£ãã¨ãã®è¡ç«¯ã®ç©ºç½ã®ä¸ä¹åã§ããã¨ãã¾ã.
æ±ãããã®ã¯, S[1,n] ã«ãªãã¾ã. åèª i ãã j ã®é·ãã®åã sumw(i,j)ã¨ããã¨,
S[i,j] ã¯, M-j+i-sumw(i,j)ã®ä¸ä¹(1), S[i,k]+S[k+1,j], i <= k <= j-1 ã®ãã¡ã®æå°å¤ã¨ãªãã¾ã.
(1) ã§ã¯, åèª i ãã j ãä¸è¡ã«åã¾ããªãå ´åã¯, è² ã®å¤ã«ãªãã®ã§ãã®å ´åã¯,
ãã®å¤ãæå°å¤ã¨ãããã¨ã¯ã§ãã¾ããã®ã§ç¡è¦ãã¾ã. ã¾ã, è² ã§ã¯ãªãå ´åã§, j == n ã®å ´å,
ããã¯æçµè¡ãªã®ã§ 0 ã¨ãªã, èªåçã«æå°å¤ã«ãªãã¾ã.
ãã®è¨ç®ã¯, ä¸ã®ããã°ã©ã ã® cost é¢æ°ã«ã¦è¡ã£ã¦ãã¾ã. åèª i ãã j ãåã¾ããªãå ´åã¯
ååã«å¤§ããªå¤ãè¿ãã¦ããã°ããã®ã§, INT_MAX ãè¿ãã¦ãã¾ã
S[i,i] 1 <= i < n ã«ã¤ãã¦ã¯, (1) ã®å¼ã«ã¦è¨ç®ã§ãã¾ã.S[n,n] ã¯æçµè¡ãªã®ã§ 0 ã«ãªãã¾ã.
for ã® 3 éã«ã¼ãã§ããããã®æ·»åã¯ããã ã n åã®å¤ãã¨ãã®ã§å®è¡æé㯠O(n^3) ã«ãªãã¾ã.
#include <limits.h> #include <cstring> #include <iostream> #include <algorithm> #include <numeric> #include <vector> int S[1001][1001]; int M; int n; int cost(const std::vector<int>& wlv, int first, int last) { int c = M-last+first-std::accumulate(wlv.begin()+first, wlv.begin()+last+1, 0); if(c < 0) { return INT_MAX; } else if(last == n) { return 0; } else { return c*c*c; } } int main() { int C; std::cin >> C; for(int c = 1; c <= C; ++c) { std::cin >> M; std::cin >> n; std::vector<int> wlv(n+1, 0); for(int i = 1; i <= n; ++i) { std::cin >> wlv[i]; } memset(S, 0, sizeof(S)); for(int i = 1; i < n; ++i) { S[i][i] = cost(wlv, i, i); } for(int l = 2; l <= n; ++l) { for(int i = 1; i <= n-l+1; ++i) { int j = i+l-1; int c = cost(wlv, i, j); if(c > 0) { for(int k = i; k <= j-1; ++k) { c = std::min(c, S[i][k]+S[k+1][j]); } } S[i][j] = c; } } std::cout << "Case #" << c << ": " << S[1][n] << std::endl; } }
ãµã³ãã«å ¥å
2 10 10 3 5 9 2 3 2 2 1 4 5 6 2 3 3
ãµã³ãã«åºå
Case #1: 4 Case #2: 27
ãã®ããã°
ãªããã¤ãªããã®ããã¤ãé ã. Twitter ãå¿«é©ãããªã®ã.
EVONY ã® AD
ãªãã¨ããªããã, ãã
Alien Numbers ã解ãã¦ã¿ã
http://code.google.com/codejam/contest/dashboard?c=agdjb2RlamFtcg4LEghjb250ZXN0cxh5DA#s=p0
Alien Numbers ã解ãã¦ã¿ã.
æ¦ç¥ã¯, Alien Number ã 10 é²ã«ãã¦ãã, ã¿ã¼ã²ããã®åºæ°ã«ç´ã.
import sys C=int(sys.stdin.readline().strip()) for c in range(C): line=sys.stdin.readline().strip() alienNumber, source, target=line.split(" ") sbase=len(source) tbase=len(target) decnum=0 for i in range(len(alienNumber)): decnum*=sbase decnum+=source.find(alienNumber[i]) result=[] while decnum > 0: result.append(target[decnum%tbase]) decnum/=tbase result.reverse() print "Case #%d: %s"%(c+1, "".join(result))