2018国庆第五场个人赛

CodeForces 1060A

 

Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.

For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.

You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.

Input

The first line contains an integer n— the number of cards with digits that you have (1≤n≤100).

The second line contains a string of n digits (characters "0", "1", ..., "9") s1,s2,…,sn. The string will not contain any other characters, such as leading or trailing spaces.

Output

If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.

Sample Input

Input

11
00000000008

Output

1

Input

22
0011223344556677889988

Output

2

Input

11
31415926535

Output

0
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
    int n,i,ans=0,sum=0,len=0;
    char s[110];
    cin>>n;
    ans=n/11;
    cin>>s;
    len=strlen(s);
    for(i=0;i<len;i++)
    {
        if(s[i]=='8')
            sum++;
    }
    ans=min(ans,sum);
    cout<<ans<<endl;
    return 0;
}

 CodeForces 1060B

You are given a positive integer n.

Let S(x) be sum of digits in base 10 representation of x, for example, S(123)=1+2+3=6, S(0)=0.

Your task is to find two integers a,b, such that 0≤a,b≤n, a+b=n and S(a)+S(b) is the largest possible among all such pairs.

Input

The only line of input contains an integer n(1≤n≤1012).

Output

Print largest S(a)+S(b) among all pairs of integers a,b, such that 0≤a,b≤n and a+b=n.

Sample Input

Input

35

Output

17

Input

10000000000

Output

91

Hint

In the first example, you can choose, for example, a=17 and b=18, so that S(17)+S(18)=1+7+1+8=17. It can be shown that it is impossible to get a larger answer.

In the second test example, you can choose, for example, a=5000000001 and b=4999999999, with S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.

这是一个错误的代码 样例123 

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
    ll a,b,i,n,ans1=0,ans2=0,ans=0;
    cin>>n;
    if(n%2!=0)
    {
        a=n/2;
        b=n/2+1;
    }
    else
    {
        a=n/2-1;
        b=n/2+1;
    }
    while(a)
    {
        ans1+=a%10;
        a/=10;
    }
    while(b)
    {
        ans2+=b%10;
        b/=10;
    }
    ans=ans1+ans2;
    cout<<ans<<endl;
    return 0;
}

这是AC代码

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
    ll n,k=0,kk=0,ans1=0,ans2=0,t;
    cin>>n;
    while(k<=n)
    {
        k=k*10+9;
        t=k;
        if(k>n)
        {
            t=(t-9)/10;
            break;
        }
    }
    k=t;
    kk=n-k;
    //cout<<k<<" "<<kk<<endl;
    while(k)
    {
        ans1+=k%10;
        k/=10;
    }
    while(kk)
    {
        ans2+=kk%10;
        kk/=10;
    }
    cout<<ans1+ans2<<endl;
    return 0;
}

CodeForces 712B

use MathJax to parse formulas

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:

  • An 'L' indicates he should move one unit left.
  • An 'R' indicates he should move one unit right.
  • A 'U' indicates he should move one unit up.
  • A 'D' indicates he should move one unit down.

But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.

Input

The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.

Output

If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.

Sample Input

Input

RRU

Output

-1

Input

UDUR

Output

1

Input

RUUR

Output

2

Hint

In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.

In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
char s[100100];
int main()
{
    memset(s,0,sizeof(s));
    ll n,ans=0,i,j,l=0,r=0,u=0,d=0;
    cin>>s;
    n=strlen(s);
    if(n%2!=0)
        cout<<"-1"<<endl;
    else
    {
        for(i=0;i<n;i++)
        {
            if(s[i]=='L')
                l++;
            else
            if(s[i]=='R')
                r++;
            else
            if(s[i]=='U')
                u++;
            else
            if(s[i]=='D')
                d++;
        }
        ll ans1=0,ans2=0;
        ans1=max(l,r)-min(l,r);
        ans2=max(u,d)-min(u,d);
        if((ans1+ans2)%2==1)
           cout<<"-1"<<endl;
        else
            cout<<(ans1+ans2)/2<<endl;
    }
    return 0;
}

CodeForces 712C

 

Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.

In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.

What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?

Input

The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

Output

Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

Sample Input

Input

6 3

Output

4

Input

8 5

Output

3

Input

22 4

Output

6

Hint

In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .

In the second sample test, Memory can do .

In the third sample test, Memory can do:

.

【题意】
现有边长为x的等边三角形,Memory想要将其变成边长为y的等边三角形

现规定Memory每秒能够改变一条边的大小,但要保证改变后的三条边仍能构成一个三角形

问,最少需要多少时间才能变为边长为y的等边三角形

【类型】
贪心,逆推

【分析】

一开始,正常人的想法就是如何改变边长为x的等边三角形的边,使得其变为边长为y的等边三角形

但是在不断尝试过程中,就会发现,貌似不是非常好处理

根据定理"三角形两边之和大于第三边,两边之差小于第三边"

就很难确定最初应该把x减少为多少合适,如果减少太多,势必会使得另外两条边减少速度过慢

(22,22,22)→(4,22,22)→(4,19,22)→(4,19,16)→(4,13,16)→(4,13,10)→(4,7,10)→(4,7,4)→(4,4,4)

但如果减少太少,同样会影响速率

应该是个怎么样的度,总是把握不好

这时,把问题反过来想想,发现有出路

减法控制不好,相对来说加法就简单得多

为了尽快使y->x,那每秒都要使得y增加得尽可能多

所以每次选取最短边,将其变为另两条边之和-1("三角形两边之和大于第三边")

直到三条边都大于等于x为止

AC代码

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
    ll x,y,a,b,c,ans=0;
    cin>>x>>y;
    a=b=c=y;
    while(1)
    {
        if(a>=x&&b>=x&&c>=x)
            break;
        if(a<=b&&a<=c)
            a=b+c-1;
        else
        if(b<=a&&b<=c)
            b=a+c-1;
        else
        if(c<=a&&c<=b)
            c=a+b-1;
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值