【DP】Block Sequence—CF1881E

Block Sequence—CF1881E

赛时剩的时间不多,没想到dp。

C o d e Code Code

#include 
#define int long long
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
using PII = pair<int, int>;
using i128 = __int128;
const int N = 2e5 + 10;

int n;
int a[N];
int f[N];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	f[n + 1] = 0;
	for (int i = n; i >= 1; i --) {
		f[i] = f[i + 1] + 1;
		if (i + a[i] <= n) {
			f[i] = min(f[i], f[i + a[i] + 1]);
		}
	}
	
	cout << "          ";
	cout << f[1] << "\n";
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T = 1;
	cin >> T; cin.get();
	while (T --) solve();
	return 0;
}

你可能感兴趣的:(DP,算法,c++)