A.1 | B | C.2 | D.3 | ||||
C. Yet Another Tournament
贪心,dp思维(与当前同等x比较)
![](https://i-blog.csdnimg.cn/blog_migrate/c38b983c20557cc46bdc7d5a266acb87.png)
![](https://i-blog.csdnimg.cn/blog_migrate/695c088555110b23d5fb995089724e1c.png)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (auto &x : a) cin >> x;//其他n人:依据下标而非大小
auto b = a;
sort(b.begin(), b.end());//我:依据大小->sort
int ans = 0;
for (int i = 0; i < n && b[i] <= m; i++) {//x与当前比较:动态思想
m -= b[i];
++ans;
}
if (ans != 0 && ans != n && m + b[ans-1] >= a[ans]) ++ans;//m & ans不要轻易自增/自反等->改变自身值,引发后续逻辑错误
cout << n + 1 - ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
}
D. Different Arrays
线性dp:数组的不同由i操作下,i+1和i+2决定 & mod四则运算法则
![](https://i-blog.csdnimg.cn/blog_migrate/382c5f4203a44f9f38be3b2f54441821.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a1d3f449a420d805e920c049ab00087e.png)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 305;
ll fx[N][N * N * 2];//ai的最大值300 * n的最大值300:ai的范围
void solve() {
int n;
cin >> n;
int a[N];
ll*f[N];
for (int i = 1; i <= n; i++) cin >> a[i];
const int d = 300 * 300;
for (int i = 1; i <= 300; i++) f[i] = fx[i] + d;//数组指针+偏移量:解决二维数组下标为负数问题
f[1][a[2]] = 1;
for (int i = 2; i <= n-1; i++) {//线性dp:dp[i][j] = 第i条操作,对a[i+1],a(i+1)+1变化后,前缀数组的数量(决定不同数组的数量)
for (int j = -d; j <= d; j++) {
int t = j - a[i+1];//t:前一个过程中,a[i+1]的变化
if (t >= -d && t <= d) {
if (t == 0) f[i][j] = f[i-1][t];
else f[i][j] = (f[i-1][t] + f[i-1][-t]) % mod;
}
}
}
ll ans = 0;
for (int j = -d; j <= d; j++) ans+= (f[n-1][j] % mod);
cout << ans % mod << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
t = 1;
while (t--) {
solve();
}
}