Codeforces Round #483 [Thanks, Botan Investments and Victor Shaburov!] Editoral

T1:

A. Game
time limit per test
2 seconds
memory limit per test
256 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 1122 is left on the board.

In the second sample, 22

 is left on the board regardless of the actions of the players.

题意:一个人取最大值,另一个人取最小值,轮流取n-1次。。。

扫描二维码关注公众号,回复: 926621 查看本文章

思路:直接排序,判断一下奇偶就可以了。。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n, a[1007];

int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d",&n))
    {
        for(int i  = 0; i < n; i++) scanf("%d",&a[i]);
        sort(a, a+n);
        if(n%2 != 0) printf("%d\n", a[n/2]);
        else printf("%d\n", a[n/2 - 1]);
    }
    return 0;
}

T2 : 

B. Minesweeper
time limit per test
1 second
memory limit per test
256 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.

题目:给定一个n*m的矩阵,里面'*'代表雷, '.'代表0,其他的含有‘1-8’,判断一个位置周围是不是严格的有其对应数字的雷,直接判断就好了。。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e2+7;
int n, m;
char c[maxn][maxn];

int find(int a, int b)
{
    int ans = 0;
    for(int i = -1; i < 2; i++)
        for(int j = -1; j < 2; j++)
          if(c[a+i][b+j] == '*') ans++;
    return ans;
}


void solve()
{
    bool f = true;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(c[i][j] == '*') continue;
            else if(c[i][j] == '.') {
                if(find(i, j) != 0) {
                    f = false;
                    break;
                }
            } else {
                if(find(i, j) != (c[i][j] - '0')) {
                    f = false;
                    break;
                }
            }
        }
    }
    if(f) puts("YES");
    else puts("NO");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d%d",&n,&m))
    {
      for(int i = 0; i <= n+1; i++)
        for(int j = 0; j <= m+1; j++)
        c[i][j] = '.';
      for(int i = 1; i <= n; i++)
      {
          getchar();
          for(int j = 1; j <= m;j++)
             scanf("%c",&c[i][j]);
      }
      solve();
    }
    return 0;
}


T3:

C. Finite or not?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given several queries. Each query consists of three integers ppqq 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 ppqq, and bb (0p10180≤p≤10181q10181≤q≤10182b10182≤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

612=12=0,510612=12=0,510

43=1,(3)1043=1,(3)10

936=14=0,012936=14=0,012

412=13=0,1

3


题目:n(n<=1e5)个查询给p,q,b(1<=p,q,b<=1e18)三个数,判断p/q在b进制下是不是有限的。。

思路:先利用gcd将p,q化简,首先在10进制下,判断充要条件是q的素因子因子只有2,5;那么就是判断q跟b是不是有相同的素因子;一开始用唯一分解,删素因子,超时,后来在1:59修改了一下过了样例。。。但我还是觉得不大对,可能数据比较弱;

其实可以一直判断gcd的。。

代码1(比赛时):

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6;
int n, p[maxn], tot = 0;
ll a, b, c;
bool prim[maxn];

void get_prim()
{
    for(int i = 2; i < maxn; i++) prim[i] = true;//初始化为质数
    for(int i = 2; i < maxn; i++){
        if(prim[i]) p[tot++] = i;//把质数存起来
        for(int j = 0; j < tot && i * p[j] < maxn; j++){
            prim[i * p[j]] = false;
            if(i % p[j] == 0) break;//保证每个合数被它最小的质因数筛去
        }
    }
}

ll gcd(ll a,ll b) { return b == 0 ? a : gcd(b,a%b); }


void solve(ll m)
{
    ll k = m;
    for(int i = 0; i < tot&& k > 1&& p[i]*p[i] <= k; i++)
    {
        if(k%p[i] == 0)
	    {
            while(b%p[i] == 0) b /= p[i];
            while(k%p[i] == 0) k /= p[i];
        }
    }
    if(k != 1) while(b%k == 0) b /= k;
}


int main()
{
    get_prim();
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d",&n))
    {
      while(n--)
      {
          scanf("%lld%lld%lld",&a, &b, &c);
          ll d = gcd(a, b);
          a /= d; b /= d;
          ll k = gcd(b, c);
          if(k == 1&&b != 1) {
                printf("Infinite\n");
                continue;
          }
          if(b == 1){
             printf("Finite\n");
             continue;
          }
          solve(k);
          if(b == 1) printf("Finite\n");
          else printf("Infinite\n");
      }
    }
    return 0;
}


代码2(补):

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e9;
int n;
ll a, b, c;

ll gcd(ll a,ll b) { return b == 0 ? a : gcd(b,a%b); }


void solve(ll m)
{
    ll k = gcd(b, m);
    while(k != 1)
    {
        b /= k;
        k = gcd(b, m);
    }
}


int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d",&n))
    {
      while(n--)
      {
          scanf("%lld%lld%lld",&a, &b, &c);
          ll d = gcd(a, b);
          a /= d; b /= d;
          ll k = gcd(b, c);
          if(k == 1&&b != 1) {
                printf("Infinite\n");
                continue;
          }
          if(b == 1){
             printf("Finite\n");
             continue;
          }
          solve(k);
          if(b == 1) printf("Finite\n");
          else printf("Infinite\n");
      }
    }
    return 0;
}


T4:

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

f(b)={b[1]if m=1f(b[1]b[2],b[2]b[3],,b[m1]b[m])otherwise,f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,

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 llrr (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(b),b对应一个数组,作用方式见题意。给你一个长度为n的序列b,然后给你q次询问,对于每次询问,给你l r,问你在[l,r]这段区间内所有子串中f的最大值为多少。

思路1:区间dp;

状态 dp[ i ][ j ] :表示从 i 到 j 的最大值;

状态转移方程 : dp[ i ][ j ] = max(dp[ i ][ j ] ,dp[ i + 1][ j ],  dp[i][ j - 1], f(i, j));

f(i, j) = f(i+1, j)^f(i, j - 1); 

代码1:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5e3+7;
int n, q;
ll a[maxn], dp[maxn][maxn], f[maxn][maxn];
bool vis[maxn][maxn];

void work(int l, int r)
{
    vis[l][r] = true;
    if(l == r) {
        dp[l][r] = f[l][r] = a[l];
        return;
    }
    if(!vis[l][r-1]) work(l, r - 1);
    if(!vis[l+1][r]) work(l+1, r);
    dp[l][r] = f[l][r] = f[l][r-1]^f[l+1][r];
    dp[l][r] = max(dp[l][r], max(dp[l + 1][r], dp[l][r - 1]));
}


void solve()
{
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\n", dp[l][r]);
}


int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        memset(dp, 0, sizeof(dp));
        memset(f, 0, sizeof(f));
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        work(1, n);
        scanf("%d", &q);
        while(q--) solve();
    }
    return 0;
}

思路2:就是递推就可以。。

dp[ i ][ j ]:以 i 为起点,长度为 j 的序列的最大值;

状态转移方程:dp[ i ][ j ] = max(dp[ i ][ j ], dp[ i + 1][ j - 1], dp[ i ][j - 1], dp[ i + 1][ j - 1]^dp[ i ][j - 1]);

代码2:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5e3+7;
int n, q;
ll dp[maxn][maxn];

void init()
{
   for(int j = 2; j <= n; j++)
   {
       for(int i = 0; i + j <= n+1; i++)
       {
           dp[i][j] = dp[i][j - 1]^dp[i+1][j - 1];
       }
   }
   for(int j = 2; j <= n; j++)
   {
       for(int i = 0; i + j <= n+1; i++)
       {
           dp[i][j] = max(dp[i][j], max(dp[i+1][j - 1], dp[i][j - 1]));
       }
   }
}

void solve()
{
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\n", dp[l][r - l + 1]);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++) scanf("%lld", &dp[i][1]);
        init();
        scanf("%d", &q);
        while(q--) solve();
    }
    return 0;
}

具体参照这个图吧(像不像杨辉三角)!!!!


猜你喜欢

转载自blog.csdn.net/weixin_39792252/article/details/80334222