zoukankan      html  css  js  c++  java
  • Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/480/problem/C

    Description

    Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.

    Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y ≠ x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.

    Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).

    Input

    The first line of the input contains four space-separated integers n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).

    Output

    Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).

    Sample Input

    5 2 4 1

    Sample Output

    2

    HINT

    题意

    有n层楼,你一开始在a层楼,实验室在b层楼,然后你可以瞎走k次

    每次只要满足|now-next|<|now-b|就可以从now走到next去

    然后问你一共有多少种走法

    题解:

    最简单就是dp[i][j]表示我现在在第i层,瞎走了j次的方案数,但是这个转移是n^3的

    我们得优化一下

    每次我们可以看到,从上一个状态转移过来的是一个区间,所以大概用一个线段树优化成n^2logn或者用前缀和优化成n^2的就行了

    代码:

    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    using namespace std;
    #define maxn 5005
    #define mod 1000000007
    int dp[maxn][maxn];
    int sum[maxn];
    int main()
    {
        int n,a,b,k;
        scanf("%d%d%d%d",&n,&a,&b,&k);
        dp[a][0]=1;
        for(int i=1;i<=n;i++)
            sum[i]=dp[i][0]+sum[i-1];
        for(int i=1;i<=k;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(j==b)continue;
                int L,R;
                if(j<b)
                {
                    L = 1,R = (j+b)/2;
                    if(R-j==b-R)R--;
                }
                else
                {
                    L = (j+b)/2+(j+b)%2;R=n;
                    if(j-L==L-b)L++;
                }
                L=max(1,L);R=min(n,R);
                dp[j][i]=((sum[R]-sum[L-1]+mod)%mod-dp[j][i-1]+mod)%mod;
            }
            for(int j=1;j<=n;j++)
            {
                sum[j]=dp[j][i]+sum[j-1];
                while(sum[j]>=mod)sum[j]-=mod;
            }
        }
        printf("%d
    ",sum[n]);
    }
  • 相关阅读:
    http uri唯一标识
    http协议
    python模块 sys
    file 文件的操作
    库的介绍及使用
    python os模块
    python的序列化与反序列化
    python 字典的定义以及方法
    python字符串的常用方法
    在Windows下使用adb logcat grep
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4904749.html
Copyright © 2011-2022 走看看