Codeforces Round #361 (Div. 2)

A. Mike and Cellphone
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:

Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number "586" are the same as finger movements for number "253":

Mike has already put in a number by his "finger memory" and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements?

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 9) — the number of digits in the phone number that Mike put in.

The second line contains the string consisting of n digits (characters from '0' to '9') representing the number that Mike put in.

Output

If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES" (without quotes) in the only line.

Otherwise print "NO" (without quotes) in the first line.

Examples
input
3
586
output
NO
input
2
09
output
NO
input
9
123456789
output
YES
input
3
911
output
YES
Note

You can find the picture clarifying the first sample case in the statement above.

判断这个形状能否上下移动即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<list>
#include<vector>
using namespace std;
int n,key[15];
char s[15];
char str[15];
bool judgeup(){
    for(int i=0;i<n;++i){
        if(key[i]-3<=0)return false;
    }

    return true;
}
bool judgedown(){
    for(int i=0;i<n;++i){
        if(key[i]+3>9&&(key[i]+3)!=11)return false;
    }
    return true;
}
bool judgeright(){
    for(int i=0;i<n;++i){
        if(key[i]==11||key[i]==3||key[i]==6||key[i]==9)return false;
    }
    return true;
}
bool judgeleft(){
    for(int i=0;i<n;++i){
        if(key[i]==11||key[i]==1||key[i]==4||key[i]==7)return false;
    }
    return true;
}
int main()
{
    scanf("%d",&n);
    scanf("%s",str);
    for(int i=0;i<n;++i){
        key[i]=str[i]-'0';
        if(key[i]==0)key[i]=11;
    }
    if(judgeup()||judgedown()||judgeright()||judgeleft()){
        printf("NO\n");
    }
    else {
        printf("YES\n");
    }
    return 0;
}

B. Mike and Shortcuts
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.

City consists of n intersections numbered from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i to intersection j requires |i - j| units of energy. Thetotal energy spent by Mike to visit a sequence of intersections p1 = 1, p2, ..., pk is equal to  units of energy.

Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts in Mike's city, the ith of them allows walking from intersection i to intersection ai (i ≤ ai ≤ ai + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p1 = 1, p2, ..., pk then for each 1 ≤ i < k satisfying pi + 1 = api and api ≠ pi Mike will spend only 1 unit of energy instead of |pi - pi + 1| walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequence p1 = 1, p2 = ap1, p3 = ap2, ..., pk = apk - 1, he spends exactly k - 1 units of total energy walking around them.

Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequencep1 = 1, p2, ..., pk = i.

Input

The first line contains an integer n (1 ≤ n ≤ 200 000) — the number of Mike's city intersection.

The second line contains n integers a1, a2, ..., an (i ≤ ai ≤ n , , describing shortcuts of Mike's city, allowing to walk from intersection i to intersection ai using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from ai to i).

Output

In the only line print n integers m1, m2, ..., mn, where mi denotes the least amount of total energy required to walk from intersection 1to intersection i.

Examples
input
3
2 2 3
output
0 1 2 
input
5
1 2 3 4 5
output
0 1 2 3 4 
input
7
4 4 4 4 7 7 7
output
0 1 2 1 2 3 3 
Note

In the first sample case desired sequences are:

1: 1m1 = 0;

2: 1, 2m2 = 1;

3: 1, 3m3 = |3 - 1| = 2.

In the second sample case the sequence for any intersection 1 < i is always 1, i and mi = |1 - i|.

In the third sample case — consider the following intersection sequences:

1: 1m1 = 0;

2: 1, 2m2 = |2 - 1| = 1;

3: 1, 4, 3m3 = 1 + |4 - 3| = 2;

4: 1, 4m4 = 1;

5: 1, 4, 5m5 = 1 + |4 - 5| = 2;

6: 1, 4, 6m6 = 1 + |4 - 6| = 3;

7: 1, 4, 5, 7m7 = 1 + |4 - 5| + 1 = 3.

自己写的dfs超时不知道怎么优化,换成bfs过了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=200010;
int num[maxn],ans[maxn],n;
void bfs(){
    queue<int>Q;
    Q.push(1);
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        if(u+1<=n){
            if(ans[u+1]>ans[u]+1){
                ans[u+1]=ans[u]+1;
                Q.push(u+1);
            }
        }
        if(u-1>=1){
            if(ans[u-1]>ans[u]+1){
                ans[u-1]=ans[u]+1;
                Q.push(u-1);
            }
        }
        if(num[u]!=u&&num[u]!=-1){
            if(ans[num[u]]>ans[u]+1){
                ans[num[u]]=ans[u]+1;
                Q.push(num[u]);
            }
        }
    }
}
int main()
{
    scanf("%d",&n);
    memset(num,-1,sizeof(num));
    memset(ans,0x3f,sizeof(ans));
    for(int i=1;i<=n;++i){
        scanf("%d",&num[i]);
    }ans[1]=0;bfs();
    for(int i=1;i<=n;++i){
        printf("%d ",ans[i]);
    }
    printf("\n");
    return 0;
}

C. Mike and Chocolate Thieves
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bad news came to Mike's village, some thieves stole a bunch of chocolates from the local factory! Horrible!

Aside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of chocolates for himself, the next thief will take exactly k times more than the previous one. The value of k (k > 1) is a secret integer known only to them. It is also known that each thief's bag can carry at most n chocolates (if they intend to take more, the deal is cancelled) and that there were exactly four thieves involved.

Sadly, only the thieves know the value of n, but rumours say that the numbers of ways they could have taken the chocolates (for a fixed n, but not fixed k) is m. Two ways are considered different if one of the thieves (they should be numbered in the order they take chocolates) took different number of chocolates in them.

Mike want to track the thieves down, so he wants to know what their bags are and value of n will help him in that. Please find the smallest possible value of n or tell him that the rumors are false and there is no such n.

Input

The single line of input contains the integer m (1 ≤ m ≤ 1015) — the number of ways the thieves might steal the chocolates, as rumours say.

Output

Print the only integer n — the maximum amount of chocolates that thieves' bags can carry. If there are more than one n satisfying the rumors, print the smallest one.

If there is no such n for a false-rumoured m, print  - 1.

Examples
input
1
output
8
input
8
output
54
input
10
output
-1
Note

In the first sample case the smallest n that leads to exactly one way of stealing chocolates is n = 8, whereas the amounts of stealed chocolates are (1, 2, 4, 8) (the number of chocolates stolen by each of the thieves).

In the second sample case the smallest n that leads to exactly 8 ways is n = 54 with the possibilities:(1, 2, 4, 8),  (1, 3, 9, 27),  (2, 4, 8, 16),  (2, 6, 18, 54),  (3, 6, 12, 24),  (4, 8, 16, 32),  (5, 10, 20, 40),  (6, 12, 24, 48).

There is no n leading to exactly 10 ways of stealing chocolates in the third sample case.

二分

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<list>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=200010;
typedef long long LL;
LL n;
LL judge(LL mid){
    LL cnt = 0;
    for(LL k = 2;k <= mid;++k) {
        if(k*k*k>mid)break;
        cnt = cnt + mid/(k*k*k);
        if(cnt>n)break;
    }
    return cnt;
}
int main()
{
    scanf("%lld",&n);
    LL left=1,right=1e17,ans=-1;
    while(left<=right){
        LL mid=(left+right)/2;
        LL cnt=judge(mid);
        if(cnt==n){
            ans=mid;right=mid-1;
        }
        else if(cnt>n){
            right=mid-1;
        }
        else {
            left=mid+1;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


D. Friends and Subsequences
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?

Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of  while !Mike can instantly tell the value of .

Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactlyn(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs  is satisfied.

How many occasions will the robot count?

Input

The first line contains only integer n (1 ≤ n ≤ 200 000).

The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.

The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.

Output

Print the only integer number — the number of occasions the robot will count, thus for how many pairs  is satisfied.

Examples
input
6
1 2 3 2 1 4
6 7 1 2 3 2
output
2
input
3
3 3 3
1 1 1
output
0
Note

The occasions in the first sample case are:

1.l = 4,r = 4 since max{2} = min{2}.

2.l = 4,r = 5 since max{2, 1} = min{2, 3}.

There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.

题意:给出两列数,查找有多少区间满足第一列数的最大值等于第二列数的最小值。

解题思路二分:固定左区间二分右边界当最小值大于最大值可以继续向右否则向左,两次二分求出相等的范围即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<stack>
using namespace std;
const int maxn=200010;
int dpmax[maxn][20];
int dpmin[maxn][20];
int n;
void init(){
    int i,j;
    for(j=1;j<=log2(n+1);++j){
        for(i=1;i<=n-(1<<j)+1;++i){
            dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
            dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
        }
    }
}
int query(int l,int r){
    int k=log2(r-l+1);
    return max(dpmax[l][k],dpmax[r-(1<<k)+1][k])-min(dpmin[l][k],dpmin[r-(1<<k)+1][k]);
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i){
        scanf("%d",&dpmax[i][0]);
    }
    for(int i=1;i<=n;++i){
        scanf("%d",&dpmin[i][0]);
    }init();
    long long ans=0;
    for(int i=1;i<=n;++i){
        int left=i,right=n,ans1=-1,ans2=-1;
        while(left<=right){
            int mid = left + right >>1;
            int k=query(i,mid);
            if(k==0){
                ans1=mid;
                right=mid-1;
            }
            else if(k>0){
                right=mid-1;
            }
            else {
                left=mid+1;
            }
        }
        if(ans1!=-1){

            left=i;right=n;
            while(left<=right){
                int mid =left + right >>1;
                int k=query(i,mid);
                if(k==0){
                    ans2=mid;
                    left=mid+1;
                }
                else if(k>0){
                    right=mid-1;
                }
                else {
                    left=mid+1;
                }
            }
            ans=ans+ans2-ans1+1;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值