Codeforces Round #479 (Div. 3)
A - Wrong Subtraction
Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm:
if the last digit of the number is non-zero, she decreases the number by one;
if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).
You are given an integer number . Tanya will subtract one from it times. Your task is to print the result after all subtractions.
It is guaranteed that the result will be positive integer number.
input
The first line of the input contains two integer numbers and ( , ) — the number from which Tanya will subtract and the number of subtractions correspondingly.
ouput
Print one integer number — the result of the decreasing by one times.
It is guaranteed that the result will be positive integer number.
Examples
Input
512 4
Output
50
Input
题目大意
给定一个数n,如果他的个位数为0,则除10,否则-1。k为操作的次数。
代码如下:
#include<iostream>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
while(k--)
{
int p=n%10;
if(p)n-=1;
else n/=10;
}
cout<<n<<endl;
return 0;
}
B - Two-gram
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, “AZ”, “AA”, “ZA” — three distinct two-grams.
You are given a string sconsisting of n capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string s = “BBAABBBA” the answer is two-gram “BB”, which contained in sthree times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other.
Input
The first line of the input contains integer number n (2≤n≤100) — the length of string s. The second line of the input contains the string s consisting of n capital Latin letters.
Output
Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string s as a substring (i.e. two consecutive characters of the string) maximal number of times.
Examples
Input
7
ABACABA
Output
AB
Input
5
ZZZAA
Output
ZZ
Note
In the first example “BA” is also valid answer.
In the second example the only two-gram “ZZ” can be printed because it contained in the string “ZZZAA” two times.
题目大意
给一个长度为n的字符串,找出出现次数最多的两个连续字符。并输出其出现的次数。
思路:
开一个二维数组记录每种字符串出现的次数并及时更新最大值,最后输出最大值即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int book[30][30];//记录每组字符出现的次数
int main()
{
int n,ma=0;
char m1,m2;
cin>>n;
char s[110]={0};
for(int i=0;i<n;i++)cin>>s[i];
for(int j=0;j<n-1;j++)
{
int t1=s[j]-'A';//转换为数字
int t2=s[j+1]-'A';
book[t1][t2]++;
if(book[t1][t2]>ma)//更新出现次数最多的
{
ma=book[t1][t2];
m1=s[j];m2=s[j+1];
}
}
cout<<m1<<m2<<endl;
return 0;
}
听说用KMP算法??没学过先MARK一下。
C - Less or Equal
You are given a sequence of integers of length n and integer number k. You should print any integer number x in the range of [1;109] (i.e. 1≤x≤109) such that exactly k elements of given sequence are less than or equal to x.
Note that the sequence can contain equal elements.
If there is no such x, print “-1” (without quotes).
Input
The first line of the input contains integer numbers n and k (1≤n≤2⋅105, 0≤k≤n). The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤109) — the sequence itself.
Output
Print any integer number x from range [1;109] such that exactly k elements of given sequence is less or equal to x.
If there is no such x, print “-1” (without quotes).
Examples
Input
7 4
3 7 5 1 10 3 20
Output
6
Input
7 2
3 7 5 1 10 3 20
Output
-1
Note
In the first example 5 is also a valid answer because the elements with indices [1,3,4,6] is less than or equal to 5 and obviously less than or equal to 6.
In the second example you cannot choose any number that only 2elements of the given sequence will be less than or equal to this number because 3 elements of the given sequence will be also less than or equal to this number.
题目大意:
给n个数字,要求输出x,x大于该数列等于前k个数的每一个数。如果不存在这样的数则输出-1。
思路:
排序后if判断即可,需要注意的是对于边界情况的处理和x的取值范围。当k为0的时候,我们需要判断数列最小元素的大小,如果这个数比1大,那么就输出比最小数还要小的数即可(但是还是要满足x的取值范围)。如果小于等于1,那就输出-1。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long a[200100];
int main()
{
long long n,k;
cin>>n>>k;
for(long long i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
if(k==0)//要考虑到X的范围,就要考虑a[0]的大小
{
if(a[0]>1)cout<<a[0]-1<<endl;
else cout<<-1<<endl;
}
else if(k==n)cout<<a[n-1]<<endl;
else
{
if(a[k-1]!=a[k])cout<<a[k-1]<<endl;
else cout<<-1<<endl;
}
return 0;
}
D - Divide by three, multiply by two
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n−1operations of the two kinds:
1.divide the number xby 3 (x must be divisible by 3);
2.multiply the number xby 2
After each operation, Polycarp writes down the result on the board and replaces x
by the result. So there will be nnumbers on the board after all.
You are given a sequence of length n
— the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp’s game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2≤n≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
题目大意:
给定n个数,输出它的一种排列,这种排列的规则是下一个数是前一个数除3或者乘2.
思路:
根据题意,本题暗藏着一种排序的规则,那就是根据含3因子数量从大到小排序。可以整除3次数多的排在前,数量相等则值小的排在前。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct num
{
long long key;//数值
int yin;//含多少个3因子
}a[110];
int cmp(num a,num b)
{
if(a.yin==b.yin)return a.key<b.key;//因子数相等的
else return a.yin>b.yin;
}
int divide(long long t)
{
int sum=0;
while(t%3==0)
{
sum++;
t/=3;
}
return sum;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i].key;
a[i].yin=divide(a[i].key);//统计3因子的数量
}
sort(a,a+n,cmp);
for(int i=0;i<n-1;i++)cout<<a[i].key<<' ';
cout<<a[n-1].key<<endl;
return 0;
}
还可以深搜写。未完待更。。。。。