A、Mishka and Contest
思路:简单贪心。每次删掉首尾不超过k的元素,直到分别第一次遇到超过k的元素就不再继续删除即可。
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 = 105; 27 const int inf = 0x3f3f3f3f; 28 29 int n, k, ans, a[maxn]; 30 31 int main() { 32 while(cin >> n >> k) { 33 ans = 0; 34 for(int i = 0; i < n; ++i) cin >> a[i]; 35 for(int i = 0, j = n - 1; i <= j;) { 36 if(a[i] <= k) ++i, ++ans; 37 else if(a[j] <= k) --j, ++ans; 38 else break; 39 } 40 cout << ans << endl; 41 } 42 return 0; 43 }
B、Reversing Encryption
思路:暴力。简单按照n的所有公约数从小到大有序反转一下即可。
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; 30 string str; 31 32 int main() { 33 while(cin >> n >> str) { 34 for(int i = 1; i <= n; ++i) 35 if(n % i == 0) reverse(str.begin(), str.begin() + i); 36 cout << str << endl; 37 } 38 return 0; 39 }
C、Alphabetic Removals
思路:暴力。关联式容器multimap(键---值:一对多)的简单应用。注意:map.erase(迭代器)的结果只是被删元素的迭代器失效,但因为其返回值为void,所以要采用map.erase(it++)的方式删除被删元素的迭代器,此时it已经指向下一个迭代器的位置了。
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 = 4e5+5; 27 const int inf = 0x3f3f3f3f; 28 29 int n, k, len; 30 31 multimap<char, int> mp1; 32 multimap<char, int>::iterator it1; 33 34 map<int, char> mp2; 35 map<int, char>::iterator it2; 36 37 char str[maxn]; 38 39 int main() { 40 while(cin >> n >> k >> str) { 41 len = strlen(str); 42 if(k == len) continue; 43 mp1.clear(); mp2.clear(); 44 for(int i = 0; i < len; ++i) mp1.insert(make_pair(str[i], i)); 45 // 注意:mp1.erase(迭代器)的结果只是被删元素的迭代器失效,但因为其返回值为void,所以要采用erase(it++)的方式删除被删元素的迭代器 46 for(it1 = mp1.begin(); k && (it1 != mp1.end()); --k) mp1.erase(it1++); 47 for(it1 = mp1.begin(); it1 != mp1.end(); ++it1) mp2[it1 -> second] = it1 -> first; 48 for(it2 = mp2.begin(); it2 != mp2.end(); ++it2) printf("%c", it2 -> second); 49 puts(""); 50 } 51 return 0; 52 }
原文地址:https://www.cnblogs.com/acgoto/p/10693468.html
时间: 2024-11-15 00:33:54