标签:dp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3798 | Accepted: 1363 |
Description
Input
Output
Sample Input
2 8 1 2 6 7 3 6
Sample Output
3
Hint
|-----c2----|-c1| cows‘ preferred ranges |---1---|-------2-------|---3---| sprinklers +---+---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 7 8
Source
参考博客 http://www.2cto.com/kf/201208/147513.html
上面博客已经比较清楚
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8 typedef long long ll; using namespace std; #define N 1000005 #define INF 0x3f3f3f3f struct stud{ int le,ri,va; }f[N*4]; int lena,lenb; int a[N]; int n,all; inline void pushup(int pos) { f[pos].va=min(f[L(pos)].va,f[R(pos)].va); } void build(int pos,int le,int ri) { f[pos].le=le; f[pos].ri=ri; if(le==ri) { f[pos].va=a[le]; return ; } int mid=MID(le,ri); build(L(pos),le,mid); build(R(pos),mid+1,ri); pushup(pos); } void update(int pos,int le,int va) { if(f[pos].le==le&&f[pos].ri==le) { f[pos].va=va; return ; } int mid=MID(f[pos].le,f[pos].ri); if(mid>=le) update(L(pos),le,va); else update(R(pos),le,va); pushup(pos); } int query(int pos,int le,int ri) { if(f[pos].le==le&&f[pos].ri==ri) return f[pos].va; int mid=MID(f[pos].le,f[pos].ri); if(mid>=ri) return query(L(pos),le,ri); else if(mid<le) return query(R(pos),le,ri); return min(query(L(pos),le,mid),query(R(pos),mid+1,ri)); } int solve() { int i,j; if(all&1) return -1; if(lenb<1) return -1; for(i=1;i<=all;i+=2) a[i]=all+1; a[0]=0; build(1,0,all); for(i=2;i<=all;i+=2) { if(a[i]>all) continue; int le=i-lenb*2; int ri=i-lena*2; le=max(0,le); if(ri<0) continue; int temp=query(1,le,ri); a[i]=temp+1; update(1,i,a[i]); } if(a[all]>all) return -1; return a[all]; } int main() { int i,j; while(~scanf("%d%d",&n,&all)) { scanf("%d%d",&lena,&lenb); int s,e; for(i=0;i<=all;i++) a[i]=all; while(n--) { scanf("%d%d",&s,&e); for(i=s+1;i<e;i++) a[i]=all+1; } a[0]=0; printf("%d\n",solve()); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2373 Dividing the Path(线段树+dp)
标签:dp
原文地址:http://blog.csdn.net/u014737310/article/details/48084639