zoukankan      html  css  js  c++  java
  • Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )

    A. Mr. Kitayuta, the Treasure Hunter
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi.

    Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process:

    • First, he will jump from island 0 to island d.
    • After that, he will continue jumping according to the following rule. Let l be the length of the previous jump, that is, if his previous jump was from island prev to island cur, let l = cur - prev. He will perform a jump of length l - 1, l or l + 1 to the east. That is, he will jump to island (cur + l - 1), (cur + l) or (cur + l + 1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when l = 1. If there is no valid destination, he will stop jumping.

    Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.

    Input

    The first line of the input contains two space-separated integers n and d (1 ≤ n, d ≤ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.

    The next n lines describe the location of the gems. The i-th of them (1 ≤ i ≤ n) contains a integer pi (d ≤ p1 ≤ p2 ≤ ... ≤ pn ≤ 30000), denoting the number of the island that contains the i-th gem.

    Output

    Print the maximum number of gems that Mr. Kitayuta can collect.

    这个题一开始就想到DP了 。dp[i][j]表示跳到了下标 i 上次跳了 j 个长度的最大得分。

    但是d的范围有点大 , 我试了一下开二维记忆化搜索死机了。

    后来大洲爷说了一下思路 , d <= 2000  dp , 否则 dfs , d > 2000的话层数应该10来层就OK了吧 。

    其几天考试没时间写,刚考完才有时间补一下题来着。 一写就过了~

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define root 1,n,1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define lr rt<<1
    #define rr rt<<1|1
    typedef long long LL;
    const int oo = 1e9+7;
    const double PI = acos(-1.0);
    const double eps = 1e-6 ;
    const int N = 30010;
    const int mod = 2333333;
    int n , d , x , dp[N][2010] , cnt[N];
    
    int dfs( int i , int l ) {
        if( i > x ) return 0 ;
        int tmp = cnt[i];
        if( l > 1 ) tmp = max( tmp , cnt[i] + dfs( i + l - 1 , l - 1 ) );
        tmp = max( tmp , cnt[i] + dfs( i + l , l ) );
        tmp = max( tmp , cnt[i] + dfs( i + l + 1 , l + 1 ) );
        return tmp ;
    }
    
    int DP( int i , int l ) {
        if( i > x ) return 0 ;
        if( dp[i][l] == -1 ) {
            int &tmp = dp[i][l] ; tmp = cnt[i];
            if( l > 1 ) tmp = max( tmp , cnt[i] + DP( i + l - 1 , l - 1 ) );
            tmp = max( tmp , cnt[i] + DP( i + l , l ) );
            tmp = max( tmp , cnt[i] + DP( i + l + 1 , l + 1 ) );
        }
        return dp[i][l];
    }
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
    //        freopen("out.txt","w",stdout);
        #endif // LOCAL
        ios::sync_with_stdio(false);
        while( cin >> n >> d ) {
            memset( cnt , 0 , sizeof cnt ) ;
            memset( dp , -1 , sizeof dp ) ;
            for( int i = 0 ; i < n ; ++i ) {
                cin >> x ; cnt[x]++;
            }
            if( d > 2000 ) cout << dfs( d , d ) << endl ;
            else cout << DP(d,d) << endl ;
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    ibmmq 性能测试
    zabbix-agent 安装
    关于dubbo接口性能测试
    关于vyos 防火墙配置
    appium自动化的工作原理(1)
    unittest如何在循环遍历一条用例时生成多个测试结果
    在Linux中#!/usr/bin/python之后把后面的代码当成程序来执行。 但是在windows中用IDLE编程的话#后面的都是注释,之后的代码都被当成文本了。 该怎么样才能解决这个问题呢?
    Cookie和Session的区别详解
    点单登录原理和java实现简单的单点登录
    new一个JAVA对象的时候,内存是怎么分配的?
  • 原文地址:https://www.cnblogs.com/hlmark/p/4237481.html
Copyright © 2011-2022 走看看