Codeforces Round #550 (Div. 3)

A、Diverse Strings

思路:简单判断一下给定的字符串是否刚好为一个顺子即可。

AC代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <complex>
 9 #include <string>
10 #include <vector>
11 #include <set>
12 #include <map>
13 #include <list>
14 #include <deque>
15 #include <queue>
16 #include <stack>
17 #include <bitset>
18 using namespace std;
19 typedef long long LL;
20 typedef unsigned long long ULL;
21 const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 上右下左
22 const int mx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; // 马可走的八个方向
23 const int my[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
24 const double eps = 1e-6;
25 const double PI = acos(-1.0);
26 const int maxn = 1e5+5;
27 const int inf = 0x3f3f3f3f;
28 
29 int n, len, now, cnt[30];
30 char str[105];
31 bool flag, first;
32 map<int, int> mp;
33 
34 int main() {
35     while(cin >> n) {
36         while(n--) {
37             cin >> str;
38             memset(cnt, 0, sizeof(cnt));
39             len = strlen(str);
40             flag = first = false;
41             mp.clear();
42             for(int i = 0; i < len; ++i) ++mp[str[i] - 'a'];
43             for(map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
44                 if(it -> second > 1) {flag = true; break;}
45                 if(!first) first = true, now = it -> first;
46                 else if(it -> first - now != 1) {flag = true; break;}
47                 else now = it -> first;
48             }
49             puts(!flag ? "Yes" : "No");
50         }
51     }
52     return 0;
53 }
View Code

B、Parity Alternated Deletions

思路:贪心。能构造出来的奇偶交替序列的长度一定为2*min(num1,num2)+1,剩下的元素一定是多出来的那几个最小(都是奇数或者都是偶数)的元素之和。

AC代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <complex>
 9 #include <string>
10 #include <vector>
11 #include <set>
12 #include <map>
13 #include <list>
14 #include <deque>
15 #include <queue>
16 #include <stack>
17 #include <bitset>
18 using namespace std;
19 typedef long long LL;
20 typedef unsigned long long ULL;
21 const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 上右下左
22 const int mx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; // 马可走的八个方向
23 const int my[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
24 const double eps = 1e-6;
25 const double PI = acos(-1.0);
26 const int maxn = 2005;
27 const int inf = 0x3f3f3f3f;
28 
29 int n, x, ans, num1, num2, sum;
30 vector<int> odd, even;
31 
32 int main() {
33     while(cin >> n) {
34         odd.clear(), even.clear();
35         for(int i = 1; i <= n; ++i) {
36             cin >> x;
37             if(x & 1) odd.push_back(x);
38             else even.push_back(x);
39         }
40         sort(odd.begin(), odd.end());
41         sort(even.begin(), even.end());
42         num1 = odd.size();
43         num2 = even.size();
44         if(abs(num1 - num2) < 2) cout << 0 << endl;
45         else {
46             ans = inf;
47             if(num1 > num2) {
48                 num1 -= num2 + 1, sum = 0;
49                 for(int i = 0; i < num1; ++i) sum += odd[i];
50                 ans = min(ans, sum);
51             }else {
52                 num2 -= num1 + 1, sum = 0;
53                 for(int i = 0; i < num2; ++i) sum += even[i];
54                 ans = min(ans, sum);
55             }
56             cout << ans << endl;
57         }
58     }
59     return 0;
60 }
View Code

C、Two Shuffled Sequences

思路:简单排序。显然,给定序列中每个数字出现的次数不超过2次;然后简单搞一下即可。

AC代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <complex>
 9 #include <string>
10 #include <vector>
11 #include <set>
12 #include <map>
13 #include <list>
14 #include <deque>
15 #include <queue>
16 #include <stack>
17 #include <bitset>
18 using namespace std;
19 typedef long long LL;
20 typedef unsigned long long ULL;
21 const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 上右下左
22 const int mx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; // 马可走的八个方向
23 const int my[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
24 const double eps = 1e-6;
25 const double PI = acos(-1.0);
26 const int maxn = 1e5+5;
27 const int inf = 0x3f3f3f3f;
28 
29 
30 int n, x, pos, num;
31 vector<int> vec, now;
32 map<int, int> mp;
33 bool flag;
34 
35 int main() {
36     while(cin >> n) {
37         vec.clear(), mp.clear(); flag = false; now.clear();
38         for(int i = 0; i < n; ++i) {cin >> x; vec.push_back(x); ++mp[x];}
39         for(map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
40             if(it -> second > 2) {flag = true; break;}
41         }
42         if(flag) {cout << "NO" << endl; continue;}
43         cout << "YES" << endl;
44         sort(vec.begin(), vec.end());
45         now.insert(now.begin(), vec.begin(), vec.end()); // 拷贝一份
46         pos = unique(now.begin(), now.end()) - now.begin(); // 去重 指向第一个重复元素的位置
47         cout << pos << endl;
48         for(int i = 0; i < pos; ++i) cout << now[i] << " 
"[i == pos - 1];
49         cout << n - pos << endl;
50         num = 0;
51         for(map<int, int>::reverse_iterator it = mp.rbegin(); it != mp.rend(); ++it) {
52             if(it -> second == 2) cout << it -> first << " 
"[++num == n - pos];
53         }
54         if(n - pos == 0) cout << endl;
55     }
56     return 0;
57 }
View Code

D、Equalize Them All

思路:贪心。按给定的2种操作使用最少的次数将数组变成n个相同的元素,题目已保证操作后一定能得到n个相同的元素。显然,我们需要将其他元素都变成出现次数最多(cnt个)的数字,这样就要操作n-cnt次,简单搞一下即可。

AC代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <complex>
 9 #include <string>
10 #include <vector>
11 #include <set>
12 #include <map>
13 #include <list>
14 #include <deque>
15 #include <queue>
16 #include <stack>
17 #include <bitset>
18 using namespace std;
19 typedef long long LL;
20 typedef unsigned long long ULL;
21 const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 上右下左
22 const int mx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; // 马可走的八个方向
23 const int my[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
24 const double eps = 1e-6;
25 const double PI = acos(-1.0);
26 const int maxn = 2e5+5;
27 const int inf = 0x3f3f3f3f;
28 
29 int n, val, cnt, pos, a[maxn];
30 map<int, int> mp;
31 
32 int main() {
33     while(cin >> n) {
34         mp.clear();
35         for(int i = 0; i < n; ++i) {
36             cin >> a[i];
37             ++mp[a[i]];
38         }
39         cnt = 1, val = a[0];
40         for(map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
41             if(it -> second > cnt) {
42                 cnt = it -> second, val = it -> first;
43             }
44         }
45         cout << n - cnt << endl;
46         pos = find(a, a + n, val) - a; // 找到第一个val出现的位置pos
47         for(int i = pos + 1; i < n; ++i) {
48             if(a[i] == val) continue;
49             cout << (1 + (a[i] > val)) << ' ' << i + 1 << ' ' << i << endl;
50         }
51         for(int i = pos - 1; ~i; --i) {
52             if(a[i] == val) continue;
53             cout << (1 + (a[i] > val)) << ' ' << i + 1 << ' ' << i + 2 << endl;
54         }
55     }
56     return 0;
57 }
View Code
原文地址:https://www.cnblogs.com/acgoto/p/10646149.html