传送门:CodeForces-1253E
思路分析
设$dp[i]$表示前$1-i$已经覆盖,要覆盖$i+1$到$m$的最小花费,边界$dp[m]=0$
从后往前枚举,如果$i+1$已经被覆盖,$dp[i]=dp[i+1]$,可以直接继承
如果一条i小于某条线段的左端点,那么可以转移这条线段,覆盖$i-l$的代价为$cost$
那么转移就是$dp[i]=$$min$$(dp[i],dp[r+cost]+cost)$
样例输入
3 595
43 2
300 4
554 10
样例输出
281
AC代码
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
int dp[N];
pair<int,int>a[100];
int main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int n,m;
read(n,m);
for(int i=1;i<=n;i++){
int x,s;
read(x,s);
a[i].fi=max(0,x-s);
a[i].se=min(m,x+s);
}
dp[m]=0;
for(int i=m-1;i>=0;i--){
dp[i]=m-i;
for(int j=1;j<=n;j++){
int l=a[j].fi,r=a[j].se;
if(i+1>=l && i+1<=r) {
dp[i]=dp[i+1];
break;
}
if(i<l){
int cost=(l-i-1);
int rr=min(m,r+cost);
dp[i]=min(dp[i],dp[rr]+cost);
}
}
}
cout<<dp[0]<<endl;
return 0;
}