日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

zoj 3632 Watermelon Full of Water

發布時間:2023/12/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zoj 3632 Watermelon Full of Water 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4778

?

題目大意:

暑假有n(1~50000)天,第i天西瓜的價格是p[i],可以吃d[i]天,如果在第i天買了個西瓜,那么舊的西瓜要扔掉,開始吃新西瓜。

問每天都能吃到西瓜的最小花費。

?

?

題目思路:

p[i]表示第i天買喜歡的花費,d[i]表示第i天買的西瓜能吃的天數。

dp[i],表示以i為起點,最少要花費多少錢才能使i~n每天都有西瓜吃。

狀態轉移很顯然dp[i]= (i+d[i]>n)? p[i] : min{dp[i+1]~dp[i+d[i]]}+p[i]。

很明顯在取dp[i+1]~dp[i+d[i]]的過程顯然不可以從i+1 ~ i+d[i]掃一遍,這樣復雜度O(n*n),會超時。

所以最直接的方法就是用線段樹優化。

?

代碼:

#include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <math.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std;#define ll long long #define ls rt<<1 #define rs ls|1 #define lson l,mid,ls #define rson mid+1,r,rs #define middle (l+r)>>1 #define clr_all(x,c) memset(x,c,sizeof(x)) #define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1)) #define eps (1e-8) #define MOD 1000000007 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #pragma comment(linker, "/STACK:102400000,102400000") template <class T> T _max(T x,T y){return x>y? x:y;} template <class T> T _min(T x,T y){return x<y? x:y;} template <class T> T _abs(T x){return (x < 0)? -x:x;} template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;} template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;} template <class T> void getmax(T& x,T y){x=(y > x)? y:x;} template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;} int TS,cas=1; const int M=50000+5; ll n,p[M],d[M],mmin[M<<2],MAX; void build(int l,int r,int rt){mmin[rt]=MAX;if(l==r) return;int mid=middle;build(lson),build(rson); } void pushUp(int rt){mmin[rt]=_min(mmin[ls],mmin[rs]); } void update(int l,int r,int rt,int p,ll c){if(l==r){mmin[rt]=c;return;}int mid=middle;if(p<=mid) update(lson,p,c);else update(rson,p,c);pushUp(rt); } ll query(int l,int r,int rt,int L,int R){if(L<=l && r<=R) return mmin[rt];int mid=middle;if(R<=mid) return query(lson,L,R);else if(mid<L) return query(rson,L,R);else return _min(query(lson,L,mid),query(rson,mid+1,R)); }void run(){int i,j;MAX=1;for(i=1;i<=n;i++) scanf("%lld",&p[i]),MAX+=p[i];for(i=1;i<=n;i++) scanf("%lld",&d[i]);ll ret,tmp;build(1,n,1);for(i=n;i>=1;i--){int l=i+1,r=i+d[i];if(r>n) ret=p[i];else ret=query(1,n,1,i+1,i+d[i])+p[i];update(1,n,1,i,ret);}printf("%lld\n",ret); }void preSof(){ }int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);preSof();//run();while((~scanf("%d",&n))) run();//for(scanf("%d",&TS);cas<=TS;cas++) run();return 0; }


?

?

轉載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/17/3026934.html

總結

以上是生活随笔為你收集整理的zoj 3632 Watermelon Full of Water的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。