2018SDIBT_国庆个人第五场

时间:2023-03-09 15:01:55
2018SDIBT_国庆个人第五场

A - ACodeForces 1060A

Description

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 \leq n \leq 100$$$).

The second line contains a string of $$$n$$$ digits (characters "0", "1", ..., "9") $$$s_1, s_2, \ldots, s_n$$$. 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

Hint

In the first example, one phone number, "8000000000", can be made from these cards.

In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".

In the third example you can't make any phone number from the given cards.

题意:问你能组成多少个电话号码 像8xxxxxxxxxx的,给你n个卡片,每个卡片只能用一次或者不用

分析:不用考虑电话号码里具体的排列

1:如果给出的卡片没有8或者卡片数量小于11,直接输出0

2:t=卡片数除以11,如果t的数量大于号码为8的卡片的数量,则输出8的数量,否则输出t

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int n,a[];
char s[];
while(~scanf("%d",&n))
{
memset(a,,sizeof(a));
scanf("%s",s);
for(int i=;i<n;i++)
{
a[s[i]-'']++;
}
if(a[]==||n<)
printf("0\n");
else
{
int t=n/;
if(a[]>t)
{
printf("%d\n",t);
}
else
printf("%d\n",a[]);
}
}
return ;
}

B-codeforces 1060B

Description

You are given a positive integer nn.

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

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

Input

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

Output

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

Sample Input

Input
35
Output
17
Input
10000000000
Output
91

Hint

In the first example, you can choose, for example, a=17a=17 and b=18b=18, so that S(17)+S(18)=1+7+1+8=17S(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=5000000001a=5000000001 and b=4999999999b=4999999999, with S(5000000001)+S(4999999999)=91S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.

题意:给你一个n,让你找到a,b,使得a+b=n,并且s(a)+s(b)最大,s(123)=1+2+3,输出s(a)+s(b)

分析:我们很容易想到,当拆分的两个数中9的数量最多的时候,s(a)+s(b)最大,例如35=29+6=19+16=9+26=17+18,我们发现他们的s(a)+s(b)都等于17;

又比如 10000000000,我们可以拆成 10000000000=9999999999+1=9999999998+2...,他们的s(a)+s(b)都等于91

所以我们可以从最高位拆分,比如23456=19999+3457,100=99+1,123=99+24这种形式,这时获得的9是最多的,s(a)+s(b)也是最大的

#include<cstdio>
int main()
{
long long n;
while(~scanf("%lld",&n))
{
long long t,tt=;
t=n;
while(t>)
{
t/=;
tt*=;
}//找到最高位
long long ans1=t*tt-;//第一部分
long long ans2=n-ans1;第二部分
int sum=;
while(ans1)//求第一部分每个数字的和
{
sum+=ans1%;
ans1/=;
}
while(ans2)//求第二部分每个数字的和
{
sum+=ans2%;
ans2/=;
}
printf("%d\n",sum);
}
return ;
}

D-codeforces712c

Description

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 ab, and c as (a, b, c). Then, Memory can do 2018SDIBT_国庆个人第五场.

In the second sample test, Memory can do 2018SDIBT_国庆个人第五场.

In the third sample test, Memory can do: 2018SDIBT_国庆个人第五场

2018SDIBT_国庆个人第五场.

题意:给你一个x,y,让你从边长为y的等边三角形变到边长为x的等边三角形,一次只能改动一条边的大小,问你最少需要改动多少次

分析:改动一条边的前提是,改动这条边后,这三条边还是能组成一个三角形,满足a+b>c,a-b<c。一开始三边为(a,b,c)并且a=b=c,,将最小的那条边变为另外那两条边相加再减一,(a,b,a+b-1),满足a+b>c&&a-b<c,依次改动下去...直到某一条边将大于等于x,这时,三条边都处于小于x的状态,每条边只需要再改动一次即可形成边长为x的等边三角形

注意: 要开long long

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int x,y;
while(~scanf("%d %d",&x,&y))
{
int ans=,flag=;
int t=y,k=y;//t,tt表示每一轮的三角形中最大的边和第二大的边
while()
{
if(t+k-<x)//每改动一条边次数加1
ans++;
else//当某条边将要大于等于x的时候就退出
break;
if(flag==)
{
t+=k-;
flag=;
}
else
{
k+=t-;
flag=;
} }
printf("%d\n",ans+);//最后每一条边再改动一次就能形成边长为x的等边三角形
}
return ;
}

E-codeforces 712B

Description

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.

题意:给你一个字符串s,其中的‘U’代表上,‘D’代表下,‘L’代表左,‘R’代表右,每次只能改变一个字符,问你最少需要改动多少次,memory能回到起点,输出最小的改动次数,若不能回到起点则输出-1。

分析:当字符串长度为奇数时,一定不能回到起点。

当字符串长度为偶数时,统计字符串中u,d,l,r的数量,需要改动的次数为|(u-d)/2|+|(l-r)/2|,需要注意的是,当u-d与l-r同时为奇数时,会少算一次改动的次数,加上就好了。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
char s[];
while(~scanf("%s",s))
{
int k;
k=strlen(s);
if(k%!=)
printf("-1\n");
else
{
int u=,d=,l=,r=;
for(int i=; i<k; i++)
{
if(s[i]=='U')
u++;
else if(s[i]=='D')
d++;
else if(s[i]=='L')
l++;
else if(s[i]=='R')
r++;
}
int sum=;
sum+=abs(u-d)/+abs(l-r)/;
if((u-d)%!=&&(l-r)%!=)
sum++;
printf("%d\n",sum);
}
}
return ;
}