最近看到牛课网美团一个编程竞赛,想着作作看,结果一写就是两天。。真是写不动了啊。话很少说,下面开始个人题解。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 }