zoukankan      html  css  js  c++  java
  • Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]

    题目链接:http://codeforces.com/contest/984

    A. Game
    time limit per test:2 seconds
    memory limit per test:512 megabytes
    input:standard input
    output:standard output

    Two players play a game.

    Initially there are nn integers a1,a2,,ana1,a2,…,an written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. n1n−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 n1n−1 turns if both players make optimal moves.

    Input

    The first line contains one integer nn (1n10001≤n≤1000) — the number of numbers on the board.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106).

    Output

    Print one number that will be left on the board.

    Examples
    Input
    Copy
    3
    2 1 3
    Output
    Copy
    2
    Input
    Copy
    3
    2 2 2
    Output
    Copy
    2
    Note

    In the first sample, the first player erases 33 and the second erases 11. 22 is left on the board.

    In the second sample, 22 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 }
    B. Minesweeper
    time limit per test:2 seconds
    memory limit per test:512 megabytes
    input:standard input
    output:standard output

    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 n×mn×m, where each cell is either empty, or contains a digit from 11 to 88, or a bomb. The field is valid if for each cell:

    • if there is a digit kk in the cell, then exactly kk 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 88 neighboring cells).

    Input

    The first line contains two integers nn and mm (1n,m1001≤n,m≤100) — the sizes of the field.

    The next nn lines contain the description of the field. Each line contains mm characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from 11 to 88, inclusive.

    Output

    Print "YES", if the field is valid and "NO" otherwise.

    You can choose the case (lower or upper) for each letter arbitrarily.

    Examples
    Input
    Copy
    3 3
    111
    1*1
    111
    Output
    Copy
    YES
    Input
    Copy
    2 4
    *.*.
    1211
    Output
    Copy
    NO
    Note

    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 }
    C. Finite or not?
    time limit per test:2 seconds
    memory limit per test:512 megabytes
    input:standard input
    output:standard output

    You are given several queries. Each query consists of three integers pp, qq and bb. You need to answer whether the result of p/qp/q in notation with base bb is a finite fraction.

    A fraction in notation with base bb 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.

    Input

    The first line contains a single integer nn (1n1051≤n≤105) — the number of queries.

    Next nn lines contain queries, one per line. Each line contains three integers pp, qq, and bb (0p10180≤p≤1018, 1q10181≤q≤1018, 2b10182≤b≤1018). All numbers are given in notation with base 1010.

    Output

    For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.

    Examples
    Input
    Copy
    2
    6 12 10
    4 3 10
    Output
    Copy
    Finite
    Infinite
    Input
    Copy
    4
    1 1 2
    9 36 2
    4 12 3
    3 5 4
    Output
    Copy
    Finite
    Finite
    Finite
    Infinite
    Note

    题意:给你一个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 }
    D. XOR-pyramid
    time limit per test:2 seconds
    memory limit per test:512 megabytes
    input:standard input
    output:standard output

    For an array bb of length mm we define the function ff as

    where ⊕ is bitwise exclusive OR.

    For example, f(1,2,4,8)=f(12,24,48)=f(3,6,12)=f(36,612)=f(5,10)=f(510)=f(15)=15f(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 aa and a few queries. Each query is represented as two integers ll and rr. The answer is the maximum value of ff on all continuous subsegments of the array al,al+1,,aral,al+1,…,ar.

    Input

    The first line contains a single integer nn (1n50001≤n≤5000) — the length of aa.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai23010≤ai≤230−1) — the elements of the array.

    The third line contains a single integer qq (1q1000001≤q≤100000) — the number of queries.

    Each of the next qq lines contains a query represented as two integers ll, rr (1lrn1≤l≤r≤n).

    Output

    Print qq lines — the answers for the queries.

    Examples
    Input
    Copy
    3
    8 4 1
    2
    2 3
    1 2
    Output
    Copy
    5
    12
    Input
    Copy
    6
    1 2 4 8 16 32
    4
    1 6
    2 5
    3 4
    1 2
    Output
    Copy
    60
    30
    12
    3
    Note

    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 [3,6][3,6], for second query — [2,5][2,5], for third — [3,4][3,4], for fourth — [1,2][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 }
  • 相关阅读:
    django上传下载大文件
    ssh隧道技术
    防止网站被抓
    lvm在线扩容
    Python之配置文件模块 ConfigParser
    Oracle常用查询
    Oracle_where子句
    Oracle_单行函数
    Oracle_多行函数
    Oracle_SQL92_连接查询
  • 原文地址:https://www.cnblogs.com/Dillonh/p/9047601.html
Copyright © 2011-2022 走看看