A Diverse Team
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.
题目类型:排序+简单枚举
算法分析:可以使用unique并重载'=='运算符来快速求解出序列中的不同元素的个数和下标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/************************************************** filename :a.cpp author :maksyuki created time :2018/6/4 5:52:53 last modified :2018/6/4 19:22:59 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 666; struct node { int a, b; bool operator <(const node &aa) const { return a < aa.a; } bool operator ==(const node &aa) const { return a == aa.a; } }; node aa[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, k; while(cin >> n >> k) { for(int i = 1; i <= n; i++) { cin >> aa[i].a; aa[i].b = i; } sort(aa + 1, aa + 1 + n); int v = unique(aa + 1, aa + 1 + n) - (aa + 1); if(v >= k) { cout << "YES" << endl; for(int i = 1; i <= k; i++) { if(i == 1) cout << aa[i].b; else cout << " " << aa[i].b; } cout << endl; } else cout << "NO" << endl; } return 0; } |
B Substrings Sort
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 nlines 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".
题目类型:排序+字符串处理
算法分析:子串的长度不会大于母串,所以可以先按照串的长度递增排序,然后依次暴力枚举判断排在其前面串是否是其子串即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
/************************************************** filename :b.cpp author :maksyuki created time :2018/6/1 22:50:49 last modified :2018/6/6 9:40:02 file location :C:\Users\abcd\Desktop\codeforces\test ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 166; int n; bool cmp(const string &a, const string &b) { return a.size() < b.size(); } int main() { #ifdef LOCAL CFF; //CFO; #endif string s[maxn]; while(cin >> n) { for(int i = 1; i <= n; i++) cin >> s[i]; sort(s + 1, s + 1 + n, cmp); bool is_valid = true; for(int j = 2; j <= n; j++) { if(!is_valid) break; for(int i = 1; i <= j - 1; i++) if(s[j].find(s[i]) == string::npos) { is_valid = false; break; } } if(is_valid) { cout << "YES" << endl; for(int i = 1; i <= n; i++) cout << s[i] << endl; } else cout << "NO" << endl; } return 0; } |
C Equal Sums
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⋅10^5) — 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⋅10^5) — 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,n i.
The elements of sequences are integer numbers from −104 to 104.
The sum of lengths of all given sequences don't exceed 2⋅10^5, i.e. n1+n2+⋯+nk ≤ 2⋅10^5.
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.
题目类型:简单排序
算分分析:依次枚举产生一个包含(sum, seq_flag, index_flag)的三元组,然后对该三元组按照sum的大小排序,然后依次判断相邻元素的sum是否相同且不在一个序列中,存在一个即输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
/************************************************** filename :c.cpp author :maksyuki created time :2018/6/6 10:35:07 last modified :2018/6/6 11:00:12 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 2e5 + 6666; int k, n, v[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif while(scanf("%d", &k) != EOF) { vector<pair<int, PII> > ans; for(int i = 1; i <= k; i++) { scanf("%d", &n); for(int j = 1; j <= n; j++) scanf("%d", &v[j]); int sum = accumulate(v + 1, v + 1 + n, 0); for(int j = 1; j <= n; j++) ans.emplace_back(pair<int, PII> (sum - v[j], PII(i, j))); } sort(ans.begin(), ans.end()); int aalen = ans.size(); bool is_find = false; for(int i = 0; i < aalen - 1; i++) { if(ans[i].first == ans[i+1].first && ans[i].second.first != ans[i+1].second.first) { is_find = true; puts("YES"); printf("%d %d\n", ans[i].second.first, ans[i].second.second); printf("%d %d\n", ans[i+1].second.first, ans[i+1].second.second); break; } } if(!is_find) puts("NO"); } return 0; } |
D Points and Powers of Two
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 x
ij, xik it is true that |xij−xik| = 2^d 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⋅10^5) — the number of points.
The second line contains n pairwise distinct integers x1, x2, …, xn(−10^9 ≤ xi ≤10^9) — 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 = 2^2, |7 − 5| = 2 = 2^1 and |3 − 5| = 2 = 2^1
. You can't find a subset having more points satisfying the required property.
题目类型:简单数学
算法分析:由直线上距离有dis(a,c) = dis(a,b) + dis(b,c)可简单通过反证得到答案不会超过3,然后依次判断答案是否是3, 2或者1。判断答案是3可以通过枚举答案中间的数和2的0~30次幂做差和判是否存在得到,2的类似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
/************************************************** filename :d.cpp author :maksyuki created time :2018/6/8 13:56:22 last modified :2018/6/8 14:11:56 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 2e5 + 6666; int main() { #ifdef LOCAL CFF; //CFO; #endif int n; set<int> dict; while(cin >> n) { dict.clear(); int tt; for(int i = 1; i <= n; i++) { cin >> tt; dict.insert(tt); } bool is_three = false; vector<int> ans; for(set<int>:: iterator it = dict.begin(); it != dict.end(); it++) { int tmp = *it; for(int i = 0; i <= 30; i++) if(dict.count(tmp - (1<<i)) && dict.count(tmp + (1<<i))) { is_three = true; ans.emplace_back(tmp - (1<<i)); ans.emplace_back(tmp); ans.emplace_back(tmp + (1<<i)); break; } if(is_three) break; } if(is_three) { int aalen = ans.size(); cout << aalen << endl; for(int i = 0; i < aalen; i++) { if(!i) cout << ans[i]; else cout << " " << ans[i]; } cout << endl; } else { bool is_two = false; for(set<int>:: iterator it = dict.begin(); it != dict.end(); it++) { int tmp = *it; for(int i = 0; i <= 30; i++) if(dict.count(tmp + (1<<i))) { is_two = true; ans.emplace_back(tmp); ans.emplace_back(tmp + (1<<i)); break; } if(is_two) break; } if(is_two) { int aalen = ans.size(); cout << aalen << endl; for(int i = 0; i < aalen; i++) { if(!i) cout << ans[i]; else cout << " " << ans[i]; } cout << endl; } else { cout << "1" << endl; for(set<int>:: iterator it = dict.begin(); it != dict.end(); it++) { cout << *it << endl; break; } } } } return 0; } |
E Divisibility by 25
You are given an integer n from 1 to 10^18 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 n(1 ≤ n ≤ 10^18). It is guaranteed that the first (left) digit of the number
n 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
5071
output
4
input
705
output
1
input
1241367
output
-1
Note
In the first example one of the possible sequences of moves is 5071 → 5701 → 7501 → 7510 →7150.
题目类型:字符串枚举+数学
算法分析:满足能整除25的数的特征是最后两位为00, 25,50和75。每次移动过程当中保证不存在前导零时的移动次数等价于先移动出满足条件的后两位的次数再加上最高那位不为零的数移动到最高位时的次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
/************************************************** filename :e.cpp author :maksyuki created time :2018/6/8 14:37:16 last modified :2018/6/8 16:32:38 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int main() { #ifdef LOCAL CFF; //CFO; #endif string sa, s; while(cin >> sa) { int ans = INF; int len = sa.size(); for(int i = 0; i < len; i++) for(int j = i + 1; j < len; j++) { int tt = 0; s = sa; if((s[i] == '0' && s[j] == '0') || (s[i] == '2' && s[j] == '5') || (s[i] == '5' && s[j] == '0') || (s[i] == '7' && s[j] == '5')) { for(int k = j; k < len - 1; k++) { swap(s[k], s[k+1]); tt++; } for(int k = i; k < len - 2; k++) { swap(s[k], s[k+1]); tt++; } int pos = -1; for(int k = 0; k < len; k++) if(s[k] != '0') { pos = k; break; } for(int k = pos; k >= 1; k--) { swap(s[k], s[k-1]); tt++; } ans = min(ans, tt); } else if((s[i] == '5' && s[j] == '2') || (s[i] == '0' && s[j] == '5') || (s[i] == '5' && s[j] == '7')) { for(int k = i; k < len - 1; k++) { swap(s[k], s[k+1]); tt++; } for(int k = j - 1; k < len - 2; k++) { swap(s[k], s[k+1]); tt++; } int pos = -1; for(int k = 0; k < len; k++) if(s[k] != '0') { pos = k; break; } for(int k = pos; k >= 1; k--) { swap(s[k], s[k-1]); tt++; } ans = min(ans, tt); } } if(ans == INF) cout << "-1" << endl; else cout << ans << endl; } return 0; } |