题意: n个线段,每个线段有覆盖面积,你可以将线段左右扩展长度1,花费1单位。求最少花费使得线段被完全覆盖。
- dp[pos] :从1到pos已覆盖,要覆盖pos+1到m最小花费 起点dp[len]=0
- 枚举pos 由dp含义可知 dp[pos]=len-pos(pos处已覆盖,要继续覆盖
- 可以pos为起点扩张,len-pos为最大值)状态转移: 枚举天线
- 若pos+1处已位于某天线范围内,则该点已成立,dp[pos]=dp[pos+1]
- 否则 若pos小于某天线范围,计算该天线扩到此处花费+dp[min(len,cost+r)]取最小
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
int qpow(int m, int k, int mod)
{
int res = 1, t = m;
while (k)
{
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}
const int MAX_N=1e5+5;
int n,len,pos;
int dp[MAX_N];
int i,j,k;
struct node
{
int l,r;
} p[100];
int main()
{
scanf("%d %d",&n,&len);
for(i=1;i<=n;i++)
{
int x,s;
scanf("%d %d",&x,&s);
p[i].l=max(0,x-s),p[i].r=min(len,x+s);
}
dp[len]=0;
for(pos=len-1; pos>=0; pos--)
{
dp[pos]=len-pos;
for(i=1;i<=n;i++)
{
int l=p[i].l,r=p[i].r;
if(l<=pos+1&&pos+1<=r)
{
dp[pos]=dp[pos+1];
break;
}
if(pos<l)
{
int cost=(l-pos-1);///覆盖到左区间的花费
int rr=min(len,r+cost);///右区间不能超界
dp[pos]=min(dp[pos],cost+dp[rr]);
}
}
}
cout<<dp[0]<<endl;
return 0;
}