小红书9.19笔试
1.ac
#include <iostream> using namespace std; int main() { int a1, a2, a3, a4, x; cin >> a1 >> a2 >> a3 >> a4 >> x; int good1 = a1, good2 = a2, good3 = a3, good4 = a4; int tmp = 0; while (a1--) { cin >> tmp; if (tmp <= x) good1--; } while (a2--) { cin >> tmp; if (tmp <= x) good2--; } while (a3--) { cin >> tmp; if (tmp <= x) good3--; } while (a4--) { cin >> tmp; if (tmp <= x) good4--; } int res = min(good1, min(good2, min(good3, good4))); cout << res; return 0; }
2.动态规划(ac)
#include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> high(n); for (int i = 0; i < n; i++) cin >> high[i]; vector<int> dp(n, 200000); dp[0] = 0; int cost = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= k && i - j >= 0; j++) { cost = max(high[i] - high[i - j], 0); dp[i] = min(dp[i], dp[i - j] + cost); } //cout << dp[i]; } cout << dp.back(); return 0; }
3.ac72%
#include <iostream> #include <vector> #include <unordered_map> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; int res = 0; unordered_map<int, int> map; for (int i = 0; i < n - k + 1; i++) { map.clear(); map[nums[i]]++; for (int j = i + 1; j < n; j++) { if (++map[nums[j]] >= k) { res += (n - j); break; } } } cout << res; return 0; }