码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • TOYOTA MOTOR CORPORATION Programming Contest 2022(AtCoder Beginner Contest 270)


    D - Stones

    贪心   ×

    dp     √

    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 400 points

    Problem Statement

    Takahashi and Aoki will play a game of taking stones using a sequence (A1​,…,AK​).

    There is a pile that initially contains N stones. The two players will alternately perform the following operation, with Takahashi going first.

    • Choose an Ai​ that is at most the current number of stones in the pile. RemoveAi​ stones from the pile.

    The game ends when the pile has no stones.

    If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove?

     

    Constraints

    • 1≤N≤10^4
    • 1≤K≤100
    • 1=A1​
    • All values in the input are integers.

     

    Input

    The input is given from Standard Input in the following format:

    N K
    A1 A2      AK

    Output

    Print the answer.


    Sample Input 1 Copy

    10 2
    1 4
    

    Sample Output 1 Copy

    5
    

    Below is one possible progression of the game.

    • Takahashi removes 4 stones from the pile.
    • Aoki removes 4 stones from the pile.
    • Takahashi removes 1 stone from the pile.
    • Aoki removes 1 stone from the pile.

    In this case, Takahashi removes 5 stones. There is no way for him to remove 6 or more stones, so this is the maximum.

    Below is another possible progression of the game where Takahashi removes 5 stones.

    • Takahashi removes 1 stone from the pile.
    • Aoki removes 4 stones from the pile.
    • Takahashi removes 4 stones from the pile.
    • Aoki removes 1 stone from the pile.

    Sample Input 2 Copy

    11 4
    1 2 3 6
    

    Sample Output 2 Copy

    Copy

    8
    

    Below is one possible progression of the game.

    • Takahashi removes 6 stones.
    • Aoki removes 3 stones.
    • Takahashi removes 2 stones.

    In this case, Takahashi removes 8 stones. There is no way for him to remove 9 or more stones, so this is the maximum.


    Sample Input 3 Copy

    10000 10
    1 2 4 8 16 32 64 128 256 512
    

    Sample Output 3 Copy

    5136

     

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. typedef double db;
    4. #define int long long
    5. int dp[10010];
    6. int a[110];
    7. int n,k;
    8. signed main()
    9. {
    10. ios::sync_with_stdio(false);
    11. cin.tie(0);
    12. cout.tie(0);
    13. cin>>n>>k;
    14. for(int i=1;i<=k;i++)
    15. {
    16. cin>>a[i];
    17. }
    18. for(int i=0;i<=n;i++)
    19. {
    20. for(int j=1;j<=k;j++)
    21. {
    22. if(a[j]>i)break;
    23. dp[i]=max(dp[i],i-dp[i-a[j]]);
    24. }
    25. }
    26. cout<<dp[n]<<"\n";
    27. return 0;
    28. }

     贪心反例:
     

    10 3
    1 3 4

    最大值是6而不是4

  • 相关阅读:
    【Java基础】方法重写、修饰符、权限修饰符及final、static关键字
    银行业生产系统存储数据迁移方法及实践
    从 Oracle 迁移到 TiDB 的方案设计与用户实践
    海思 OSD 抗锯齿、背景透明叠加水印
    泛联新安EDA系列——国内自主研发,首款集成双国军标的HDL代码缺陷管理平台VHawk
    【微信小程序】全局样式文件app.wxss、页面的根元素page、 app.json中的window配置项
    python---进阶篇【函数使用技巧/注意事项】
    《程序员面试金典(第6版)》面试题 02.08. 环路检测(哈希法,双指针,检测链表是否有环)
    Unity-特殊文件夹
    (附源码)计算机毕业设计SSM基于的社区疫情管理系统
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/127033928
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号