Apple Catching

本文探讨了一道关于在两棵不断掉落APP的树下,如何在有限次移动中收集最多APP的问题。通过动态规划(DP)算法,详细解析了代码实现过程,包括状态转移方程的设计和边界条件的处理。

http://poj.org/problem?id=2385

Description

有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速移动到另外一棵APP树下接APP(移动时间可以忽略不计),但由于却乏锻炼,你最多移动W次.问在T秒内,你最多能收集多少个APP.假设你开始站在1号APP树下.

Input

第1行:两个整数T(1 < = T< = 1000)和W(1 < = W< = 30)
第2..T+1行:1或2,代表每分钟掉落APP的那棵树的编号

Output

一行一个整数,代表你移动不超过W次能接住的最大APP数

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

 

思路:dp[i][j]代表第i时间,换j次得到的最多苹果

注意a初始化,dp初始化,还有数组的大小,死了还久,希望下次不会了。。。。。。

 

代码

#include<cstdio>  
#include<iostream> 
#include<cstring>
#include<algorithm> 
using namespace std;
int dp[1010][40];
int main()  
{  
    int n,m; int a[1010][2];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
	    int b;  
	    for(int i=1;i<=n;i++)
		{
			scanf("%d",&b);
			a[i][b]=1;
		    a[i][b%2+1]=0;
		} 
		if(a[1][1]==1)
		{
			dp[1][0]=1;
			dp[1][1]=0;
		}
		else
		{
			dp[1][0]=0;
		   	dp[1][1]=1;
		}
		int ans=0;
	    for(int i=1;i<=n;i++)
	    {
	    	for(int j=0;j<=m;j++)
	    	{		
	    	    if(j==0)
	    	    {
	    	    	dp[i][j]=dp[i-1][j]+a[i][1]; 
				}
	    	    else if(j%2==1)
	    	    {
	    	    	dp[i][j]=max(dp[i-1][j-1]+a[i][1],dp[i-1][j]+a[i][2]);
				}
				else
				dp[i][j]=max(dp[i-1][j-1]+a[i][2],dp[i-1][j]+a[i][1]);
				
				ans=max(dp[i][j],ans);
			}
		}
	    printf("%d\n",ans);        
	}
      return 0;
}  

代码2

#include<cstdio>  
#include<iostream> 
#include<cstring>
#include<algorithm> 
using namespace std;
const int maxn = 1111;
int dp[maxn][35];
int a[maxn];
int main()  
{  
    int t, w;
    while(~scanf("%d%d", &t, &w)) 
	{
        for(int i = 1; i <= t; i++)
            scanf("%d", &a[i]);
        if(a[1] == 1) 
		{
            dp[1][0] = 1;
            dp[1][1] = 0;
        }
        else 
		{
            dp[1][0] = 0;
            dp[1][1] = 1;
        }
        for(int i = 2; i <= t; i++) 
		{
            for(int j = 0; j <= w; j++) 
			{
                if(j == 0) dp[i][j] = dp[i-1][j] + a[i]%2;
                else 
				{
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]);
                    if(j%2+1 == a[i]) dp[i][j]++;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i <= w; i++)
            ans = max(ans, dp[t][i]);
        printf("%d\n", ans);
 
    }
    return 0;
}  

 

Advanced Apple Debugging &amp; Reverse Engineering, Second Edition ISBN: Learn the powerful secrets of Apple&#39;s software debugger, LLDB, that can get more information out of any program than you ever thought possible. In Advanced Apple Debugging and Reverse Engineering, you&#39;ll come to realize debugging is an enjoyable process to help you better understand software. Not only will you learn to find bugs faster, but you&#39;ll also learn how other developers have solved problems similar to yours. You&#39;ll also learn how to create custom, powerful debugging scripts that will help you quickly find the secrets behind any bit of code that piques your interest. This book is for intermediate to advanced iOS/macOS developers who are already familiar with either Swift or Objective-C and want to take their debugging skills to the next level. Topics Covered in Advanced Apple Debugging &amp; Reverse Engineering: LLDB Max Achievement: Master LLDB and learn about its extensive list of subcommands and options. 1&#39;s and 0&#39;s: Learn the low-level components available to help extract useful information from a program, from assembly calling conventions to exploring the process of dynamically-loaded frameworks. The Power of Python: Use LLDB&#39;s Python module to create powerful custom debugging commands to introspect and augment existing programs. Nothing is Secret: Learn how to use DTrace, a dynamic tracing framework, and how to write D scripts to query anything you were ever curious about on your macOS machine. Case Studies: Quickly find and solve the real-world issues that iOS and macOS developers typically face in their day-to-day development workflow. After reading this book, you&#39;ll have the tools and knowledge to answer even the most obscure question about your code - or someone else&#39;s.
完整高清+书签版,全面支持 Xcode 9, Swift 4.0,由于上传110MB的限制,要下源码,请移步至:http://download.youkuaiyun.com/download/a56562626206/10208625 Advanced Apple Debugging &amp; Reverse Engineering, Second Edition ISBN: Learn the powerful secrets of Apple&rsquo;s software debugger, LLDB, that can get more information out of any program than you ever thought possible. In Advanced Apple Debugging and Reverse Engineering, you&rsquo;ll come to realize debugging is an enjoyable process to help you better understand software. Not only will you learn to find bugs faster, but you&rsquo;ll also learn how other developers have solved problems similar to yours. You&rsquo;ll also learn how to create custom, powerful debugging scripts that will help you quickly find the secrets behind any bit of code that piques your interest. This book is for intermediate to advanced iOS/macOS developers who are already familiar with either Swift or Objective-C and want to take their debugging skills to the next level. Topics Covered in Advanced Apple Debugging &amp; Reverse Engineering: LLDB Max Achievement: Master LLDB and learn about its extensive list of subcommands and options. 1&rsquo;s and 0&rsquo;s: Learn the low-level components available to help extract useful information from a program, from assembly calling conventions to exploring the process of dynamically-loaded frameworks. The Power of Python: Use LLDB&rsquo;s Python module to create powerful custom debugging commands to introspect and augment existing programs. Nothing is Secret: Learn how to use DTrace, a dynamic tracing framework, and how to write D scripts to query anything you were ever curious about on your macOS machine. Case Studies: Quickly find and solve the real-world issues that iOS and macOS developers typically face in their day-to-day development workflow.
Advanced Apple Debugging &amp; Reverse Engineering v0.9.5 Explore code through LLDB, Python and DTrace, to discover more about any program than you ever thought possible. Table of Contents 1. Getting Started In this chapter, you&rsquo;re going to get acquainted with LLDB and investigate the process of introspecting and debugging a program. You&rsquo;ll start off by introspecting a program you didn&rsquo;t even write &mdash; Xcode! 2. Help &amp; Apropos Just like any respectable developer tool, LLDB ships with a healthy amount of documentation. Knowing how to navigate through this documentation &mdash; including some of the more obscure command flags &mdash; is essential to mastering LLDB. 3. Attaching with LLDB Now that you&rsquo;ve learned about the two most essential commands, help and apropos, it&rsquo;s time to investigate how LLDB attaches itself to processes. You&rsquo;ll learn all the different ways you can attach LLDB to processes using various options, as well as what happens behind the scenes when attaching to processes. 4. Stopping in Code Whether you&rsquo;re using Swift, Objective-C, C++, C, or an entirely different language in your technology stack, you&rsquo;ll need to learn how to create breakpoints. It&rsquo;s easy to click on the side panel in Xcode to create a breakpoint using the GUI, but the LLDB console can give you much more control over breakpoints. 5. Expression Now that you&rsquo;ve learned how to set breakpoints so the debugger will stop in your code, it&rsquo;s time to get useful information out of whatever software you&rsquo;re debugging. In this chapter you&rsquo;ll learn about the expression command, which allows you to execute arbitrary code in the debugger. 6. Thread, Frame &amp; Stepping Around You&rsquo;ve learned how to create breakpoints, how to print and modify values, as well as how to execute code while paused in the debugger. But so far you&rsquo;ve been left high and dry on how to move around in the debugger and inspect data beyond the immediate. In this chapter, you&rsquo;ll learn how to move the debugger in and out of functions while LLDB is currently paused. 7. Image it&rsquo;s time to explore one of the best tools for finding code of interest through the powers of LLDB. In this chapter, you&rsquo;ll take a deep dive into the image command. 8. Persisting &amp; Customizing Commands In this chapter, you&rsquo;ll learn how to persist these choices through the .lldbinit file. By persisting your choices and making convenience commands for yourself, your debugging sessions will run much more smoothly and efficiently. This is also an important concept because from here on out, you&rsquo;ll use the .lldbinit file on a regular basis. 9. Regex Commands In the previous chapter, you learned about the command alias command as well as how to persist commands through the lldbinit file. Unfortunately, command alias has some limitations. The LLDB command command regex acts much like command alias, except you can provide a regular expression for input which will be parsed and applied to the action part of the command. 10. Assembly Register Calling Convention Now you&rsquo;ve gained a basic understanding of how to maneuver around the debugger, it&rsquo;s time to take a step down the executable Jenga tower and explore the 1s and 0s that make up your source code. This section will focus on the low-level aspects of debugging. 11. Assembly &amp; Memory In this chapter, you&rsquo;ll explore how a program executes. You&rsquo;ll look at a special register used to tell the processor where it should read the next instruction from, as well as how different sizes and groupings of memory can produce very different results. 12. Assembly and the Stack What does being &quot;passed on the stack&quot; mean exactly? It&rsquo;s time to take a deeper dive into what happens when a function is called from an assembly standpoint by exploring some &ldquo;stack related&rdquo; registers as well as the contents in the stack. 13. Hello, Ptrace As alluded to in the introduction to this book, debugging is not entirely about just fixing stuff. Debugging is the process of gaining a better understanding of what&rsquo;s happening behind the scenes. In this chapter, you&rsquo;ll explore the foundation of debugging, namely, a system call responsible for a process attaching itself to another process: ptrace. 14. Dynamic Frameworks With dynamic frameworks comes a very interesting aspect of learning, debugging, and reverse engineering. Since you have the ability to load the framework at runtime, you can use LLDB to explore and execute code at runtime, which is great for spelunking in both public and private frameworks. 15. Hooking &amp; Executing Code with dlopen &amp; dlsym It&rsquo;s time to learn about the complementary skills of developing with these frameworks. In this chapter, you&rsquo;re going to learn about methods and strategies to &ldquo;hook&rdquo; into Swift and C code as well as execute methods you wouldn&rsquo;t normally have access to. 16. Exploring and Method Swizzling Objective-C Frameworks You&rsquo;ll cap off this round of dynamic framework exploration by digging into Objective-C frameworks using the Objective-C runtime to hook and execute methods of interest. 17. Hello Script Bridging Next up in the tradeoff between convenience and complexity is LLDB&rsquo;s script bridging. With script bridging, you can do nearly anything you like. Script bridging is a Python interface LLDB uses to help extend the debugger to accomplish your wildest debugging dreams. 18. Debugging Script Bridging You need a methodical way to figure out what went wrong in your LLDB script so you don&rsquo;t pull your hair out. In this chapter, you&rsquo;ll explore how to inspect your LLDB Python scripts using the Python pdb module, which is used for debugging Python scripts. 19. Script Bridging Classes and Hierarchy You&rsquo;ve learned the essentials of working with LLDB&rsquo;s Python module, as well as how to correct any errors using Python&rsquo;s PDB debugging module. Now you&rsquo;ll explore the main players within the lldb Python module for a good overview of the main parts. In this chapter, you&rsquo;ll add some arguments to this script and deal with some annoying edge cases, such handling commands differently between Objective-C and Swift. 20. Script Bridging with Options &amp; Arguments When you&rsquo;re creating a custom debugging command, you&rsquo;ll often want to slightly tweak functionality based upon options or arguments supplied to your command. A custom LLDB command that can do a job only one way is a boring one-trick pony. In this chapter, you&rsquo;ll explore how to pass optional parameters (aka options) as well as arguments (parameters which are expected) to your custom command to alter functionality or logic in your custom LLDB scripts. 21. Script Bridging with SBValue &amp; Memory So far, when evaluating JIT code (i.e. Objective-C, Swift, C, etc. code that&rsquo;s executed through your Python script), you&rsquo;ve used a small set of APIs to evaluate the code. It&rsquo;s time to talk about a new class in the lldb Python module, SBValue, and how it can simplify the parsing of JIT code output. 22. SB Examples, Improved Lookup For the rest of the chapters in this section, you&#39;ll focus on Python scripts. As alluded to in the previous chapter, the image lookup -rn command is on its way out. When you finish this chapter, you&rsquo;ll have a new script named &quot;lookup&quot; which queries in a much cleaner way. 23. SB Examples, Resymbolicating a Stripped ObjC Binary When LLDB comes up against a stripped executable (an executable devoid of DWARF debugging information), LLDB won&rsquo;t have the symbol information to give you the stack trace. Instead, LLDB will generate a synthetic name for a method it recognizes as a method, but doesn&rsquo;t know what to call it. In this chapter, you&rsquo;ll build an LLDB script that will resymbolicate stripped Objective-C functions in a stack trace. 24. SB Examples, Malloc Logging For the final chapter in this section, you&rsquo;ll go through the same steps I myself took to understand how the MallocStackLogging environment variable is used to get the stack trace when an object is created. From there, you&rsquo;ll create a custom LLDB command which gives you the stack trace of when an object was allocated or deallocated in memory &mdash; even after the stack trace is long gone from the debugger. 25. Hello, DTrace You&rsquo;ll explore a very small section of what DTrace is capable of doing by tracing Objective-C code in already compiled applications. Using DTrace to observe iOS frameworks (like UIKit) can give you an incredible insight into how the authors designed their code. 26. Intermediate DTrace This chapter will act as a grab-bag of more DTrace fundamentals, destructive actions (yay!), as well as how to use DTrace with Swift. In this chapter, you&#39;ll learn additional ways DTrace can profile code, as well as how to augment existing code without laying a finger on the actual executable itself. 27. DTrace vs objc_msgSend In this chapter, you&#39;ll use DTrace to hook objc_msgSend&#39;s entry probe and pull out the class name along with the Objective-C selector for that class. By the end of this chapter, you&#39;ll have LLDB generating a DTrace script which only generates tracing info for code implemented within the main executable that calls objc_msgSend.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值