题目链接:http://codeforces.com/contest/984
Two players play a game.
Initially there are n−1 turns are made. The first player makes the first move, then players alternate turns.
The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.
You want to know what number will be left on the board after n−1 turns if both players make optimal moves.
The first line contains one integer 1≤n≤1000) — the number of numbers on the board.
The second line contains 1≤ai≤106).
Print one number that will be left on the board.
3
2 1 3
2
3
2 2 2
2
In the first sample, the first player erases 2 is left on the board.
In the second sample, 2 is left on the board regardless of the actions of the players.
题意:给你n个数,求出排在中间的那个数,签到题。
代码实现如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n; 5 int a[1007]; 6 7 int main() { 8 cin >>n; 9 for(int i = 0; i < n; i++) { 10 cin >>a[i]; 11 } 12 sort(a, a + n); 13 if(n % 2 == 1) { 14 cout <<a[n/2] <<endl; 15 } else { 16 cout <<a[(n-1) / 2] <<endl; 17 } 18 return 0; 19 }
One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.
Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?
He needs your help to check it.
A Minesweeper field is a rectangle 8, or a bomb. The field is valid if for each cell:
- if there is a digit k neighboring cells have bombs.
- if the cell is empty, then all neighboring cells have no bombs.
Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most 8 neighboring cells).
The first line contains two integers 1≤n,m≤100) — the sizes of the field.
The next 8, inclusive.
Print "YES", if the field is valid and "NO" otherwise.
You can choose the case (lower or upper) for each letter arbitrarily.
3 3
111
1*1
111
YES
2 4
*.*.
1211
NO
In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.
You can read more about Minesweeper in Wikipedia's article.
题意:扫雷游戏,让你判断他给你的图是否合法(.表示周围八格里面没有雷,*表示雷,数字表示周围有几颗雷)。
思路:用一个dfs跑一个标准图出来,然后进行对照即可。
代码实现如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n, m; 5 char mp[105][105], check[105][105]; 6 int vis[105][105]; 7 8 void dfs(int x, int y) { 9 vis[x][y] = 1; 10 int cnt = 0; 11 for(int i = -1; i < 2; i++) { 12 for(int j = -1; j < 2; j++) { 13 if(i == 0 && j == 0) continue; 14 int nx = x + i, ny = y + j; 15 if(nx >= 0 && nx < n && ny >= 0 && ny < m) { 16 if(check[nx][ny] == '*') cnt++; 17 if(vis[nx][ny] == 0) { 18 dfs(nx, ny); 19 } 20 } 21 } 22 } 23 if(cnt && check[x][y] == '.') { 24 check[x][y] = cnt + '0'; 25 } 26 } 27 28 int main() { 29 cin >>n >>m; 30 for(int i = 0; i < n; i++) { 31 cin >>mp[i]; 32 } 33 for(int i = 0; i < n; i++) { 34 for(int j = 0; j < m; j++) { 35 if(mp[i][j] == '*') { 36 check[i][j] = '*'; 37 } else { 38 check[i][j] = '.'; 39 } 40 } 41 } 42 dfs(0, 0); 43 int flag = 1; 44 for(int i = 0; i < n; i++) { 45 for(int j = 0; j < m; j++) { 46 if(mp[i][j] != check[i][j]) { 47 flag = 0; 48 break; 49 } 50 } 51 } 52 if(flag) cout <<"YES" <<endl; 53 else cout <<"NO" <<endl; 54 return 0; 55 }
You are given several queries. Each query consists of three integers b is a finite fraction.
A fraction in notation with base b is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.
The first line contains a single integer 1≤n≤105) — the number of queries.
Next 10.
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
2
6 12 10
4 3 10
Finite
Infinite
4
1 1 2
9 36 2
4 12 3
3 5 4
Finite
Finite
Finite
Infinite
题意:给你一个p,q,b,问p/q在b进制下是否为有限小数。
思路:数论题,方法为判断b的x次方是否能整除q。一开始我用的是唯一分解定理,但是一直T,从T10->WA7->T11,然后本以为接近正理了,然后就没有然后了,其实这题可以用gcd来将q进行消去因子,最后判断q是否等于1。注意小trick,不然还是会T。
代码实现如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 int n; 6 ll p, q, b; 7 8 ll gcd(ll a, ll b) { 9 return (b == 0) ? a : gcd(b, a % b); 10 } 11 12 int main() { 13 ios::sync_with_stdio(false); 14 cin.tie(0); 15 cin >>n; 16 while(n--) { 17 cin >>p >>q >>b; 18 ll g = gcd(p, q); 19 p = p / g; 20 q = q / g; 21 while(g = gcd(q, b), g != 1) { 22 while(q % g == 0) q /= g; 23 } 24 if(q == 1) cout <<"Finite" <<endl; 25 else cout <<"Infinite" <<endl; 26 } 27 return 0; 28 }
For an array f as
where bitwise exclusive OR.
For example, f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15
You are given an array al,al+1,…,ar.
The first line contains a single integer a.
The second line contains 0≤ai≤230−1) — the elements of the array.
The third line contains a single integer 1≤q≤100000) — the number of queries.
Each of the next 1≤l≤r≤n).
Print q lines — the answers for the queries.
3
8 4 1
2
2 3
1 2
5
12
6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2
60
30
12
3
In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.
In second sample, optimal segment for first query are [1,2].
题意:给你一个序列,然后按照他所给的递推式求出所问区间内f的最大值。
思路:仔细研究一下这个递推式会发现本题的方法为区间dp。由于询问次数太大,因而我们先进行预处理,然后就能在O(1)的复杂度内进行询问。
代码实现如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 const int maxn = 5e3 + 7; 6 int n, q, l, r; 7 ll a[maxn], ans[maxn][maxn], dp[maxn][maxn]; 8 9 int main() { 10 ios::sync_with_stdio(false); 11 cin.tie(0); 12 cin >>n; 13 for(int i = 1; i <= n; i++) { 14 cin >>a[i]; 15 dp[i][i] = a[i]; 16 ans[i][i] = a[i]; 17 } 18 for(int i = 1; i <= n; i++) { 19 for(int j = 1; j + i <= n; j++) { 20 ans[j][j+i] = ans[j][j+i-1] ^ ans[j+1][i+j]; 21 dp[j][j+i] = max(ans[j][j+i], max(dp[j][j+i-1], dp[j+1][i+j])); 22 } 23 } 24 cin >>q; 25 while(q--) { 26 cin >>l >>r; 27 cout <<dp[l][r] <<endl; 28 } 29 return 0; 30 }