Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)A,B

时间:2022-11-16 13:18:05

A. Bear and Poker

Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.

Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?
Input

First line of input contains an integer n (2 ≤ n ≤ 105), the number of players.

The second line contains n integer numbers a1, a2, …, an (1 ≤ ai ≤ 109) — the bids of players.

Output

Print “Yes” (without the quotes) if players can make their bids become equal, or “No” otherwise.
Sample test(s)

Input

4
75 150 75 50

Output

Yes

Input

3
100 150 250

Output

No

Note

In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.

It can be shown that in the second sample test there is no way to make all bids equal.

思路:求出最大公约数,除掉之后看是不是只包含2和3

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N,a[maxn];
bool judge(int x){
    while(x%2==0)x/=2;
    while(x%3==0)x/=3;
    return x==1;
}
int main(){
    while(scanf("%d",&N)!=EOF){
        int g=1;
        for(int i=1;i<=N;i++){
            scanf("%d",&a[i]);
            if(i==1)g=a[i];
            else g=__gcd(g,a[i]);
        }
        bool flag=true;

        for(int i=1;i<=N;i++){
            a[i]/=g;
            if(!judge(a[i])){
                flag=false;
                break;
            }
        }
        printf("%s\n",flag?"Yes":"No");
    }
    return 0;
}

B. Bear and Blocks

Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.

Limak will repeat the following operation till everything is destroyed.

Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.

Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.
Input

The first line contains single integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers h1, h2, …, hn (1 ≤ hi ≤ 109) — sizes of towers.
Output

Print the number of operations needed to destroy all towers.
Sample test(s)
Input

6
2 1 4 6 2 2

Output

3

Input

7
3 3 3 1 3 3 3

Output

2

Note

The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.
Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)A,B
After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.

思路:对于第i列,一次操作之后变成min(h[i]-1,h[i+1],h[i-1])所以先预处理出从左从右分别消除的次数,然后取最大值即可

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N,H[maxn];
int main(){
    scanf("%d",&N);
    for(int i=1;i<=N;i++)scanf("%d",&H[i]);
    H[1]=1;
    for(int i=2;i<=N;i++)H[i]=min(H[i],H[i-1]+1);
    H[N]=1;
    for(int i=N-1;i>=1;i--)H[i]=min(H[i],H[i+1]+1);
    int ans=0;
    for(int i=1;i<=N;i++)ans=max(ans,H[i]);
    printf("%d\n",ans);
    return 0;
}