题目大意

  有\(n\)个士兵(\(1 \leq n \leq 10^5\)),第\(i\)个士兵的身高为\(h_{i}\),现在要求把士兵按照原来的顺序分成连续的若干组,要求每组的士兵数量不超过\(len\)。

  同时,我们设每组的最后一个士兵的身高为\(b_{i}\),则有\(b_{i} > b_{i - 1}\)(\(b_0 = 0\)),现在我们设每种分组方案的价值为\(\sum b_{i}^2 - b_{i - 1}\),求能得到的最大价值为多少?

题解

  我们设\(dp[i]\)表示前\(i\)个士兵分成任意组的最大价值,容易得到:

\[dp[i] = \underset{i - len \leq j < i}{\max} \{ dp[j] + k_{i}^2 - k_{j} \}
\]

  整理一下,得到:

\[dp[i] = k_{i}^2 + \underset{i - len \leq j < i}{\max} \{ dp[j] - k_{j} \}
\]

  我们可以用线段树来维护\(\underset{i - len \leq j < i}{\max} \{ dp[j] - k_{j} \}\).

  但是。如何保证题目中要求的\(b_{i} > b_{i - 1}\)呢?

  其实,对于每个士兵,我们可以先按照身高来进行升序排列,如果身高相同,我们就按照编号(原来的顺序)降序排列,然后对于排序后的士兵\(i\),我们设他原来的编号为\(idx_{i}\),则我们就查找线段树上\([idx_{i} - len, idx_{i} - 1]\)的价值,同时更新也是更新线段树上的\(idx_{i}\)的位置。

  因为对于每个士兵\(i\),如果在排序前能找到和他进行状态转移的士兵\(j\),那么排序后,肯定有\(idx_{j} \in [idx_{i} - len, idx_{i} - 1]\),这个大家可以自己试几个情况,所以这样做即可。

#include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm> #define MAX_N (100000 + 5)
#define SIZE (1 << 21) #define lowbit(x) ((x) & -(x))
#define Getchar() (p1 == p2 && (p2 = (p1 = fr) + fread(fr, 1, SIZE, stdin), p1 == p2) ? EOF : *p1++) using namespace std; char fr[SIZE], * p1 = fr, * p2 = fr; void Read(int & res)
{
res = 0;
char ch = Getchar();
while(!isdigit(ch)) ch = Getchar();
while(isdigit(ch)) res = res * 10 + ch - '0', ch = Getchar();
return;
} struct Node
{
int h;
int idx;
friend inline bool operator < (Node a, Node b)
{
if(a.h != b.h) return a.h < b.h;
return a.idx > b.idx;
}
}; int T;
int n, len;
Node a[MAX_N];
long long s[MAX_N << 2]; void Modify(int x, int l, int r, int pos, long long val)
{
if (r < pos || pos < l) return;
if (l == r)
{
s[x] = val;
return;
}
int mid = l + r >> 1;
Modify(x << 1, l, mid, pos, val);
Modify(x << 1 | 1, mid + 1, r, pos, val);
s[x] = max(s[x << 1], s[x << 1 | 1]);
return;
} long long Query(int x, int l, int r, int L, int R)
{
if (r < L || R < l) return -0x7f7f7f7f7f7f7f7f;
if (L <= l && r <= R) return s[x];
int mid = l + r >> 1;
return max(Query(x << 1, l, mid, L, R), Query(x << 1 | 1, mid + 1, r, L, R));
} int main()
{
Read(T);
for (int I = 1; I <= T; ++I)
{
memset(s, -0x7f, sizeof s);
Read(n); Read(len);
for (int i = 1; i <= n; ++i)
{
Read(a[i].h);
a[i].idx = i;
}
sort(a + 1, a + n + 1);
long long tmp;
printf("Case #%d: ", I);
Modify(1, 0, n, 0, 0);
for (int i = 1; i <= n; ++i)
{
tmp = Query(1, 0, n, max(0, a[i].idx - len), a[i].idx - 1);
if (tmp < -0x7f7f7f7f)
{
if (a[i].idx == n)
{
printf("No solution\n");
break;
}
continue;
}
if (a[i].idx == n)
{
printf("%lld\n", (long long)a[i].h * a[i].h + tmp);
break;
}
Modify(1, 0, n, a[i].idx, (long long)a[i].h * a[i].h + tmp - a[i].h);
}
}
return 0;
}

【题解】Oh My Holy FFF的更多相关文章

  1. HDU4719-Oh My Holy FFF(DP线段树优化)

    Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) T ...

  2. HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description N soldiers from the famous "*FFF* army" is standing in a line, from left to ri ...

  3. hdu4719 Oh My Holy FFF 线段树维护dp

    题意:给你一个长度为n的数组v,你需要把这个数组分成很多段,你需要保证每一段的长度不能超过k我们设一共有m段,每一段右边界那个数为bi那么我们要使得sum(bi*bi-b(i-1))最大 (1< ...

  4. hdu4719 Oh My Holy FFF 线段树优化dp

    思路 好久之前的了,忘记什么题目了 可以到我这里做luogu 反正就是hdu数据太水,导致自己造的数据都过不去,而hdu却A了 好像是维护了最大值和次大值,然后出错的几率就小了很多也许是自己写错了,忘 ...

  5. HDU 4719Oh My Holy FFF 线段树+DP

    /* ** 日期: 2013-9-12 ** 题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个.每个数有一个h[i], ** 每份最右边的那个数要大于前一份最右边的那个数.设每份最右边的 ...

  6. 2013 ACM/ICPC Asia Regional Online —— Warmup2

    HDU 4716 A Computer Graphics Problem 水题.略 HDU 4717 The Moving Points 题目:给出n个点的起始位置以及速度矢量,问任意一个时刻使得最远 ...

  7. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  8. 华南师大 2017 年 ACM 程序设计竞赛新生初赛题解

    题解 被你们虐了千百遍的题目和 OJ 也很累的,也想要休息,所以你们别想了,行行好放过它们,我们来看题解吧... A. 诡异的计数法 Description cgy 太喜欢质数了以至于他计数也需要用质 ...

  9. 洛谷 P2194 HXY烧情侣【Tarjan缩点】 分析+题解代码

    洛谷 P2194 HXY烧情侣[Tarjan缩点] 分析+题解代码 题目描述: 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里 ...

随机推荐

  1. BSOJ5458 [NOI2018模拟5]三角剖分Bsh 分治最短路

    题意简述 给定一个正\(n\)边形及其三角剖分,每条边的长度为\(1\),给你\(q\)组询问,每次询问给定两个点\(x_i\)至\(y_i\)的最短距离. 做法 显然正多边形的三角剖分是一个平面图, ...

  2. MySQL数据库3分组与单表、多表查询

    目录 一.表操作的补充 1.1null 和 not null 1.2使用not null的时候 二.单表的操作(import) 2.1分组 2.1.1聚合函数 2.1.2group by 2.1.3h ...

  3. LeetCode--075--颜色分类(python)

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  4. jQuery 遍历 - 同胞(siblings)

    同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...

  5. luogu P1147 连续自然数和 x

    P1147 连续自然数和 题目描述 对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M. 例子:1998+1999+2000+2001+2002 = 10000,所以 ...

  6. Linq in not in\like not like

    别人的博客 http://blog.163.com/lesheng@126/blog/static/357364652010102111051668/ using System.Data.Linq.S ...

  7. RESTful风格编程

    参考文档:http://blog.didispace.com/springbootrestfulapi/ https://www.jianshu.com/p/91600da4df95 *)RESTfu ...

  8. ProtocolHandler继承体系

  9. Matlab 读取文件夹里所有的文件

    (image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...

  10. leetcode 142. 环形链表 II(c++)

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...