HDU 4719 Oh My Holy FFF

N soldiers from the famous "*FFF* army" is standing in a line, from left to right. 
 o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o
/F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\
/ \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \

You, as the captain of *FFF*, want to divide them into smaller groups, but each group should still be continous in the original line. Like this: 
 o   o   o  |  o   o   o   o  |  o   o   o   o   o   o  |  o   o   o   o   o
/F\ /F\ /F\ | /F\ /F\ /F\ /F\ | /F\ /F\ /F\ /F\ /F\ /F\ | /F\ /F\ /F\ /F\ /F\
/ \ / \ / \ | / \ / \ / \ / \ | / \ / \ / \ / \ / \ / \ | / \ / \ / \ / \ / \

In your opinion, the number of soldiers in each group should be no more than L. 
Meanwhile, you want your division be "holy". Since the soldier may have different heights, you decide that for each group except the first one, its last soldier(which is the rightmost one) should be strictly taller than the previous group's last soldier. That is, if we set bi as the height of the last soldier in group i. Then for i >= 2, there should be b  i > b  i-1
You give your division a score, which is calculated as  , b  0 = 0 and 1 <= k <= M, if there are M groups in total. Note that M can equal to 1. 
Given the heights of all soldiers, please tell us the best score you can get, or declare the division as impossible.
Input
The first line has a number T (T <= 10) , indicating the number of test cases. 
For each test case, first line has two numbers N and L (1 <= L <= N <= 10  5), as described above. 
Then comes a single line with N numbers, from H1 to Hn, they are the height of each soldier in the line, from left to right. (1 <= H  i <= 10  5)
Output
For test case X, output "Case #X: " first, then output the best score.
Sample Input
2
5 2
1 4 3 2 5
5 2
5 4 3 2 1
Sample Output
Case #1: 31

Case #2: No solution

其实就是一个线性的dp,但是需要考虑到前L个比当前小的最大值,所以通过线段树优化。

线段树中保存权值区间的最大值,单点更新,同时在底层维护一个set,来存放权值相同的不同答案。

然后根据dp向后移动删除之前多余的值。

#include<map>   
#include<set>  
#include<ctime>    
#include<cmath>   
#include<stack>
#include<queue>     
#include<string>    
#include<vector>    
#include<cstdio>        
#include<cstring>      
#include<iostream>    
#include<algorithm>        
#include<functional>    
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))        
#define rep(i,j,k) for(int i=j;i<=k;i++)        
#define per(i,j,k) for(int i=j;i>=k;i--)        
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])        
#define inone(x) scanf("%d",&x)        
#define intwo(x,y) scanf("%d%d",&x,&y)        
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)      
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)     
#define lson x<<1,l,mid    
#define rson x<<1|1,mid+1,r    
#define mp(i,j) make_pair(i,j)    
#define ff first    
#define ss second    
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const int M = 1e5;
const double eps = 1e-8;
int T, n, m, cas = 1;
int a[N];
LL f[N], g[N];
multiset<LL> p[N];

void clear(int x, int l, int r)
{
	f[x] = 0;
	if (l == r) {
		p[r].clear(); return;
	}
	int mid = l + r >> 1;
	clear(lson); clear(rson);
}

void insert(int x, int l, int r, int u, LL v)
{
	if (l == r) { 
		p[r].insert(v); 
		multiset<LL> ::iterator i = p[r].end();
		f[x] = *(--i);
		return; 
	}
	int mid = l + r >> 1;
	if (u <= mid) insert(lson, u, v);
	else insert(rson, u, v);
	f[x] = max(f[x << 1], f[x << 1 | 1]);
}

void del(int x, int l, int r, int u, LL v)
{
	if (l == r) {
		p[r].erase(p[r].find(v));
		if (p[r].size())
		{
			multiset<LL> ::iterator i = p[r].end();
			f[x] = *(--i);
		}
		else f[x] = 0;
		return;
	}
	int mid = l + r >> 1;
	if (u <= mid) del(lson, u, v);
	else del(rson, u, v);
	f[x] = max(f[x << 1], f[x << 1 | 1]);
}

LL find(int x, int l, int r, int u)
{
	if (r <= u) return f[x];
	int mid = l + r >> 1;
	LL now = find(lson, u);
	if (mid < u) now = max(now, find(rson, u));
	return now;
}

int main()
{
	for (inone(T); T--; cas++)
	{
		intwo(n, m);
		rep(i, 1, n) inone(a[i]);
		clear(1, 0, M);
		g[0] = 1; a[0] = 0;
		insert(1, 0, M, a[0], g[0]);
		rep(i, 1, n)
		{
			g[i] = find(1, 0, M, a[i] - 1);
			if (g[i])
			{
				g[i] = g[i] + 1LL * a[i] * (a[i] - 1);
				insert(1, 0, M, a[i], g[i]);
			}
			if (i >= m && g[i - m]) del(1, 0, M, a[i - m], g[i - m]);
		}
		if (g[n]) printf("Case #%d: %lld\n", cas, g[n] + a[n] - 1);
		else printf("Case #%d: No solution\n", cas);
	}
	return 0;
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值