ural 1260. Nudnik Photographer 规律dp

点击打开链接

1260. Nudnik Photographer

Time limit: 1.0 second

Memory limit: 64 MB

If two people were born one after another with one second difference and one of them is a child, then the other one is a child too. We get by induction that all the people are children.

Everyone knows that the mathematical department of the Ural State University is a big family of Npersons, 1, 2, 3, …, N years old respectively.

Once the dean of the department ordered a photo if his big family. There were to be present all the students of the department arranged in one row. At first the dean wanted to arrange them by their
age starting from the youngest student, but than he decided that it would look unnatural. Than he advised to arrange the students as follows:

  1. The 1 year old student is to sit at the left end of the row.
  2. The difference in ages of every two neighbors mustn’t exceed 2 years.

The dean decided that thereby the students would seem look as they were arranged by their ages (one can hardly see the difference in ages of 25 and 27 years old people). There exist several arrangements
satisfying to the requirements. Photographer didn’t want to thwart dean’s desire and made the photos of all the possible mathematical department students’ arrangements.

Input

There is the integer number N, 1 ≤ N ≤ 55.

Output

the number of photos made by the photographer.

Sample

input output
4
4

Hint

If N = 4 then there are following possible arrangements: (1,2,3,4), (1,2,4,3), (1,3,2,4) and (1,3,4,2).

Problem Author: Alexander Ipatov

Problem Source: Open collegiate programming contest for high school children of the Sverdlovsk region, October 11, 2003

给你1-n这n个数,让他们进行组合排序,要求:

1.1必须放在最左面

2.相邻两个数之间的差不能大于2

求有多少种情况满足要求

设dp[n]表示n个数时符合条件的情况,肯定由dp[n-1]这种情况组成,如果n和n-1相邻且在最后的两个位置的话,可以额外增加一种符合条件的方案,这种情况的数量就是dp[n-3],最后需要额外增加1种情况。

//0.015	202 KB
#include<stdio.h>
#include<string.h>
#define ll long long
using namespace std;
ll dp[70];
int main()
{
    int n;
    memset(dp,0,sizeof(dp));
    dp[1]=1;
    dp[2]=1;
    dp[3]=2;
    for(int i=4;i<=55;i++)
        dp[i]=dp[i-1]+dp[i-3]+1;
    while(scanf("%d",&n)!=EOF)
        printf("%lld\n",dp[n]);
    return 0;
}

时间: 2024-10-19 16:07:57

ural 1260. Nudnik Photographer 规律dp的相关文章

URAL 1260 Nudnik Photographer DFS DP

题目:click here :这个题可以先dfs深搜下,规律dp dfs: 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define S second 4 typedef long long ll; 5 const int INF = 0x3f3f3f3f; 6 const int M = 1e5+3; 7 8 int s; 9 int cnt = 0; 10 bool vis[60]; 11 void dfs( int last,

递推DP URAL 1260 Nudnik Photographer

题目传送门 1 /* 2 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e4 + 10; 11 const int INF = 0x3f3f3f3f; 12 int

Ural 1260 A nudnik photographer(DP)

A nudnik photographer 大意: 对1到N这些数进行排列,1必须要在最左边,相邻的两个数之间的差值不能超过2,问有多少种排列的方法. 思路: 对座位进行DP,当第一个是1,第二个是2的时候,组合为dp[i-1]:当第一个是1,第二个是3的时候,第三个也确定了是2,组合为dp[i-3]:还有最后一种情况是1357--8642. 所以DP方程为dp[i] = dp[i-1]+dp[i-3]+1. #include <stdio.h> int n; int dp[100]; int

timus 1260. Nudnik Photographer 动态规划

题目传送门 做完这题感觉dp像是一些状态在转移,由一个(多个)目前状态推到后一(多)个状态. 这个题目的意思是n个人要照相,n个人的年龄为1到n.站成一排,规定最左段是1岁的人,相邻两个人的年龄差不会超过2岁.问有多少总排队方案. 解法: 第i个人加入所产生的状态可以从第i-1个人加入产生的状态推过来.我们知道,第i个人至少会与i-1或i-2相邻.因此以[i-1]和[i-2]作为状态.可以分为四种状态: ① [i-1][i-2] 即 i-1,i-2相邻且i-1在i-2前 ②[i-2][i-1]

URAL 1167. Bicolored Horses (DP)

题目链接 题意 :农夫每天都会放马出去,然后晚上把马赶入马厩,于是让马排成一行入马厩,但是不想马走更多的路,所以让前p1匹入第一个马厩,p2匹马入第二个马厩…………但是他不想让他的任何一个马厩空着,所有的马都必须入马厩.有两种颜色的马,如果 i 匹黑马与 j 匹白马同在一个马厩,不愉快系数是 i * j,总系数就是k个系数相加.让总系数最小. 思路 : dp[i][j] 代表的是前 i 个马厩放 j 匹马的最小不愉快系数值. 1 //1167 2 #include <cstdio> 3 #in

URAL 1073 Square Country(DP)

题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 :其实就是求x12+x22+--+Xn2中的最小的n. 1 //1073 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 int a[

HDU 1260:Tickets(DP)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 923    Accepted Submission(s): 467 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

SPOJ GNYR09F 数字上的找规律DP

Problem C      SPOJ GNYR09F dp题,dp可能刚刚开始对大家来说比较难,但是静下心来分析还是比较简单的: dp(i ,j ,k)表示前i个数中,当前累积和为j,末尾数字为k的方案数. 考虑第i个位置的2种情况: 1.放0:dp(i,j,0) = dp(i-1,j,0) + dp(i-1,j,1) 2.放1:dp(i,j,1)= dp(i-1,j,0) 因为每一行最开始的状态均从i=j+1,dp(i,j,0)=0,dp(i,j,1)=1开始的,但因为这样子开头均为1,那些

URAL——DFS找规律——Nudnik Photographer

Description If two people were born one after another with one second difference and one of them is a child, then the other one is a child too. We get by induction that all the people are children. Everyone knows that the mathematical department of t