很久之前做的了,已经想不起思路了,容我日后补充。
1 #include <stdio.h> 2 #include <deque> 3 #include <algorithm> 4 #define inf 1000000000 5 using namespace std; 6 int n, L, a, b, cnt = 1; 7 int f[1000010]; 8 bool d[1000010]; 9 struct node 10 { 11 int s, e; 12 }p[1001], P[1001]; 13 bool cmp(const node &a, const node &b) 14 { 15 if(a.s != b.s) return a.s < b.s; 16 else return a.e > b.e; 17 } 18 int main() 19 { 20 int beg,end,i,j,t; 21 scanf("%d%d%d%d", &n, &L, &a, &b); 22 for(i = 1; i <= n; ++i) 23 scanf("%d%d", &p[i].s, &p[i].e); 24 sort(p + 1, p + n + 1, cmp); 25 P[1] = p[1]; 26 for(i = 2; i <= n; ++i) 27 if(p[i].e > p[i - 1].e) 28 P[++cnt] = p[i]; 29 beg = end = 0; 30 for(i = 1; i <= cnt; ++i) 31 { 32 if(P[i].s >= end) 33 { 34 for(j = beg + 1; j <= end - 1; ++j) 35 d[j] = 1; 36 beg = P[i].s; 37 end = P[i].e; 38 } 39 else end = P[i].e; 40 } 41 for(j = beg + 1; j <= end - 1; ++j) 42 d[j] = 1; 43 f[0] = 0; 44 deque<int>Q; 45 for(i = 1; i <= L; ++i) 46 { 47 f[i] = inf; 48 while(!Q.empty() && Q.front() < i - 2 * b) 49 Q.pop_front(); 50 if(!d[i] && i % 2 == 0) 51 { 52 if(!Q.empty()) 53 f[i] = min(f[i], f[Q.front()] + 1); 54 } 55 t = i - 2 * a + 1; 56 if(t >= 0 && !d[t] && t % 2 == 0) 57 { 58 while(!Q.empty() && f[t] < f[Q.back()]) 59 Q.pop_back(); 60 Q.push_back(t); 61 } 62 } 63 if(f[L] == inf) puts("-1"); 64 else printf("%d\n", f[L]); 65 return 0; 66 }