2月23日两题
The First:https://codeforces.com/contest/2069/problem/C
思路:
找出以1和3围起来的2的组合数量。
1223
代码:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
int c1 = 0;
int c2 = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 1) c1++;
else if (x == 2) c2 = (2LL * c2 + c1) % MOD;
else ans = (ans + c2) % MOD;
}
cout << ans << '\n';
}
}
Second:https://leetcode.cn/problems/maximum-sum-with-at-most-k-elements/solutions/3086172/cong-da-dao-xiao-tan-xin-pythonjavacgo-b-vjt2/
思路:
取出每行的相较大的值,之后将所有值排序取k个大值,返回这些值的和。
代码:
class Solution {
public:
long long maxSum(vector<vector<int>>& grid, vector<int>& limits, int k) {
if (k == 0) {
return 0;
}
vector<int> a;
for (int i = 0; i < grid.size(); i++) {
sort(grid[i].begin(), grid[i].end());
for (int j = grid[i].size() - 1, p = 1; p <= limits[i]; j--, p++) {
a.push_back(grid[i][j]);
}
}
sort(a.begin(), a.end());
long long ans = 0;
for (int i = 0; i < k; i++) {
ans += a[a.size() - 1 - i];
}
return ans;
}
};