问一下记忆化搜索能用滚动数组吗
查看原帖
问一下记忆化搜索能用滚动数组吗
383782
StarPatrick楼主2021/4/28 13:57

这里定义不了太大的记忆化数组,只能交上去TLE80分

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int l, n, b;
struct e
{
	int x, w, f, c;
}a[10005];
void qr(int &ret)
{
    int x=0,f=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')f|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    ret=f?-x:x;
}
bool cmp(e x, e y)
{
	if (x.x>=y.x)
	{
		return false;
	}
	else
	{
		return true;
	}
}
int dfs(int i, int last, int value)
{
	if (last==l)
	{
		return 0;
	}
	if (i==n+1)
	{
		return -2147483647;
	}
	if (a[i].x==last&&value+a[i].c<=b)
	{
		return max(dfs(i+1, last+a[i].w, value+a[i].c)+a[i].f, dfs(i+1, last, value));
	}
	else if (a[i].x>last)
	{
		return -2147483647;
	}
	return dfs(i+1, last, value);
}
int main()
{
	cin>>l>>n>>b;
	for (int p=1;p<=n;++p)
	{
		qr(a[p].x);
		qr(a[p].w);
		qr(a[p].f);
		qr(a[p].c);
	}
	sort(a+1, a+n+1, cmp);
	int u = dfs(1, 0, 0);
	if (u<0)
	{
		cout<<-1;
		return 0;
	}
	cout<<u;
	return 0;
}
2021/4/28 13:57
加载中...