杭电 HDU ACM 1087 Super Jumping! Jumping! Jumping!

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25410    Accepted Submission(s): 11226

Problem Description

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.

The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In
the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping
can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his
jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.

Your task is to output the maximum value according to the given chessmen list.

Input

Input contains multiple test cases. Each test case is described in a line as follow:

N value_1 value_2 …value_N

It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.

A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3

Author

lcy

能不能说 这个题恶心死我了。错在了一个盲点上——memset函数的用法上!一开始并没有把所有元素初始化为0;

状态方程:sum[j]=max{sum[i]}+a[j]; 其中,0<=i<=j,a[i]<a[j]

这里我查了下函数用法,也学会了如何适用于结构。

转载http://www.cnblogs.com/PegasusWang/archive/2013/01/20/2868824.html

memset用法详解

1。void *memset(void *s,int c,size_t n)

总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。

2。例子

#include

void main(){

char *s="Golden Global View";

clrscr();

memset(s,‘G‘,6);

printf("%s",s);

getchar();

return 0;

} 

3。memset() 函数常用于内存空间初始化。如:

char str[100];

memset(str,0,100);

4。memset()的深刻内涵:用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;例:char a[100];memset(a, ‘/0‘, sizeof(a));

memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),会造成b的内存地址溢出。

strcpy就只能拷贝字符串了,它遇到‘/0‘就结束拷贝;例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字符串长度(第一个‘/0’之前)是否超过50位,如超过,则会造成b的内存地址溢出。

5.补充:一点心得

memset可以方便的清空一个结构类型的变量或数组。

如:

struct sample_struct

{

char csName[16];

int iSeq;

int iType;

};

对于变量

struct sample_strcut stTest;

一般情况下,清空stTest的方法:

stTest.csName[0]=‘/0‘;

stTest.iSeq=0;

stTest.iType=0;

用memset就非常方便:

memset(&stTest,0,sizeof(struct sample_struct));

如果是数组:

struct sample_struct TEST[10];

memset(TEST,0,sizeof(struct sample_struct)*10);

6。strcpy

原型:extern char *strcpy(char *dest,char *src);

用法:#i nclude

功能:把src所指由NULL结束的字符串复制到dest所指的数组中。

说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。

返回指向dest的指针。

memcpy

原型:extern void *memcpy(void *dest, void *src, unsigned int count);

用法:#i nclude

功能:由src所指内存区域复制count个字节到dest所指内存区域。

说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。

memset

原型:extern void *memset(void *buffer, int c, int count);

用法:#i nclude

功能:把buffer所指内存区域的前count个字节设置成字符c。

说明:返回指向buffer的指针。

memset函数的用法和注意事项:

memset 函数是内存赋值函数,用来给某一块内存空间进行赋值的。

其原型是:void* memset(void *_Dst, int  _Val, size_t _Size);

使用时在文件头加上#include "stdlib.h" 。

_Dst是目标起始地址,_Val是要赋的值,_Size是要赋值的字节数 。

例1:

char str[9];

我们用memset给str初始化为“00000000”,用法如下

memset(str,0,8);

注意,memset是逐字节 拷贝的。

下面再看一个例子:

例2:

int num[8];

我们用memset给str初始化为{1,1,1,1,1,1,1,1},

memset(num,1,8);//这样是不对的

一个int是4个字节的,8个int是32个字节,所以首先要赋值的长度就不应该为8而是32。

因为memset是 逐字节 拷贝,以num为首地址的8字节空间都被赋值为1,

即一个int变为0X00000001 00000001 00000001 00000001,显然,把这个数化为十进制不会等于1的。

所以,在memset使用时要千万小心,在给char以外的数组赋值时,只能初始化为0或者-1。因为在计算机里,0的二进制都是0,-1的二进制都是1。

最近才发现memset的这个问题,之所以要写这篇文章,是因为我在自己的DES算法里多处用到了memset,而我当时却不知memset有这个问题,特此记下,引以为鉴。

内存初始化函数memset()用法详解

作用:在一段内存中填充某个给定的值,注意填充时是按照字节顺序填充的,而不是按照

元素填充。

此方法是对较大的结构体和数组进行清零操作的一种有效方法。

函数形式:memset(void *buffer,int c,size_t n)

buffer是需要设置的内存的开始地址;c是期望填充值;n是需要填充的字节数。

例1:一个int a[10]型变量,则memset(a,100,sizeof(int))此操作后,元素a[0]的每个字

节的值都是100,即0x64,二进制表示:01100100,所以元素a[0]为0x64646464,二进制表示

:01100100 01100100 01100100 01100100

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void main()

{

int i,a[20];

memset(a,10,sizeof(a));

for(i=0;i<20;i++)

printf("%d\n",a[i]);

}

此函数输出的元素并非10,而是每个字节都是00001010组成的int型数。

例2:

#include

#include

void main( void )

{

char buffer[] = "This is a test of the memset function";

printf( "Before: %s\n", buffer );

memset( buffer, ‘*‘, 4 );

printf( "After: %s\n", buffer );

}

Output

Before: This is a test of the memset function

After: **** is a test of the memset function

Memset用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘

‘或‘\0‘;

例:char a[100];

memset(a, ‘\0‘, sizeof(a));

memset可以方便地清空一个结构体类型的变量或数组。

如:

struct sample_struct

{

char csName[16];

int iSeq;

int iType;

};

对于变量

struct sample_strcut stTest;

一般情况下,清空stTest的方法:

stTest.csName[0]=‘\0‘;

stTest.iSeq=0;

stTest.iType=0;

用memset就非常方便:

memset(&stTest,0,sizeof(struct sample_struct));

如果是数组:

struct sample_struct TEST[10];

memset(TEST,0,sizeof(struct sample_struct)*10);

memcpy 用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度

例:char a[100],b[50]; memcpy(b, a, sizeof(b));

注意如果用memcpy(b,a,sizeof(a)),很可能会造成b的内存地址溢出。

Strcpy就只能拷贝字符串了,它遇到‘\0‘就结束拷贝。

例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字符串长度(第一

个‘\0’之前)是否超过50位,如超过,则会造成b的内存地址溢出。

str也可以用用个参数的strncpy(a,b,n)

AC:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
int maxx(int x,int y)
{
	return x>y? x:y;
}
int main()
{
    int n;int  ls[1005];int max,r;
   int   dp[1005];
    while(cin>>n,n)
    {for(int i=1;i<=n;i++)
            cin>>ls[i];
        memset(ls,0,sizeof(int));

        memset(dp,0,sizeof(dp));

        for(int j=1;j<=n;j++)
        {

            for(r=0;r<j;r++)
            {
                if(ls[r]<ls[j])
                {
                    dp[j]=maxx(dp[j],dp[r]+ls[j]);

                }

            }

        }

       max=0;
        for(int k=0;k<=n;k++)
        {
            if(dp[k]>max)
                max=dp[k];
        }
        cout<<max<<endl;
    }
    return 0;
}
时间: 2024-10-08 20:32:00

杭电 HDU ACM 1087 Super Jumping! Jumping! Jumping!的相关文章

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

杭电 HDU ACM 1025 Constructing Roads In JGShining&#39;s Kingdom

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17732    Accepted Submission(s): 5023 Problem Description JGShining's kingdom consists of 2n(n is no mo

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

杭电 HDU ACM 圆桌会议

圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3356    Accepted Submission(s): 2351 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有

杭电 HDU ACM 1046 Tempter of the Bone

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 83458    Accepted Submission(s): 22740 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

杭电 HDU ACM 1496 Equations

Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6065    Accepted Submission(s): 2455 Problem Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a,

杭电 HDU ACM 1283 最简单的计算机

最简单的计算机 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5238    Accepted Submission(s): 2967 Problem Description 一个名叫是PigHeadThree的研究组织设计了一台实验用的计算机,命名为PpMm.PpMm只能执行简单的六种命令A,B,C,D,E,F:只有二个内存M1,M

杭电 HDU ACM 1213 How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17049    Accepted Submission(s): 8349 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn