cin >> T; while (T--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int res = n; for (int i = 0; i < n; i++) { int ans = i; for (int j = i; j < n; j++) if (a[j] > a[i]) ans++; res = min(res, ans); } cout << res << endl; } return0; }
C. Add Zeros
\(a_i=|a|+1-i\ ==>\ |a|=a_i+i-1\)
考虑建图,对于下标 \(i\), 执行操作后,数组长度更新为 \(|a| + i - 1\)。因此,可以把 \(i\) 与所有能在新数组长度上执行操作的下标进行连边。
#define int long long #define x first #define y second #define endl '\n'
usingnamespace std;
constint N = 300010;
int T; int n; int a[N]; vector<int> h[N]; bool st[N];
intdfs(int u) { st[u] = true; int res = a[u] + (u - 1) * 2; for (int i = 0; i < h[u].size(); i++) { if (st[h[u][i]]) continue; res = max(res, dfs(h[u][i])); } return res; }
signedmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; map<int, vector<int>> p; for (int i = 1; i <= n; i++) p[a[i] + i - 1].push_back(i); for (int i = 1; i <= n; i++) { if (a[i] == n - i + 1) h[0].push_back(i); h[i] = p[a[i] + (i - 1) * 2]; } a[0] = n + 2; cout << dfs(0) << endl; for (int i = 0; i <= n; i++) { st[i] = false; h[i].clear(); } } return0; }
cin >> T; while (T--) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) cin >> b[i]; int m1 = 0, m2 = 0; for (int i = 1; i <= n; i++) { m1 = max(m1, a[i]); a[i] += a[i - 1]; } for (int i = 1; i <= m; i++) m2 = max(m2, b[i]); if (m1 > m2) { cout << -1 << endl; continue; } vector<vector<int>> f(m + 10, vector<int>(n + 10)); for (int i = 1; i <= n; i++) f[0][i] = 1e18; for (int i = 1; i <= m; i++) { for (int l = 0, r = 1; r <= n; r++) { f[i][r] = f[i - 1][r]; while (a[r] - a[l] > b[i] && l < r) l++; if (l < r) f[i][r] = min(f[i][r], f[i][l] + m - i); } } cout << f[m][n] << endl; }