zoj 3310 - Unrequited Love

题目:给你一个数字环,从里面去除不相邻的数字,使得取出的数字和最大。

分析:dp,动态规划。

如果没有首尾相连的话,这个题目就很简单了:

动态方程:F(n)= max(F[ n-1 ],F[ n-2 ] + D[ n ]);

现在告诉我们首尾相连,为了无后效性,我们要把环拆开。;

我们把第一个元素分为两种状况,取或不取:

当取第一个元素时,我们DP到n-1位置;当不取第一个元素时,我们DP到n位置;

时间复杂性:O(n)。

说明:(2011-09-19 09:26)。

#include <stdio.h>
#include <string.h>

int D[ 1000002 ];
int F[ 1000002 ];
int G[ 1000002 ];

int main()
{
    int n;
    while ( ~scanf("%d",&n) ) {
        for ( int i = 1 ; i <= n ; ++ i )
            scanf("%d",&D[ i ]);

        /* 当选择第一个数字时 */
        memset( F, 0, sizeof( F ) );
        F[ 1 ] = D[ 1 ];
        for ( int i = 2 ; i <= n ; ++ i ) {
            F[ i ] = F[ i-2 ] + D[ i ];
            if ( F[ i ] < F[ i-1 ] )
                F[ i ] = F[ i-1 ];
        }

        int Max = F[ n-1 ];

        /* 当不选择第一个数字时 */
        memset( G, 0, sizeof( F ) );
        for ( int i = 2 ; i <= n ; ++ i ) {
            G[ i ] = G[ i-2 ] + D[ i ];
            if ( G[ i ] < G[ i-1 ] )
                G[ i ] = G[ i-1 ];
        }

        if ( Max < G[ n ] )
            Max = G[ n ];

        printf("%d\n",Max);
    }
    return 0;
}
时间: 2025-01-14 10:21:59

zoj 3310 - Unrequited Love的相关文章

(环形DP) zoj 3310

W - Unrequited Love Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3310 Appoint description:  System Crawler  (2015-04-16) Description Owen had been through unrequited love with N MM for a long t

zoj 4704 Unrequited Love 贪心 孤独者

Unrequited Love Time Limit: 16 Seconds      Memory Limit: 131072 KB There are n single boys and m single girls. Each of them may love none, one or several of other people unrequitedly and one-sidedly. For the coming q days, each night some of them wi

ZOJ 3601 Unrequited Love 浙江省第九届省赛

Unrequited Love Time Limit: 16 Seconds      Memory Limit: 131072 KB There are n single boys and m single girls. Each of them may love none, one or several of other people unrequitedly and one-sidedly. For the coming q days, each night some of them wi

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost