#include <iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 5e4+10;
char s[N];
int t;
LL f[N][2];
int main() {
scanf ("%d",&t);
while (t--)
{
memset(f,0,sizeof f);
int n;
scanf ("%d",&n);
f[0][0] = 0;
f[0][1] = -1e18;
for (int i=1;i<=n;i++)
{
int x;
scanf ("%s%d",s,&x);
if (s[0] == 'L')
{
f[i][0] = max(0LL,f[i-1][0]-x);
f[i][1] = max(f[i-1][0],max(f[i-1][1]-x,0LL));
}
else
{
f[i][0] = f[i-1][0]+x;
f[i][1] = f[i-1][1] +x;
}
}
printf ("%lld\n",max(f[n][0],f[n][1]));
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 3e5 + 10;
int a[N], b[N], c[N], d[N];
int f[N];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(f, 0, sizeof f);
int n, h;
scanf("%d%d", &n, &h);
for (int i=1;i<=n;i++)
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
int res = -1;
for (int i = 1; i <= n; i++)
{
for (int j = h; j; j--)
{
int t = 0;
if (j >= a[i] && (f[j - a[i]] != 0||i==1)) t = f[j - a[i]] + b[i];
if (j >= c[i] && (f[j - c[i]] != 0||i==1)) t = max(t,f[j - c[i]] + d[i]);
f[j] = t;
res = max(res, t);
}
}
cout << res << endl;
}
return 0;
}
/*
1
4 9
3 1 3 2
2 2 2 2
4 3 3 1
2 4 2 1
*/
#include <iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a[17];
int x[] = {1,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,5};
int y[] = {1,2,4,5,2,3,4,2,3,4,2,3,4,1,2,4,5};;
int mp[7][7];
int dx[4] = {-1,0,1,0},dy[4]={0,1,0,-1};
int tr,mh;
long long sum;
void init()
{
memset(mp,-1,sizeof mp);
for (int i=0,k=0;i<17;i++)
{
mp[x[i]][y[i]] = k++;
}
}
int cul(int x)
{
int res = 0;
while (x)
{
if (x&1)
res++;
x>>=1;
}
return res;
}
void dfs(int xx,int yy)
{
int idx = mp[xx][yy];
if (idx==-1||tr>>idx&1||!(mh>>idx&1)) return;//(mh>>idx)%2==0;
sum+=a[idx];
tr|=1<<idx;
for (int i=0;i<4;i++)
{
dfs(xx+dx[i],yy+dy[i]);
}
}
int main() {
init();
int t;
scanf ("%d",&t);
while (t--)
{
for (int i=0;i<17;i++){
scanf ("%d",&a[i]);
a[i] = a[i]/6+1;
}
int n;
scanf ("%d",&n);
int res = 0;
for (mh=0;mh<1<<17;++mh)
{
if (mh>>13&1==0) continue;
sum=tr=0;
dfs(x[13],y[13]);
if (sum<=n&&tr==mh){
res = max(res,cul(tr));
}
}
cout<<res<<endl;
}
return 0;
}
附赠一道刷到的有意思的DP题,当决策条件间相互独立不影响时,可以分开决策,分别进行多次转移。
守望者的逃离题目链接+++++++++++++++++++++++++++++++++