9.23日志

2.1089. 烽火传递 - AcWing题库

我们发现这题要求连续的m个就需要点燃一个,那么换言之就是不能有超过m个连续的烽火都是熄灭的,那么要我们求点燃的最小代价其实就是要我们去求能不点燃的最大代价,我们令dp[i]表示的值是前i座烽烟最大的不点燃的代价那么对于第i座烽烟若是选择点亮则dp[i] = dp[i - 1],若不点亮那么dp[i] = dp[i - x + 1] + a[i] - a[i - x],那么我们要找的实际上就是dp[i - x + 1] - a[i - x]的最大值其中1 <= x <= m,那么我们就需要维护一个长度为m的滑动窗口

#define yyy cout<<"Yes"<<"\n"
#define nnn	cout<<"No"<<"\n"
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) 
#define x first 
#define y second 
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll , ll> pii;
typedef pair <int , char> pic;
const int N = 2e6 + 10,inf = 0x3f3f3f3f,mod = 1e9 + 7;

int n,m;
ll a[N],dp[N];
int q[N];

ll g(int x)
{
	if(x == 0)
	{
		return 0;
	}else
	{
		return dp[x - 1] - a[x];
	}
}

int main()
{
	IOS;
	
	cin>>n>>m;
	for(int i = 1 ; i <= n ; i++)
	{
		cin>>a[i];
		a[i] += a[i - 1];
	}		

	int tt = -1,hh = 0;
	q[++tt] = 0;
	m--;
	for(int i = 1 ; i <= n ; i++)
	{
		while(hh <= tt && q[hh] < i - m)
		{
			hh++;
		}
		while(hh <= tt && g(q[tt]) <= g(i))
		{
			tt--;
		}
		q[++tt] = i;
		dp[i] = max(dp[i - 1] , a[i] + g(q[hh]));
	}

	cout<<a[n] - dp[n];
}

这一类题型其实和之前的修剪草坪是一模一样的,只不过需要完全反向来考虑,因此可以用两种办法解决这一类题目,该题用的是修剪草坪的办法,我用该题的思路反着写了一遍修剪草坪

#define yyy cout<<"Yes"<<"\n"
#define nnn	cout<<"No"<<"\n"
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) 
#define x first 
#define y second 
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll , ll> pii;
typedef pair <int , char> pic;
const int N = 2e6 + 10,inf = 0x3f3f3f3f,mod = 1e9 + 7;

int n,k;
ll a[N],dp[N];
int q[N];

int main()
{
	IOS;
	
	cin>>n>>k;
	ll sum = 0;
	for(int i = 1 ; i <= n ; i++)
	{
		cin>>a[i];
		sum += a[i];
	}		

	int tt = -1,hh = 0;
	q[++tt] = 0;
	k++;
	for(int i = 1 ; i <= n ; i++)
	{
		while(hh <= tt && q[hh] < i - k)
		{
			hh++;
		}
		dp[i] = dp[q[hh]] + a[i];
		while(hh <= tt && dp[q[tt]] >= dp[i])
		{
			tt--;
		}
		q[++tt] = i;
	}

	ll minn = 1e18;
	for(int i = n - k + 1 ; i <= n ; i++)
	{
		minn = min(minn , dp[i]);
	}

	cout<<sum - minn;
}

明天彻底结束单调队列dp,然后学习数位dp,之后把洛谷上相关题单刷完,之后就开始学习高级数据结构,明天继续学习web,断了蛮多天了。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值