您的位置:首页 > 其它

【dp 挺棒的】URAL - 1260 Nudnik Photographer

2018-03-10 21:03 435 查看
Problem Description

有n个数 1-n,将它们排成一列,最左边必须是1,后面排序的数字与它相邻数字的差不能大于2

问你有多少种排列方式

思路:

这种类型的题,自己还是想不出,参考别人博客理解了。

别人博客的思路很完美,直接用了。

n个人时求dp
。第2个位置放2时有dp[n-1]种;第2个位置放3,第3个位置放2,第4个位置只能放4,有dp[n-3]种;第2个位置放3,第3个位置放5,13578642,有1种;第2个位置放3,第3个位置不能放4。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[100];
int main()
{
int n;
dp[1] = dp[2] = 1;
dp[3] = 2;
for(int i = 4; i <= 55; i++)
{
dp[i] = dp[i-1] + dp[i-3] + 1;
}
while(cin >> n)
{
cout << dp
<< endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: