P3662 [USACO17FEB] Why Did the Cow Cross the Road II S(前缀和+枚举)

思路:前缀和+枚举(看代码挺好理解滴)

ACcode:


#include
using namespace std;
#define int long long
const int N=1e5+10;
int n,b,k,a[N],pre[N];
void solve() {
	cin>>n>>k>>b;
	while(b--) {
		int x;
		cin>>x;
		a[x]=1;
	}
	for(int i=1; i<=n; i++) {
		pre[i]=pre[i-1]+a[i];
	}
	int mmin=1<<30;
	for(int i=k; i<=n; i++) {
		mmin=min(mmin,pre[i]-pre[i-k]);//***
	}
	cout<>t;
	while(t--) {
		solve();
	}
	return 0;
}



over~

你可能感兴趣的:(算法,c++,前缀和,枚举)