|NOIOJ|动态规划|8462:大盗阿福

http://noi.openjudge.cn/ch0206/8462/

f[i] = max(f[i-1], f[i-2]+a[i])

f[i-1]表示不抢这家店,f[i-2]+a[i]表示抢这家店

#include 
#include 
#include 
#define ms(i,j) memset(i, j, sizeof(i));
typedef long long ll;
using namespace std;
int f[100005];
int a[100005];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		for (int i=1;i<=n;i++) {scanf("%d", &a[i]); f[i] = a[i]; }
		for (int i=2;i<=n;i++)
		{
			f[i] = max(f[i-1], f[i-2]+a[i]);
		}
		printf("%d\n", f[n]);
	}
	return 0;
}


你可能感兴趣的:(OpenJudge,Noi,动态规划)