题意:

给你一个长度为n个序列v,你需要从中找一个子序列。这个子序列的值等于:子序列中奇数下标的值-偶数下标的值

你需要使得这个值尽可能大,让你输出这个最大值

题解:

dp[i][0]表示:在原序列从1到i位置中找一个子序列,这个子序列长度为偶数的子序列最大值

dp[i][1]表示:在原序列从1到i位置中找一个子序列,这个子序列长度为奇数的子序列最大值

初始化:

dp[i][0]=0

dp[i][1]=v[i]

转移方程:

1、dp[i][0]=max(dp[i-1][1]-v[i],dp[i-1][0]);

它的值或着是继承上一个dp[i-1][0],或者是由之前的奇数长度再加上vi构成一个偶数长度序列dp[i-1][1]-vi

dp[i][1]=max(dp[i][1],max(dp[i-1][0]+v[i],dp[i-1][1]));

它的值可以由本身得到(因为它可以不取之前的序列,它自己就能构成一个长度为1的子序列),或者是继承上一个dp[i-1][1],或者是由之前的偶数长度再加上vi构成一个奇数长度序列dp[i-1][0]+vi

代码:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=3e5+10;
ll v[maxn],q[maxn][2];
int main()
{
ll t;
scanf("%lld",&t);
while(t--)
{
ll n,m;
scanf("%lld%lld",&n,&m);
ll maxx=0;
for(ll i=1;i<=n;++i)
{
scanf("%lld",&q[i][1]);
v[i]=q[i][1];
q[i][0]=0;
}
//printf("%lld %lld\n",q[1][0],q[1][1]);
for(ll i=2;i<=n;++i)
{
q[i][0]=max(q[i-1][1]-v[i],q[i-1][0]); //1 3 2
q[i][1]=max(q[i][1],max(q[i-1][0]+v[i],q[i-1][1]));
//printf("%lld %lld\n",q[i-1][0],q[i-1][1]);
}
printf("%lld\n",max(q[n][1],q[n][0]));
}
return 0;
}

Pokémon Army (easy version) CodeForces - 1420C1 dp的更多相关文章

  1. C1. Pokémon Army (easy version) 解析(DP)

    Codeforce 1420 C1. Pokémon Army (easy version) 解析(DP) 今天我們來看看CF1420C1 題目連結 題目 對於一個數列\(a\),選若干個數字,求al ...

  2. Codeforces Round #672 (Div. 2) C1. Pokémon Army (easy version) (DP)

    题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置, ...

  3. C2. Pokémon Army (hard version) 解析(思維)

    Codeforce 1420 C2. Pokémon Army (hard version) 解析(思維) 今天我們來看看CF1420C2 題目連結 題目 略,請直接看原題. 前言 根本想不到這個等價 ...

  4. Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)

    <题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...

  5. D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)

    D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...

  6. Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)

    The only difference between easy and hard versions is a number of elements in the array. You are giv ...

  7. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

  8. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

  9. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

随机推荐

  1. VBA实现相同行合并

    帮人捣鼓了个VBA代码用来实现多行合并,具体需求为:列2/列3/列4 相同的情况下,则对应的行合并为一行,且列1用空格隔开,列5则相加: (对大多数办公室职员,VBA还算是提高效率的一个利器吧) 最终 ...

  2. Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because of access permission

    Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because ...

  3. 01-CentOS 8.1安装 Docker

    官方参考地址:https://docs.docker.com/install/linux/docker-ce/centos/ 里面包含包下载地址:https://download.docker.com ...

  4. Poj-P2533题解【动态规划】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=2533 题目描述: 如果ai1 < ...

  5. Nginx的简介和使用nginx实现请求转发

    一.什么是Nginx Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为H ...

  6. 说说C# 8.0 新增功能Index和Range的^0是什么?

    前言 在<C# 8.0 中使用 Index 和 Range>这篇中有人提出^0是什么意思?处于好奇就去试了,结果抛出异常.查看官方文档说^0索引与 sequence[sequence.Le ...

  7. virtualenv安装和配置

    安装命令 命令执行结束 配 执行命令:virtualenv testvir 执行完成:会在当前目录下生成如下文件夹 进入到testvir目录 进入Scripts目录: 进入虚拟环境:执行 activa ...

  8. MonkeyRunner使用

    #-*- coding:utf-8 –*- from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage #连接 ...

  9. .net core Wpf中使用cefsharp加载本地html网页,并且cefsharp支持any cpu

    第一步,在程序包管理器安装 cefsharp.wpf 第二步 您必须在项目的第一个 < propertygroup > 中添加 < cefsharpanycpusupport > ...

  10. Codeforces 1437F Emotional Fishermen(思维,dp)

    题意 给出数列\(a_i\),求排列\(p_i\)的数量满足 \[\frac{a_{p_i}}{max_{j=1}^{i-1}a_{p_j}} \notin (\frac{1}{2},2) \] 思路 ...