CodeForces - 977A-B-C-D-E-F

Codefoces 977

A - Wrong Subtraction CodeForces - 977A

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 n. Tanya will subtract one from it k times. Your task is to print the result after all k subtractions.

It is guaranteed that the result will be positive integer number.

Input

The first line of the input contains two integer numbers n and k (2≤n≤109, 1≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.

Output

Print one integer number — the result of the decreasing n by one k times.

It is guaranteed that the result will be positive integer number.

Examples

Input
512 4
Output
50
Input
1000000000 9
Output
1

Note

The first example corresponds to the following sequence: 512→511→510→51→50.

题目大意:

给一个数,如果末位是0,就除以十;如果末位不是0就减一;需要操作k次,然后最后输出结果。

具体做法:

直接模拟

代码如下
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

bool judge(int n){
    
    
    n = n % 10;
    if(n == 0) return true;
    else return false;
}

int main(){
    
    
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
    
    
        for(int i = 1; i <= k; i ++){
    
    
            bool flag = judge(n);
            if(flag) n /= 10;
            else n --;
        }
        printf("%d\n",n);
    }
    return 0;
}


B - Two-gram CodeForces - 977B

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 s consisting 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 s three 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.

题目大意:

给你个字符串,问你哪两个相邻的字母组合出现的次数最多;

思路:

  1. 思路一(hash)
    对每个字符组合进行哈希,考虑到组合数并不多,可以开一个num数组记录每个组合出现次数。在更新次数最大值maxx时顺便更新一下答案,第二个字符的位置pos。
#include<bits/stdc++.h>
using namespace std;
const int N = 3e6 + 105;

int n, pos, num[N];
char s[N];

int main()
{
    
    
    scanf("%d",&n);
    scanf("%s",s + 1);
    int maxx = 0;
    for(int i = 2; i <= n; ++ i){
    
    
        int tp = ++ num[(s[i - 1] - 'A') * 30 + (s[i] - 'A')];
        if(tp > maxx){
    
    
            maxx = tp;
            pos = i;
        }
    }
    printf("%c%c\n",s[pos - 1], s[pos]);
    return 0;
}
  1. 思路二
    用map存字符组合,然后统计每个组合出现的次数,然后输出最大的
#include<bits/stdc++.h>
using namespace std;

map<string,int> mp;

map<string,int>::const_iterator it=mp.begin();
#define ll long long 
int main()
{
    
    
    int n;
    
    cin>>n;
    string s;
    cin>>s;
    char a,b;
    
    for(int i=0;i<n-1;i++)
    {
    
    
        a=s[i];
        b=s[i+1];
        char c[2];
        c[0]=a;
        c[1]=b;
        
        mp[c]++;
    }
    int max=0;
    for(it=mp.begin();it!=mp.end();it++)
    {
    
    
        if(it->second>max)
        {
    
    
            max=it->second;
        }
    }
    
    for(it=mp.begin();it!=mp.end();it++)
    {
    
    
        if(it->second==max)
        {
    
    
            cout<<it->first[0];
            cout<<it->first[1];
            cout<<endl;
            break;
        }
    }
    return 0;
}

C - Less or Equal CodeForces - 977C

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 2 elements 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个数,问能否在【1,1e9】中找到一个x,使得x恰好大于或等于n个数中k个,只能是k个,不能输出“-1”。能的话输出x;

思路:

相对比较简单,只需要特判一下当k==0时,先给a[n]sort一下,如果a【0】>1,则输出1
,如果a【0】=1,输出“-1”

代码如下

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;

int n, k;
ll a[N];

int main()
{
    
    
    scanf("%d%d",&n,&k);
    for(int i = 1; i <= n; ++ i) scanf("%lld",&a[i]);
    sort(a + 1, a + n + 1);
    if(k == 0){
    
    
        if(a[1] > 1) printf("1\n");
        else printf("-1\n");
        return 0;
    }
    if(a[k] != a[k + 1]) printf("%lld\n",a[k]);
    else printf("-1\n");
    return 0;
}

D - Divide by three, multiply by two CodeForces - 977D

Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n−1 operations of the two kinds:

divide the number x by 3 (x must be divisible by 3);
multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers 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

Note

In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8]. It can match possible Polycarp’s game which started with x=9.

题目大意:

题意一开始没看明白
•题意
  给你一个数 x,x 可以执行以下两种操作中的一种得到数 y:

y 再执行上述两种操作的一种得到数 z;

接着对 z 得到…

这样依次执行了 n-1 次会得到 n 个数;

现在给你这 n 个数,让你按照上述规则给这 n 个数排序,使得其满足

a1=x , a2=y , a3=z , …

输出这 n 个数;

思路:

  1. 思路一

直接硬干,找到一个x,然后判断能否成功

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=100+50;

int n;
ll a[maxn];
map<ll ,bool>f;

void Solve()
{
    
    
    ll x=a[1];
    while(true)///找到x
    {
    
    
        if(f.count(x*3))
            x *= 3;
        else if(!(x&1) && f.count(x/2))
            x /= 2;
        else
            break;
    }
    printf("%lld",x);///由x开始依次向后推
    for(int i=2;i <= n;++i)
    {
    
    
        if(f.count(x*2))
            x *= 2;
        else
            x /= 3;
        printf(" %lld",x);
    }
    printf("\n");
}
int main()
{
    
    
    scanf("%d",&n);
    for(int i=1;i <= n;++i)
    {
    
    
        scanf("%lld",a+i);
        f[a[i]]=true;
    }
    Solve();

    return 0;
}
  1. 思路二
    to[i]表示第 i 数后面放的数的位置,vis[i]表示第 i 个数前面的值有没有确定。从小到大排序,对每个数x,如果没有数在它前面,说明x/2没有在x前面,那只能试着把3x放在x前面,然后如果没有确定x后面的数,就试着把2x放在x后面。不考虑放x/3是因为x/3已经处理过了,肯定有放在它前面的值了。最后找一个前面没有值的数开始,沿着to[]数组的转移输出重排的序列。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 5;

int n;
ll a[N];
map<ll, int> mp;
int to[N], vis[N];
vector<int> res;

int main()
{
    
    
    scanf("%d",&n);
    for(int i = 1; i <= n; ++ i) {
    
    
        scanf("%lld",&a[i]);
    }
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; ++ i) mp[a[i]] = i;
    for(int i = 1; i <= n; ++ i){
    
    
        if(!vis[i]){
    
    
            ll tp = a[i] * 3;
            if(mp[tp]) {
    
    
                to[mp[tp]] = i;
                vis[i] = 1;
            }
        }
        if(!to[i]){
    
    
            ll tp = a[i] * 2;
            if(mp[tp]){
    
    
                to[i] = mp[tp];
                vis[mp[tp]] = 1;
            }
        }
    }
    for(int i = 1; i <= n; ++ i){
    
    
        if(vis[i]) continue;
        int tp = i;
        while(tp){
    
    
            res.push_back(tp);
            tp = to[tp];
        }
        break;
    }
    int tt = res.size();
    for(int i = 0; i < tt; ++ i){
    
    
        printf("%lld",a[res[i]]);
        if(i == tt - 1) printf("\n");
        else printf(" ");
    }
    return 0;
}

E - Cyclic Components CodeForces - 977E

You are given an undirected graph consisting of n vertices and m edges. Your task is to find the number of connected components which are cycles.

Here are some definitions of graph theory.

An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex a is connected with a vertex b, a vertex b is also connected with a vertex a). An edge can’t connect vertex with itself, there is at most one edge between a pair of vertices.

Two vertices u and v belong to the same connected component if and only if there is at least one path along edges connecting u and v.

A connected component is a cycle if and only if its vertices can be reordered in such a way that:

the first vertex is connected with the second vertex by an edge,
the second vertex is connected with the third vertex by an edge,

the last vertex is connected with the first vertex by an edge,
all the described edges of a cycle are distinct.
A cycle doesn’t contain any other edges except described above. By definition any cycle contains three or more vertices.

There are 6 connected components, 2 of them are cycles: [7,10,16] and [5,11,9,15].

Input

The first line contains two integer numbers n and m (1≤n≤2⋅105, 0≤m≤2⋅105) — number of vertices and edges.

The following m lines contains edges: edge i is given as a pair of vertices vi, ui (1≤vi,ui≤n, ui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,ui) there no other pairs (vi,ui) and (ui,vi) in the list of edges.

Output

Print one integer — the number of connected components which are also cycles.

Examples

Input
5 4
1 2
3 4
5 4
3 5
Output
1
Input
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
Output
2

Note

In the first example only component [3,4,5] is also a cycle.

The illustration above corresponds to the second example.

题意

给n个点, m条边,求如下图这样环的个数。
首先是个环,然后每个顶点的度数都是2
规定了一个简单环,除了在环上的边以外不能有任何其他边。求环的个数。

【思路】

可以发现,每个这样的环首先是一个联通块,而且联通块中每个点的度数为2。根据这一点我们用并查集找到所有联通块,如果一个联通块中度数都为2,res ++。最后res就是满足题意的环的个数。

#include<bits/stdc++.h>
using namespace std;

#define ll long long 

const int N=2e5+105;
int n,m,res;
int root[N],in[N],vis[N];
int Find(int x)
{
    
    
    return x==root[x]?x:root[x]=Find(root[x]);
}
int main()
{
    
    
    cin>>n>>m;
    for(int i=0;i<=n;i++)
    {
    
    
        root[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
    
    
        int x,y;
        cin>>x>>y;
        in[x]++;in[y]++;
        int tx=Find(x),ty=Find(y);
        if(tx!=ty) root[tx]=ty;
    }
    for(int i=1;i<=n;i++)
    {
    
    
        if(in[i]!=2) vis[Find(i)]=1;
    }
    for(int i=1;i<=n;i++)
    {
    
    
        if(root[i]==i&&!vis[i]) res++;
    }
    cout<<res<<endl;
    return 0;
}

F - Consecutive Subsequence CodeForces - 977F

You are given an integer array of length n.

You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [x,x+1,…,x+k−1] for some value x and length k.

Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4] the following arrays are subsequences: [3], [5,3,1,2,4], [5,1,4], but the array [1,3] is not.

Input

The first line of the input containing integer number n (1≤n≤2⋅105) — the length of the array. The second line of the input containing n integer numbers a1,a2,…,an (1≤ai≤109) — the array itself.

Output

On the first line print k — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.

On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.

Examples

Input
7
3 3 4 7 5 6 8
Output
4
2 3 5 6
Input
6
1 3 5 2 4 6
Output
2
1 4
Input
4
10 9 8 7
Output
1
1
Input
9
6 7 8 3 4 5 9 10 11
Output
6
1 2 3 7 8 9

Note

All valid answers for the first example (as sequences of indices):

[1,3,5,6]
[2,3,5,6]
All valid answers for the second example:

[1,4]
[2,5]
[3,6]
All valid answers for the third example:

[1]
[2]
[3]
[4]
All valid answers for the fourth example:

[1,2,3,7,8,9]

题意

首先输入n 然后输入n个数
  在这n个数中找出最长的连续的上升的子序列,就是123456

输出有两行

第一行为最长上升子序列的长度 第二行为序列中各个元素的下标

思路

因为是上升子序列,并且是连续的 所以可以使用map存储一下 map[i] = map[i-1]+1;

这样在我们处理完输入的数据以后便可以用迭代器遍历一下map[i]的最大值 以及i的值(map->second map->first)

i的值便是最长上升子序列的最后一个数的值 map[i]的最大值便是长度 序列开始的第一个值 = i-max(map[i])+1;

#include<bits/stdc++.h>
using namespace std;

#define ll long long 
bool cmp(const int &a,const int &b)
{
    
    
    return a>b;
}
const int maxn=2e6+10;
int main()
{
    
    
    ios::sync_with_stdio(false);
    map<int,int> m;
    int n;
    int a[maxn];
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        cin>>a[i];
        m[a[i]]=m[a[i]-1]+1;
    }
    int mmax=-999;
    int flag=0;
    map<int,int>::const_iterator it=m.begin();
    for(it=m.begin();it!=m.end();it++)
    {
    
    
        if(it->second>mmax)
        {
    
    
            mmax=it->second;
            flag=it->first;
        }
    }
    flag-=mmax;
    ++flag;
    cout<<mmax<<endl;
    for(int i=1;i<=n;i++)
    {
    
    
        if(a[i]==flag)
        {
    
    
            cout<<i<<" ";
            ++flag;
        }
    }
    printf("\n");
    
    
    return 0;
}

编写人署名:张国辉

猜你喜欢

转载自blog.csdn.net/m0_50511504/article/details/108524156