菜鸡只配做水题
思路就很简单嘛:肯定扩展的越靠后边越好了
0 0 1 0 1 1 0 0
假设范围是3 ,第一个1一定要选上,第2、3个肯定选3啦,越靠后边就一定能节省更多的点,没看出来和子问题有什么联系,基本上就是贪心
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define x first #define y second using namespace std; const int maxn = 1005; int a[maxn]; vector<int> q; int main() { //freopen("in.txt","r",stdin); int n,k; scanf("%d %d",&n,&k); int cnt = 0; q.push_back(0); for(int i = 1; i <= n; ++i) { scanf("%d",a+i); if(a[i] == 1){ q.push_back(i); cnt ++; } } int cn = 0; int pos = 0; int ans = 0; while(cn < n) { int i; for(i = pos + 1; i < q.size(); ++i) { if(cn < q[i]-(k-1)-1) break; } if(i == pos + 1) { cout << -1 << endl; return 0; } pos = i-1; cn = q[pos]+(k-1); ans ++; } cout << ans << endl; }