Codeforces Round #486 (Div. 3)

A. Diverse Team

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n students in a school class, the rating of the ii-th student on Codehorses is ai. You have to form a team consisting of k students (1kn) 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 kk 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 nn and kk (1kn100) — the number of students and the size of the team you have to form.

The second line contains nn integers a1,a2,,an (1ai100), where aiai is the rating of ii-th student.

Output

If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print kk distinct integers from 11 to nnwhich 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
Copy
5 3
15 13 15 15 12
output
Copy
YES
1 2 5
input
Copy
5 4
15 13 15 15 12
output
Copy
NO
input
Copy
4 4
20 10 40 30
output
Copy
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个数字,求输出m个不同的数字的下标 不足m个输出NO

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int f[105],pos[105];
int main()
{
    int n,m;
    int cnt=0,d;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&d);
        f[d]++;
        if(f[d]==1)
            {cnt++;
            pos[d]=i;}
    }
    if(cnt>=m)
    {
        printf("YES\n");
        for(int i=1;i<=100&&m;i++)
        {
            if(pos[i])
            {
                printf("%d ",pos[i]);
                m--;
            }
        }
    }
    else
        printf("NO\n");

    return 0;
}
View Code

B. Substrings Sort

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given nn 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 aa is a substring of string b if it is possible to choose several consecutive letters in b in such a way that they form aa. 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 nn (1n100) — the number of strings.

The next nn 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 nn given strings in required order, print "NO" (without quotes).

Otherwise print "YES" (without quotes) and nn given strings in required order.

Examples
input
Copy
5
a
aba
abacaba
ba
aba
output
Copy
YES
a
ba
aba
aba
abacaba
input
Copy
5
a
abacaba
ba
aba
abab
output
Copy
NO
input
Copy
3
qwerty
qwerty
qwerty
output
Copy
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".

 题意:输出n个字符串,重新排列,让前一个字符串成为这个字符串的子串,如果对于所有字符串都满足,输出YES且输出排列顺序,反之输出NO

写个cmp重载,然后使用substr 当然使用find strstr都行,我就记得substr用法emmmm

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
bool cmp(string a,string b)
{
    if(a.size()==b.size())
        return a>b;
    else return a.size()<b.size();
}
int main()
{
    int n;
    scanf("%d",&n);
    string s[105];
    for(int i=0; i<n; i++)
        cin>>s[i];
    sort(s,s+n,cmp);
    int cnt=0;
    for(int i=1; i<n; i++)
    {
        int len=s[i-1].size();
        for(int j=0; j<s[i].size(); j++)
        {
            if(s[i-1]==s[i].substr(j,len))
            {
                cnt++;
                break;
            }
        }
    }
    if(cnt==n-1)
    {
        cout<<"YES"<<endl;
        for(int i=0; i<n; i++)
            cout<<s[i]<<endl;
    }
    else
        cout<<"NO"<<endl;
}
View Code

 

C. Equal Sums

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given kk sequences of integers. The length of the ii-th sequence equals to nini.

You have to choose exactly two sequences ii and jj (iji≠j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence ii (its length will be equal to ni1ni−1) equals to the sum of the changed sequence jj (its length will be equal to nj1nj−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 00) sequence is 00.

Input

The first line contains an integer kk (2k2105) — the number of sequences.

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

The first line in the ii-th pair contains one integer nini (1ni<2105) — the length of the ii-th sequence. The second line of the ii-th pair contains a sequence of nini integers ai,1,ai,2,,ai,niai,1,ai,2,…,ai,ni.

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

The sum of lengths of all given sequences don't exceed 2105, i.e. n1+n2++nk2105.

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 ii, xx (1ik,1xni1≤i≤k,1≤x≤ni), in the third line — two integers jj, yy (1jk,1ynj1≤j≤k,1≤y≤nj). It means that the sum of the elements of the ii-th sequence without the element with index xx equals to the sum of the elements of the jj-th sequence without the element with index yy.

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

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

Examples
input
Copy
2
5
2 3 1 3 2
6
1 1 2 2 2 1
output
Copy
YES
2 6
1 2
input
Copy
3
1
5
5
1 1 1 1 1
2
2 3
output
Copy
NO
input
Copy
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
Copy
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][1,1,2,2,2]. The sums of the both resulting sequences equal to 88, i.e. the sums are equal.

 题意:给出K个序列,求是否可以删除两个序列的的一个元素,使得两个元素和相等

map套pair map记录每次和,然后pari记录删除的序列及元素

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
map<int,pair<int,int> >mp;
const int maxn = 200000+5;
int a[maxn];
int main()
{
    int n,k,sum;
    scanf("%d",&k);
    for(int i=1; i<=k; i++)
    {
        sum=0;
        scanf("%d",&n);
        for(int j=1; j<=n; j++)
        {
            scanf("%d",&a[j]);
            sum+=a[j];
        }
        for(int j=1;j<=n;j++)
        {
            map<int, pair<int,int> >::iterator it=mp.find(sum-a[j]);
            if(it!=mp.end())
            {
                printf("YES\n");
                printf("%d %d\n%d %d\n",it->second.first,it->second.second,i,j);
                return 0;
            }
        }
        for(int j=1;j<=n;j++)
        {
            map<int, pair<int,int> >::iterator it=mp.find(sum-a[j]);
            if(it==mp.end())
            {
               pair<int,int> p=make_pair(i,j);
                mp[sum-a[j]]=p;
            }
        }
    }
    printf("NO\n");
    return 0;
}
View Code

 

D. Points and Powers of Two

time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn distinct points on a coordinate line, the coordinate of ii-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 xijxij, xikxik it is true that |xijxik|=2d where dd is some non-negative integer number (not necessarily the same for each pair of points).

Input

The first line contains one integer nn (1n2105) — the number of points.

The second line contains nn pairwise distinct integers x1,x2,,xn (109xi109) — the coordinates of points.

Output

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

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

If there are multiple answers, print any of them.

Examples
input
Copy
6
3 5 4 7 10 12
output
Copy
3
7 3 5
input
Copy
5
-1 2 5 8 11
output
Copy
1
8
Note

In the first example the answer is [7,3,5]. Note, that |73|=4=22, |75|=2=21 and |35|=2=21. You can't find a subset having more points satisfying the required property.

 题意:求一个集合中的任意两个差为2的整数幂次

思路:开始一直没想到集合元素最多三个,后面补题才想到emmm,直接把元素都如set,然后直接判断a[i]-(1<<j)与a[i]+(1<<j)在set出现过没就行

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200000+5;
set<ll> st;
ll a[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
        st.insert(a[i]);
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<31; j++)
        {
            if(st.find(a[i]+(1LL*1<<j))!=st.end()&&st.find(a[i]-(1LL*1<<j))!=st.end())
            {
                printf("3\n%lld %lld %lld\n",a[i]+(1LL*1<<j),a[i],a[i]-(1LL*1<<j));
                return 0;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<31; j++)
        {
            if(st.find(a[i]+(1LL*1<<j))!=st.end())
            {
                printf("2\n%lld %lld\n",a[i],a[i]+1LL*(1<<j));
                return 0;
            }
        }
    }
    printf("1\n%lld\n",a[1]);
}
View Code

E. Divisibility by 25

 

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer nn from 11 to 1018 without leading zeroes.

In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.

What is the minimum number of moves you have to make to obtain a number that is divisible by 25? Print -1 if it is impossible to obtain a number that is divisible by 25.

Input

The first line contains an integer nn (1n1018). It is guaranteed that the first (left) digit of the number nn is not a zero.

Output

If it is impossible to obtain a number that is divisible by 25, print -1. Otherwise print the minimum number of moves required to obtain such number.

Note that you can swap only adjacent digits in the given number.

Examples
input
Copy
5071
output
Copy
4
input
Copy
705
output
Copy
1
input
Copy
1241367
output
Copy
-1
Note

In the first example one of the possible sequences of moves is 5071 → 5701 → 7501 → 7510 → 7150.

 题意:给出一个数,求它能否交换(相邻的交换 最后能整除25 

就考虑末尾为00 75 25 50的情况,注意交换后出现前导0的情况。直接模拟就好了  比赛的时候模拟半天写错了,还是刷少了题

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 20;
char a[maxn];
int b[maxn];
int ans=0x3f3f3f3f;
int f[11];
void pd(int x,int y)
{
    int i;
    for(int i=1; i<=strlen(a+1); i++)
        b[i]=a[i]-'0';
    int cnt=0;
    int n=strlen(a+1);

    if(b[n]!=y)
    {
        for( i=n; i>=0; i--)
            if(b[i]==y) break;
        for(; i<n; i++)
        {
            swap(b[i],b[i+1]);
            cnt++;
        }

    }
    if(b[n-1]!=x)
    {
        for(i=n-1; i>=0; i--)
            if(b[i]==x) break;
        for(; i<n-1; i++)
        {
            swap(b[i],b[i+1]);
            cnt++;
        }
    }
    if(b[1]==0)
    {
        for(i=2; i<=n-2; i++)
            if(b[i]!=0) break;
        if(i==n-1) return;
        cnt+=i-1;
    }
    ans=min(ans,cnt);;
}
int main()
{
    scanf("%s",a+1);
    bool fg=0;
    for(int i=1; i<=strlen(a+1); i++)
    {
        f[a[i]-'0']++;
    }
    if(f[0]>=2)
    {
        pd(0,0);
        fg=true;
    }
    if(f[5]>=1&&f[2]>=1)
    {
        pd(2,5);
        fg=true;
    }
    if(f[5]>=1&&f[7]>=1)
    {
        pd(7,5);
        fg=true;
    }
    if(f[5]>=1&&f[0]>=1)
    {
        pd(5,0);
        fg=true;
    }
    if(fg)
        printf("%d\n",ans);
    else
        printf("-1\n");
}
View Code

 

 F  还没补emmmmm  告辞

 

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

posted @ 2018-06-02 15:35  MengX  阅读(286)  评论(0编辑  收藏  举报

梦想不是空想