A. Diverse Team
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 (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 kk distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them.
The first line contains two integers nn and kk (1≤k≤n≤100) — the number of students and the size of the team you have to form.
The second line contains nn integers a1,a2,…,an (1≤ai≤100), where aiai is the rating of ii-th student.
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.
5 3
15 13 15 15 12
YES
1 2 5
5 4
15 13 15 15 12
NO
4 4
20 10 40 30
YES
1 2 3 4
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 "); for(int i=1;i<=100&&m;i++) { if(pos[i]) { printf("%d ",pos[i]); m--; } } } else printf("NO "); return 0; }
B. Substrings Sort
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".
The first line contains an integer nn (1≤n≤100) — 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.
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.
5
a
aba
abacaba
ba
aba
YES
a
ba
aba
aba
abacaba
5
a
abacaba
ba
aba
abab
NO
3
qwerty
qwerty
qwerty
YES
qwerty
qwerty
qwerty
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; }
C. Equal Sums
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 (i≠ji≠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 ni−1ni−1) equals to the sum of the changed sequence jj (its length will be equal to nj−1nj−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.
The first line contains an integer kk (2≤k≤2⋅105) — 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 (1≤ni<2⋅105) — 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 2⋅105, i.e. n1+n2+⋯+nk≤2⋅105.
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 (1≤i≤k,1≤x≤ni1≤i≤k,1≤x≤ni), in the third line — two integers jj, yy (1≤j≤k,1≤y≤nj1≤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. i≠ji≠j. You can print them in any order.
If there are multiple possible answers, print any of them.
2
5
2 3 1 3 2
6
1 1 2 2 2 1
YES
2 6
1 2
3
1
5
5
1 1 1 1 1
2
2 3
NO
4
6
2 2 2 2 2 2
5
2 2 2 2 2
3
2 2 2
5
2 2 2 2 2
YES
2 2
4 1
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 "); printf("%d %d %d %d ",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 "); return 0; }
D. Points and Powers of Two
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 |xij−xik|=2d where dd is some non-negative integer number (not necessarily the same for each pair of points).
The first line contains one integer nn (1≤n≤2⋅105) — the number of points.
The second line contains nn pairwise distinct integers x1,x2,…,xn (−109≤xi≤109) — the coordinates of points.
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.
6
3 5 4 7 10 12
3
7 3 5
5
-1 2 5 8 11
1
8
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.
题意:求一个集合中的任意两个差为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 %lld %lld %lld ",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 %lld %lld ",a[i],a[i]+1LL*(1<<j)); return 0; } } } printf("1 %lld ",a[1]); }
E. Divisibility by 25
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.
The first line contains an integer nn (1≤n≤1018). It is guaranteed that the first (left) digit of the number nn is not a zero.
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.
5071
4
705
1
1241367
-1
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 ",ans); else printf("-1 "); }
F 还没补emmmmm 告辞
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~