Codeforces Round #486 (Div. 3)

Codeforces Round #486 (Div. 3)

A - Diverse Team CodeForces - 988A
There are n students in a school class, the rating of the i-th student on Codehorses is ai. You have to form a team consisting of k students (1≤k≤n) such that the ratings of all team members are distinct.

If it is impossible to form a suitable team, print “NO” (without quotes). Otherwise print “YES”, and then print k distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them.

Input
The first line contains two integers n and k (1≤k≤n≤100) — the number of students and the size of the team you have to form.

The second line contains n integers a1,a2,…,an (1≤ai≤100), where ai is the rating of i-th student.

Output
If it is impossible to form a suitable team, print “NO” (without quotes). Otherwise print “YES”, and then print k distinct integers from 1 to n which should be the indices of students in the team you form. All the ratings of the students in the team should be distinct. You may print the indices in any order. If there are multiple answers, print any of them.

Assume that the students are numbered from 1 to n.

Examples
Input
5 3
15 13 15 15 12
Output
YES
1 2 5
Input
5 4
15 13 15 15 12
Output
NO
Input
4 4
20 10 40 30
Output
YES
1 2 3 4
Note

All possible answers for the first example:
{1 2 5}
{2 3 5}
{2 4 5}
Note that the order does not matter.

分析:
题目的意思就是叫你判断是否能从n个学生中找出k个不同rating的学生,如果可以输出“YES”,并输出他们的下标。而且最重要的就是可以忽略它的输出顺序,根据这个特点,我们不难想到用map容器,first保存rating,second保存下标,然后看它的size是否大于等于k,如果是,则输出“YES”,并输出k个map中的下标。

Accpetd code:

#include<iostream>
#include<map>
using namespace std;

int main()
{
    int n,k;
    while(cin>>n>>k)
    {
        map<int,int> m;//first is value,second is loc
        m.clear();
        for(int i=1;i<=n;i++)
        {
            int num;
            cin>>num;
            m[num]=i;
        }
        if(k>(int)m.size())
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            int cnt=0,first=1;
            for(map<int,int>::iterator it=m.begin();it!=m.end()&&cnt<k;it++)
            {
                if(first)
                {
                    cout<<it->second;
                    first=0;
                }
                else
                    cout<<" "<<it->second;
                cnt++;
            }
            cout<<endl;
        }
    }
}


B - Substrings Sort CodeForces - 988B
You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.

String a is a substring of string b if it is possible to choose several consecutive letters in b in such a way that they form a. For example, string “for” is contained as a substring in strings “codeforces”, “for” and “therefore”, but is not contained as a substring in strings “four”, “fofo” and “rof”.

Input
The first line contains an integer n (1≤n≤100) — the number of strings.

The next n lines contain the given strings. The number of letters in each string is from 1 to 100, inclusive. Each string consists of lowercase English letters.

Some strings might be equal.

Output
If it is impossible to reorder n given strings in required order, print “NO” (without quotes).

Otherwise print “YES” (without quotes) and n given strings in required order.

Examples
Input
5
a
aba
abacaba
ba
aba
Output
YES
a
ba
aba
aba
abacaba
Input
5
a
abacaba
ba
aba
abab
Output
NO
Input
3
qwerty
qwerty
qwerty
Output
YES
qwerty
qwerty
qwerty
Note
In the second example you cannot reorder the strings because the string “abab” is not a substring of the string “abacaba”.

分析:
题目的意思就是叫你判断是否可能把输入的字符串排成“拓扑序”,如果可能,输出“YES”,并输出排好序的字符串数组,否则输出”NO”。当然,用拓扑排序当然可以做(我一开始就是卡在拓扑排序,主要还是因为不熟),后来想了一下,如果我们直接用sort函数,然后再判断是否满足拓扑序更方便。

Accepted code:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;

const int maxn=105;
string s[maxn];
int n;

//judge whether a is substring of b
bool judge(string a,string b)
{
    if(a==b) return true;
    for(int i=0;i<b.size();i++)
    {
        for(int j=i+1;j<=b.size();j++)
        {
            if(b.substr(i,j-i)==a)
                return true;
        }
    }
    return false;
}



int main()
{
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
            cin>>s[i];

        sort(s,s+n,judge);

        bool ans=true;
        for(int i=1;i<n;i++)
        {
            if(judge(s[i-1],s[i])==false)
            {
                ans=false;
                break;
            }
        }

        if(ans)
        {
            cout<<"YES\n";
            for(int i=0;i<n;i++)
                cout<<s[i]<<endl;
        }
        else
            cout<<"NO\n";
    }
}

C - Equal Sums CodeForces - 988C
You are given k sequences of integers. The length of the i-th sequence equals to ni.

You have to choose exactly two sequences i and j (i≠j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to ni−1) equals to the sum of the changed sequence j (its length will be equal to nj−1).

Note that it’s required to remove exactly one element in each of the two chosen sequences.

Assume that the sum of the empty (of the length equals 0) sequence is 0.

Input
The first line contains an integer k (2≤k≤2⋅105) — the number of sequences.

Then k pairs of lines follow, each pair containing a sequence.

The first line in the i-th pair contains one integer ni (1≤ni<2⋅105) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of ni integers ai,1,ai,2,…,ai,ni.

The elements of sequences are integer numbers from −104 to 104.

The sum of lengths of all given sequences don’t exceed 2⋅105, i.e. n1+n2+⋯+nk≤2⋅105.

Output
If it is impossible to choose two sequences such that they satisfy given conditions, print “NO” (without quotes). Otherwise in the first line print “YES” (without quotes), in the second line — two integers i, x (1≤i≤k,1≤x≤ni), in the third line — two integers j, y (1≤j≤k,1≤y≤nj). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y.

Two chosen sequences must be distinct, i.e. i≠j. You can print them in any order.

If there are multiple possible answers, print any of them.

Examples
Input
2
5
2 3 1 3 2
6
1 1 2 2 2 1
Output
YES
2 6
1 2
Input
3
1
5
5
1 1 1 1 1
2
2 3
Output
NO
Input
4
6
2 2 2 2 2 2
5
2 2 2 2 2
3
2 2 2
5
2 2 2 2 2
Output
YES
2 2
4 1
Note
In the first example there are two sequences [2,3,1,3,2] and [1,1,2,2,2,1]. You can remove the second element from the first sequence to get [2,1,3,2] and you can remove the sixth element from the second sequence to get [1,1,2,2,2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.

分析:
题目的意思就是给你k个序列,然后判断是否能够选出2个序列,使之各去掉一个元素后的和相等。
先放官方题解吧:
官方题解
具体实现的话就是利用map,保存三元组ti=(sumi,seqi,eli),如果出现了sum一样且seqi不同,则说明有解。

Accepted code:

#include<iostream>
#include<map>
using namespace std;

const int maxn=2*(1e5);

typedef pair<int,int> p;//seq[i],ele[i]
typedef long long int ll;
map<long long int,p> Mp;

int num[maxn];

int main()
{
    int k;
    cin>>k;
    bool res=false;
    p ans1,ans2;
    for(int i=1;i<=k;i++)
    {
        int len;
        cin>>len;
        long long int s=0;
        for(int j=0;j<len;j++)
        {
            cin>>num[j];
            s+=num[j];
        }
        for(int j=0;j<len;j++)
        {
            if(Mp.count(s-num[j]))
            {
                p pa=Mp[s-num[j]];
                if(i!=pa.first)
                {
                    res=true;
                    ans1=pa;
                    ans2.first=i;
                    ans2.second=j+1;
                }
            }
            p pp=pair<int,int>(i,j+1);
            Mp.insert(map<ll,p>::value_type(s-num[j],pp));
        }
    }
    if(res)
    {
        cout<<"YES\n";
        cout<<ans1.first<<" "<<ans1.second<<endl;
        cout<<ans2.first<<" "<<ans2.second<<endl;
    }
    else
        cout<<"NO\n";
}

D - Points and Powers of Two CodeForces - 988D
There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.

In other words, you have to choose the maximum possible number of points xi1,xi2,…,xim such that for each pair xij, xik it is true that |xij−xik|=2d where d is some non-negative integer number (not necessarily the same for each pair of points).

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of points.

The second line contains n pairwise distinct integers x1,x2,…,xn (−109≤xi≤109) — the coordinates of points.

Output
In the first line print m — the maximum possible number of points in a subset that satisfies the conditions described above.

In the second line print m integers — the coordinates of points in the subset you have chosen.

If there are multiple answers, print any of them.

Examples
Input
6
3 5 4 7 10 12
Output
3
7 3 5
Input
5
-1 2 5 8 11
Output
1
8
Note
In the first example the answer is [7,3,5]. Note, that |7−3|=4=22, |7−5|=2=21 and |3−5|=2=21. You can’t find a subset having more points satisfying the required property.

分析:
题目意思就是叫你找出尽可能多的数字使他们相对subset中的每个数字都相差2^d,d>=0。
这里有个很关键的地方就是子集的个数是小于等于3的,下面我们来证明一下:
我们先假设子集的大小为3,这三个数字从小到大分别为a,b,c,则有
b-a=2^x
c-a=2^y
c-b=2^z
联立得到:2^x+2^z=2^y
要使上式成立,则x=z,y=x+1,于是可以得到a,b,c为等差数列,方差为2^x
如果子集的个数大于3,则子集的任意三个数字都要满足上述关系,可以发现不存在这样大于3个数字的子集(可以子集手动罗列一些情况试试)。
于是我们就可以先判断子集的个数是否能够为3,是否能否2,若都不能,则输出任意一个数组中的数字

Acceptd code:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=2*(1e5);
long long int a[maxn];

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);

    //judge whether has 3 anwsers?
    for(int i=0;i<n;i++)
    {
        for(int k=0;k<33;k++)
        {
            long long int temp1=pow(2,k)+a[i];
            int loc1=lower_bound(a,a+n,temp1)-a;
            long long int temp2=pow(2,k+1)+a[i];
            int loc2=lower_bound(a,a+n,temp2)-a;
            if(loc1<n&&loc2<n&&a[loc1]==temp1&&a[loc2]==temp2)
            {
                cout<<"3"<<endl;
                cout<<a[i]<<" "<<temp1<<" "<<temp2<<endl;
                return 0;
            }
        }
    }

    //judge whether has 2 anwsers?
    for(int i=0;i<n;i++)
    {
        for(int k=0;k<33;k++)
        {
            long long int temp1=pow(2,k)+a[i];
            int loc1=lower_bound(a,a+n,temp1)-a;
            if(loc1<n&&a[loc1]==temp1)
            {
                cout<<"2"<<endl;
                cout<<a[i]<<" "<<temp1<<endl;
                return 0;
            }
        }
    }

    //1 anwser
    cout<<"1"<<endl;
    cout<<a[0]<<endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值