题目戳这——> https://www.luogu.com.cn/problem/P3957
如果g个金币分数大于k,则g+1个金币分数大于k
同理,如果g个金币分数小于k,则g-1个金币分数小于k
所以可以二分g。
明显此题可以用动态规划
dp[i]=max{dp[j]+z[i]}(min<=x[i]-x[j]<=max)
min=max(d-g,1),max=d+g.
由于每次都需求(min,max)中的最大值。
所以可以单调队列优化
代码如下
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
using namespace std;
int n,d,k;
struct node
{
int x,z;
}a[500005];
int ans=-1;
long long dp[500005];
int q[500005