当前位置:网站首页>Walking "daily question" and "DP"

Walking "daily question" and "DP"

2022-06-12 04:38:00 Yang tree Yang tree

Walk - subject - Daimayuan Online Judge

Ideas :

The first thing I thought of was DFS, however 2^100 necessarily TLE;

And then I think I can't , Have to use DP,DP Always consider the previous state 「 Have you ever walked ?」&&「 Is that enough? I'll go 」

AC Code :

#include <iostream>
#include <string.h>
using namespace std;
const int N = 1e5+10;
int A[N],B[N];
int ans[101][N];
int main() {
	int n,m;
	memset(ans, 0, sizeof(ans));
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>A[i]>>B[i];
	ans[1][A[1]]=1;
	ans[1][B[1]]=1;
	ans[0][0]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if( j>=A[i] && ans[i-1][j-A[i]] ) ans[i][j]=1;
			if( j>=B[i] && ans[i-1][j-B[i]] ) ans[i][j]=1;
		}
	for(int i=0;i<=m;i++)
	{
		if(ans[n][i]==1) cout<<"1";
		else cout<<"0";
	}
	return 0;
}

原网站

版权声明
本文为[Yang tree Yang tree]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010941289931.html