• D - Snuke Panic (1D)


    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 400 points

    Problem Statement

    Takahashi is trying to catch many Snuke.

    There are five pits at coordinates 00, 11, 22, 33, and 44 on a number line, connected to Snuke's nest.

    Now, NN Snuke will appear from the pits. It is known that the ii-th Snuke will appear from the pit at coordinate X_iXi​ at time T_iTi​, and its size is A_iAi​.

    Takahashi is at coordinate 00 at time 00 and can move on the line at a speed of at most 11.
    He can catch a Snuke appearing from a pit if and only if he is at the coordinate of that pit exactly when it appears.
    The time it takes to catch a Snuke is negligible.

    Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.

    Constraints

    • 1 \leq N \leq 10^51≤N≤105
    • 0 < T_1 < T_2 < \ldots < T_N \leq 10^50
    • 0 \leq X_i \leq 40≤Xi​≤4
    • 1 \leq A_i \leq 10^91≤Ai​≤109
    • All values in input are integers.

    Input

    Input is given from Standard Input in the following format:

    NN
    T_1T1​ X_1X1​ A_1A1​
    T_2T2​ X_2X2​ A_2A2​
    \vdots⋮
    T_NTN​ X_NXN​ A_NAN​
    

    Output

    Print the answer as an integer.


    Sample Input 1 Copy

    Copy

    3
    1 0 100
    3 3 10
    5 4 1
    

    Sample Output 1 Copy

    Copy

    101
    

    The optimal strategy is as follows.

    • Wait at coordinate 00 to catch the first Snuke at time 11.
    • Go to coordinate 44 to catch the third Snuke at time 55.

    It is impossible to catch both the first and second Snuke, so this is the best he can.


    Sample Input 2 Copy

    Copy

    3
    1 4 1
    2 4 1
    3 4 1
    

    Sample Output 2 Copy

    Copy

    0
    

    Takahashi cannot catch any Snuke.


    Sample Input 3 Copy

    Copy

    10
    1 4 602436426
    2 1 623690081
    3 3 262703497
    4 4 628894325
    5 3 450968417
    6 1 161735902
    7 1 707723857
    8 2 802329211
    9 0 317063340
    10 2 125660016
    

    Sample Output 3 Copy

    Copy

    2978279323

    设f[i][j]表示最大值的一个集合, i表示第i秒, j表示第 j 个坑。

    则有状态转移方程f[i][j] = a[i][j] + f[i - 1][j], 在原地不动;

    f[i][j] = max(f[i][j], a[i][j] + f[i - 1][j - 1]), 从f[i - 1][j - 1] 得来

    f[i][j] = max(f[i][j], a[i][j] + f[i - 1][j + 1]),向后退, 从f[i - 1][j + 1] 得来。

    Code:

    1. #pragma GCC optimize(1)
    2. #pragma GCC optimize(2)
    3. #pragma GCC optimize(3, "Ofast", "inline")
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. using namespace std;
    14. const int N = 100010;
    15. typedef long long LL;
    16. LL n, m, res;
    17. LL a[N][6], f[N][6];
    18. unordered_map<int, int> mp;
    19. signed main()
    20. {
    21. ios_base::sync_with_stdio(false);
    22. cin.tie(0); cout.tie(0);
    23. cin >> n;
    24. for(int i = 1; i <= n; i ++ )
    25. {
    26. LL t, x, d;
    27. cin >> t >> x >> d;
    28. a[t][x] = d;
    29. }
    30. memset(f, -0x3f, sizeof f);
    31. f[0][0] = 0;
    32. for(int i = 1; i <= 100000; i ++ )
    33. {
    34. for(int j = 0; j <= 4; j ++ )
    35. {
    36. f[i][j] = a[i][j] + f[i - 1][j];
    37. if(j > 0) f[i][j] = max(f[i][j], a[i][j] + f[i - 1][j - 1]);
    38. if(j < 4) f[i][j] = max(f[i][j], a[i][j] + f[i - 1][j + 1]);
    39. }
    40. }
    41. for(int i = 0; i <= 4; i ++ )
    42. res = max(res, f[100000][i]);
    43. cout << res << endl;
    44. }

  • 相关阅读:
    多媒体展厅总包制作会面临的问题分析
    物联网 嵌入式 单片机 毕设如何选题 【项目分享】
    一文读懂OpenAI文生视频模型Sora原理
    Redis——》数据类型:list(列表)
    图像处理:双边滤波
    探索机器学习的无限可能性:从初学者到专家的旅程
    【Pytorch Lighting】第 3 章:使用预训练模型进行迁移学习
    GAN论文精读 P2GAN: Posterior Promoted GAN 用鉴别器产生的后验分布来提升生成器
    spark导入elasticsearch
    2023应届毕业生前端面试总结
  • 原文地址:https://blog.csdn.net/qq_62343171/article/details/126641629