最近看到牛課網美團一個編程競賽,想着作作看,結果一寫就是兩天。。真是寫不動了啊。話很少說,下面開始個人題解。node
題目大體仍是比較考察思惟和代碼能力(由於本身代碼能力較弱,纔會以爲比較考察代碼能力吧= =!),難度由簡到難變化也比較適中,有簽到題、有算法實現,固然也有稍稍一點代碼量的題。感謝美團點評,提供一套合適的題目~ios
第一行一個整數n(1 ≤ n ≤ 1000),表示第一段音頻的長度。
第二行n個整數表示第一段音頻的音高(0 ≤ 音高 ≤ 1000)。
第三行一個整數m(1 ≤ n ≤ m ≤ 1000),表示第二段音頻的長度。
第四行m個整數表示第二段音頻的音高(0 ≤ 音高 ≤ 1000)。
輸出difference的最小值
2 1 2 4 3 1 2 4
0
題解:算法
簽到題,n*m<1e6,直接暴力枚舉第二個數組的起點便可,複雜度O(n*m)。編程
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <set> 5 #include <cstring> 6 7 using namespace std; 8 9 const int kMaxN = 1000 + 5; 10 int n, m, a1[kMaxN], a2[kMaxN]; 11 12 int main(){ 13 //freopen("in.txt","r",stdin); 14 cin >> n; 15 for (int i = 1; i <= n; i ++) 16 cin >> a1[i]; 17 cin >> m; 18 for (int i = 1; i <= m; i ++) 19 cin >> a2[i]; 20 int ans = 1e9; 21 for (int i = 0; i <= m - n; i ++) { 22 int sum = 0; 23 for (int j = 1; j <= n; j ++) 24 sum += (a1[j] - a2[i + j]) * (a1[j] - a2[i + j]); 25 ans = min(ans, sum); 26 } 27 cout << ans << endl; 28 return 0; 29 }
錦標賽數組
第一行一個整數 n (1≤n≤ 2^20),表示參加比賽的總人數。 接下來 n 個數字(數字範圍:-1000000…1000000),表示每一個參賽者的積分。 小美是第一個參賽者。
小美最多參賽的輪次。
4 4 1 2 3
2
題解:函數
這個也比較好容易想到,很容易能夠想到當小美被淘汰時,全部其餘人的分數都是嚴格大於小美的分數的。因此直接將小美安排和全部與她分數低的人比賽便可,由於分數高的必定贏,所以能夠認爲她淘汰的人和被他淘汰的人淘汰的人,這些人分數都小於等於小美。測試
所以答案就是log2(count(score <= score[0])) score[0]是小美的分數。spa
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 int main(){ 8 //freopen("1.in","r",stdin); 9 int n,ans = 0,x0, x; 10 cin >> n; 11 for(int i = 0; i < n; i++){ 12 scanf("%d",&x); 13 if(i == 0) x0 = x; 14 ans += x <= x0; 15 } 16 cout << ((int)(log(ans * 1.0) / log(2.0))) << endl; 17 return 0; 18 }
優惠券設計
美團點評上有不少餐館優惠券,用戶能夠在美團點評App上購買。每種優惠券有一個惟一的正整數編號。每一個人能夠擁有多張優惠券,但每種優惠券只能同時擁有至多一張。每種優惠券能夠在使用以後繼續購買。日誌
當用戶在相應餐館就餐時,能夠在餐館使用優惠券進行消費。某人優惠券的購買和使用按照時間順序逐行記錄在一個日誌文件中,運營人員會按期抽查日誌文件看業務是否正確。業務正確的定義爲:一個優惠券必須先被購買,而後才能被使用。
某次抽查時,發現有硬盤故障,歷史日誌中有部分行損壞,這些行的存在是已知的,可是行的內容讀不出來。假設損壞的行能夠是任意的優惠券的購買或者使用。
如今給一個日誌文件,問這個日誌文件是否正確。如有錯,輸出最先出現錯誤的那一行,即求出最大s,使得記錄1到s-1知足要求;若沒有錯誤,輸出-1。
輸入包含多組數據 m 分別表示 m (0 ≤ m ≤ 5 * 10^5) 條記錄。 下面有m行,格式爲: I x (I爲Input的縮寫,表示購買優惠券x); O x(O爲Output的縮寫,表示使用優惠券x); ? (表示這條記錄不知道)。 這裏x爲正整數,且x ≤ 10^5 。
-1 或 x(1 ≤ x ≤ m) 其中x爲使得1到x-1這些記錄合法的最大行號。
0 1 O 1 2 ? O 1 3 I 1 ? O 1 2 I 2 O 1
-1 1 -1 -1 2
題解:
剛開始看到這題,就理所應當的認爲只須要記錄未知記錄的個數,和每一個優惠券的狀態便可,若是在購買時發現還存在這種優惠券,就用未知記錄代替,位置記錄條數減一(消費同理)。
這樣作的確是把不合理的購買或者消費記錄給清除了,可是這樣作法的一個最大問題就在於,它沒有考慮購買的時機,即認爲未知記錄是萬能的。可是例如連續兩次買進同一種優惠券(中間沒有未知記錄),這樣也是不合法的。所以,咱們須要分狀況討論:
購買優惠券x時:
1.若以前沒有交易過優惠券x或以前最後一次操做x已經將其賣出: 直接購買
2.前一次操做是買入優惠券x:
1)兩次買進之間沒有未知記錄: 這條記錄是不合法的
2)兩次買進之間有屢次(>=1)條未知記錄: 取最先的一條未知記錄充當x的使用記錄。
使用優惠券x時:
1.以前對x的操做最後一次是買入優惠券x: 直接使用
2.前一次對x的操做是對x的使用(或沒有對x的操做記錄):
1)找出這兩次使用操做的最先的一條未知記錄,充當買入操做
2)若沒有未知記錄,這條記錄不合法
上面爲何必定要取多天未知記錄的最先的一條,讀者本身能夠思考下。下面個人代碼用yhq變量保存最近的上一次操做優惠券x的位置,使用set集合來存放全部未知記錄出現的位置(能夠很方便的使用lower_bound函數)。
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <set> 5 #include <cstring> 6 7 using namespace std; 8 9 char c; 10 int n, x; 11 map<int, int> yhq; 12 set<int> unknown; 13 14 int main(){ 15 //freopen("in.txt","r",stdin); 16 while(~scanf("%d%*c", &n)) { 17 yhq.clear(); 18 unknown.clear(); 19 int has_error = -1; 20 for (int i = 1; i <= n; i ++) { 21 scanf("%c%*c", &c); 22 if(c == '?') { 23 unknown.insert(i); 24 continue; 25 } 26 scanf("%d%*c", &x); 27 if(has_error >= 0) { // has find ans 28 continue; 29 } 30 if(c == 'I') { 31 if(yhq[x] > 0) { 32 set<int>::iterator it = unknown.lower_bound(yhq[x]); 33 if(it == unknown.end()) has_error = i; 34 else unknown.erase(it); 35 } 36 yhq[x] = i; 37 } 38 else { 39 if(yhq[x] <= 0) { 40 set<int>::iterator it = unknown.lower_bound(-yhq[x]); 41 if(it == unknown.end()) has_error = i; 42 else unknown.erase(it); 43 } 44 yhq[x] = -i; 45 } 46 } 47 printf("%d\n", has_error); 48 } 49 return 0; 50 }
送外賣
輸入有 3 行。 第一行輸入一個整數 n (1 ≤ n ≤ 10^5)。 第二行輸入 n 個整數,分別表示 a[i] 。 第三行輸入 n 個整數,分別表示 b[i] 。 −n ≤ a[i], b[i] ≤ n
輸出一行字符串表示答案。
7 5 -3 6 5 -5 -1 6 -6 1 4 -2 0 -2 0
abbbb
題解:
終於經過全部數據了。
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <set> 5 #include <cstring> 6 7 using namespace std; 8 9 const int kMaxN = 1e5 + 5; 10 int n, a[kMaxN], b[kMaxN]; 11 int vis[kMaxN], ans_flag; 12 char str_ans[kMaxN]; 13 14 int dfs(char * ans, int step, int cur_node, bool flag) 15 { 16 if(cur_node < 1 || cur_node > n || vis[cur_node] > 1) 17 return 0; 18 if(vis[cur_node]) return 1; 19 if(cur_node == n) { 20 ans[step] = 0; 21 ans_flag = flag; 22 if(!flag) puts(ans); 23 return 2; 24 } 25 vis[cur_node] = 1; 26 ans[step] = 'a'; 27 int ret = dfs(ans, step + 1, cur_node + a[cur_node], flag); 28 if(ret == 2) return 2; 29 30 ans[step] = 'b'; 31 if(dfs(ans, step + 1, cur_node + b[cur_node], flag || ret==1) == 2) 32 return 2; 33 34 vis[cur_node] = 2; 35 return 0; 36 } 37 38 int main(){ 39 //freopen("in.txt","r",stdin); 40 cin >> n; 41 for (int i = 1; i <= n; i ++) 42 scanf("%d", &a[i]); 43 for (int i = 1; i <= n; i ++) 44 scanf("%d", &b[i]); 45 memset(vis, 0, sizeof(vis)); 46 if(dfs(str_ans, 0, 1, 0) != 2) 47 puts("No solution!"); 48 else if(ans_flag){ 49 puts("Infinity!"); 50 } 51 return 0; 52 }
數碼
一行,兩個整數 l 和 r (1 ≤ l ≤ r ≤ 10^9)。
輸出9行。
第 i 行,輸出數碼 i 出現的次數。
1 4
4 2 1 1 0 0 0 0 0
題解:
首先,使用find_ans(n)計算1~n全部數的答案,那麼最後的答案就是find_ans(r) - find_ans(l - 1)。
其次,要計算最高位爲i的約數出現的次數。例如i=2,那麼另cnt(x)爲n之內約數包含x的數的個數,有:
ans[2] = cnt(2) + cnt(20) + cnt(21) + .... cnt(29) + cnt(200) + cnt(201) + ... 其中(x<=n)
因此咱們分位數討論
1位[i, i+1),2位[i*10, (i+1)*10), 3位[i*100, (i+1)*100)... ... 其中i取1~9。
咱們知道,n之內包含約數x的天然數的個數爲 n / x,即cnt(x) = n / x, 咱們令ans[i]表示首位爲i的答案,那麼有:
ans[i] = sum{ sum{ n / x | i * 10^j <= x <= (i + 1) * 10^j} | 0 <= j <= 9}
上述步驟中能夠看到對於每個j, 即對於最高位爲i且位數爲(j+1)位的全部數x是連續的。 因此關鍵在於寫一個函數,用於計算:
sigle_sum = (n / low) + (n / (low + 1)) + ... + (n / high) 其中 low = i * 10^j, high = (i + 1) * 10^j
能夠看到上述計算單個答案sigle_sum 時,複雜度爲O(10^j), 這對於一個位數較大(如j>=7)狀況來講,枚舉量依然很是巨大。
可是咱們能夠看到當j>=5時,low = i * 10^5, 有 n/low <= 10^4,因此咱們能夠枚舉上述每個式子的商(如商=x),再計算[low,high]區間中商爲x的數有多少個,最後答案+ x * num(low, high, n, x),其中num(low, high, n, x)表示n/low, n/(low+1),,,n/(high)這些值中結果爲x的個數,這個咱們能夠O(1)的計算出來。
最後計算sigle_sum總複雜度小於O(1e5)。
最後計算總答案複雜度:O(9 * 10 * 1e5),終於能夠經過了~
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <set> 5 #include <cstring> 6 7 using namespace std; 8 9 typedef long long LL; 10 LL l, r; 11 LL a[10], b[10]; 12 13 // calc (n / low) + (n / (low + 1)) + ... + (n / high) 14 LL find_ans(LL low, LL high, LL n) 15 { 16 if(high < low) return 0; 17 LL result = 0; 18 if(high - low < 1e5) for (int i = low; i <= high; i ++) { 19 result += n / i; 20 } 21 else { 22 LL l = n / high, r = n / low; 23 for (int i = l; i <= r; i ++) { 24 LL lb = max(n / (i + 1), low), rb = min(n / i, high); 25 if(n / rb == n / lb) rb ++; 26 result += (rb - lb) * i; 27 } 28 } 29 return result; 30 } 31 32 void find_ans(LL n, LL * ans) 33 { 34 memset(ans, 0, sizeof(LL) * 10); 35 if(!n) return ; 36 for (int i = 1; i <= 9; i ++) { 37 LL mul_num = 1; 38 for (int j = 0; j <= 9 && mul_num <= n; j ++) { 39 ans[i] += find_ans(i * mul_num, min((i + 1) * mul_num - 1, n), n); 40 mul_num *= 10; 41 } 42 } 43 } 44 45 int main(){ 46 //freopen("in.txt","r",stdin); 47 cin >> l >> r; 48 find_ans(l - 1, a); 49 find_ans(r, b); 50 for (int i = 1; i <= 9; i ++) 51 cout << (b[i] - a[i]) << endl; 52 return 0; 53 }
圍棋
第一行,測試數據組數≤100 第二行,每組測試數據,執行的步數 n ≤ 2000 而後 n 行 B x y W x y (1 ≤ x ≤ 19,1 ≤ y ≤ 19) 其中,二元組 x,y 表示圍棋棋盤上第 x 行第 y 列對應的點。 輸入數據保證是黑白輪流下的。
多行 對於miss的狀況,輸出是哪種錯誤格式,其中: miss 1 表示下的位置已經有棋了 miss 2 表示違反規則6 miss 3 表示違反規則7 對於正常的操做,不用輸出。 最後輸出最終盤面。「B表示黑子,W表示白子,若是是空點的話,就輸出'.'字符。」
1 12 B 1 3 W 1 2 B 2 4 W 2 1 B 1 1 W 2 3 B 3 3 W 3 2 B 1 1 W 2 3 B 2 2 W 2 3 對應的棋形是這樣的:
miss 2 miss 2 miss 1 miss 3 .WB................ WB.B............... .WB................ ................... ................... ................... ................... ................... ................... ................... ................... ................... ................... ................... ................... ................... ................... ................... ...................
題解:
這個比較純粹,模擬每一次操做,而後判斷是否合法,沒太多可講之處,純粹是考察代碼實現能力。固然我也只是完成了這道題而已啦,代碼風格和可讀性還比較低。
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <set> 5 #include <cstring> 6 #include <vector> 7 #include <queue> 8 9 using namespace std; 10 11 const int kModNum = 1e9 + 7; 12 const int kBorderSize = 19; 13 const int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 14 15 struct Board { 16 int arr[20][20]; 17 int hash_val; 18 bool operator == (const Board & another) const { 19 for (int i = 0; i < kBorderSize; i ++) { 20 for (int j = 0; j < kBorderSize; j ++) { 21 if(arr[i][j] != another.arr[i][j]) return false; 22 } 23 } 24 return true; 25 } 26 bool operator < (const Board & another) const { 27 return hash_val < another.hash_val; 28 } 29 }; 30 31 int T, n, x, y; 32 char op; 33 Board board, tmp; 34 map< int, set<Board> > mps; 35 int vis[kBorderSize][kBorderSize]; 36 37 38 void Reset() 39 { 40 memset(board.arr, 0, sizeof(board.arr)); 41 mps.clear(); 42 } 43 44 int MyHash(const Board & board) 45 { 46 int hash_num = 0; 47 for (int i = 0; i < kBorderSize; i ++) { 48 for (int j = 0; j < kBorderSize; j ++) { 49 hash_num = ((hash_num << 1) % kModNum + hash_num) % kModNum; 50 hash_num = (hash_num + board.arr[i][j]) % kModNum; 51 } 52 } 53 return hash_num; 54 } 55 56 bool OutBound(int nx, int ny) 57 { 58 return nx < 0 || nx >= kBorderSize || ny < 0 || ny >= kBorderSize; 59 } 60 61 bool ExistBoard(const Board board) 62 { 63 return mps.count(board.hash_val) && mps[board.hash_val].count(board); 64 } 65 66 bool IsDeadPiece(int x, int y, int piece_type) 67 { 68 memset(vis, false, sizeof(vis)); 69 queue<int> que; 70 que.push(x * kBorderSize + y); 71 vis[x][y] = true; 72 while(!que.empty()) { 73 int fr = que.front(); 74 que.pop(); 75 x = fr / kBorderSize; 76 y = fr % kBorderSize; 77 for (int i = 0; i < 4; i ++) { 78 int nx = x + dir[i][0], ny = y + dir[i][1]; 79 if(OutBound(nx, ny) || vis[nx][ny]) continue; 80 if(!board.arr[nx][ny]) { 81 return false; 82 } 83 if(board.arr[nx][ny] == piece_type) { 84 que.push(nx * kBorderSize + ny); 85 vis[nx][ny] = true; 86 } 87 } 88 } 89 return true; 90 } 91 92 void RemoveDeadPiece() 93 { 94 for (int i = 0; i < kBorderSize; i ++) { 95 for (int j = 0; j < kBorderSize; j ++) { 96 if(vis[i][j]) board.arr[i][j] = 0; 97 } 98 } 99 } 100 101 bool MovePiece() 102 { 103 int put_val = op == 'B' ? 1 : 2; 104 // 放下這顆棋子 105 board.arr[x][y] = put_val; 106 // 找是否能移除對手的棋子 107 bool can_remove = false; 108 for (int i = 0; i < 4; i ++) { 109 int nx = x + dir[i][0], ny = y + dir[i][1]; 110 if(OutBound(nx, ny) || board.arr[nx][ny] != 3 - put_val) 111 continue; 112 if(IsDeadPiece(nx, ny, board.arr[nx][ny])) { 113 can_remove = true; 114 } 115 } 116 // 不能移走對手的棋子,本身又是死棋,不能下在這 117 if(!can_remove && IsDeadPiece(x, y, put_val)) 118 return false; 119 // 吧對手的死棋移除 120 for (int i = 0; i < 4; i ++) { 121 int nx = x + dir[i][0], ny = y + dir[i][1]; 122 if(OutBound(nx, ny) || board.arr[nx][ny] != 3 - put_val) 123 continue; 124 if(IsDeadPiece(nx, ny, board.arr[nx][ny])) { 125 RemoveDeadPiece(); 126 } 127 } 128 board.hash_val = MyHash(board); 129 return true; 130 } 131 132 void PrintBoard(Board board) 133 { 134 for (int i = 0; i < kBorderSize; i ++) { 135 for (int j = 0; j < kBorderSize; j ++) { 136 if(!board.arr[i][j]) putchar('.'); 137 else if(board.arr[i][j] & 1) putchar('B'); 138 else putchar('W'); 139 } 140 puts(""); 141 } 142 } 143 144 int main(){ 145 //freopen("in.txt","r",stdin); 146 cin >> T; 147 while(T--) { 148 Reset(); 149 scanf("%d%*c", &n); 150 while(n --) { 151 scanf("%c %d %d%*c", &op, &x, &y); 152 x --; y --; 153 // 第一種錯誤, 位置已經存在棋子 154 if(board.arr[x][y]) { 155 puts("miss 1"); 156 continue; 157 } 158 tmp = board; 159 if(!MovePiece()) { 160 puts("miss 2"); 161 board = tmp; 162 continue; 163 } 164 if(ExistBoard(board)) { 165 puts("miss 3"); 166 board = tmp; 167 continue; 168 } 169 mps[MyHash(board)].insert(board); 170 } 171 PrintBoard(board); 172 } 173 return 0; 174 }