生活随笔
收集整理的這篇文章主要介紹了
免费馅饼 逆向dp
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
傳送門
給你n個餡餅,之后給出n個餡餅掉落時間和位置。一共有11個位置從 0 ~ 10,這人初始在第5個位置,讓后每一秒最多能移動一個單位,問這個人能最多能拿多少餡餅。
思路:
有點像數(shù)字三角形,但是需要逆著推,保證沒有后效性,最后f[0][5]就是答案。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std
;void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL
)); }
void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<int,int> PII
;const int N
=100010,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;int n
,x
,t
;
int f
[N
][11];int main()
{
while(scanf("%d",&n
)!=EOF&&n
){int mx
=0;memset(f
,0,sizeof(f
));for(int i
=1;i
<=n
;i
++){int x
,t
; scanf("%d%d",&x
,&t
);f
[t
][x
]++; mx
=max(mx
,t
);}for(int i
=mx
;i
>=0;i
--)for(int j
=0;j
<=10;j
++) f
[i
][j
]+=max(f
[i
+1][j
],max(f
[i
+1][max(j
-1,0)],f
[i
+1][min(j
+1,10)]));printf("%d\n",f
[0][5]);}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的免费馅饼 逆向dp的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。