美夢1(JSOI2014,演算法藝術與資訊學競賽)

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   資料   for   

【問題描述】

  這天晚上,約翰做了個奇怪的美夢。他擁有了分別分布在N座高高低低的山上的N個池塘,N座山連成一條直線,從左往右第i座山的高度是Hi。池塘中的魚都是他請專家運用科學的方法專門養殖的,為了保護每個池塘的生態環境,他現在要在這N座山上建造若干個看護點。約翰是個很節約的人,在第i座山建造看護點的花費為Ci。假設在第i座山建造一個看護點,則往左或者往右第一座不比這座山低的山將擋住看護的視線。譬如說: {Hi} = {1 4 4 5 7 2}表示第一座山高度為1,第二座山高度為4。。。 如果在第1座山建造一個看護點,則可以看護第1,2兩個池塘。如果在第5座山上建造一個看護點,則左右的池塘都能被看護到。如果在第3座山上建造一個看護點,則能夠看護到第2,3,4個池塘。

Problem Task:

  要求能夠看護到所有的池塘,建造看護點的最小代價是多少。即建造看護點的山對應的花費Ci之和最小。

Problem Input:

  第一行包含一個正整數N,N滿足1<=N<=1000000。

  第二行包含N個正整數,第i個正整數Hi滿足1<=Hi<=10^9,表示第i座山的高度

  第三行包含N個正整數,第i個正整數Ci滿足1<=Ci<=10^9,表示在第i座山建造看護點的代價為Ci

Problem Output:

  一行包含一個正整數C,表示最小的代價。

Problem Input Example:

3

1 1 1

2 2 2

Problem output Example:

2

 

這題是個很裸的動歸  , 不過資料範圍有些怕人... 

Way1:設f[i] 表示 看護1~i座山所需最小花費,則顯然有f[i] = min{f[k] + C_t} (Lt<=k<=Rt = i)

不難發現可以用線段樹維護最優值,單點修改,區間查詢。

Way2: 設f[i] 表示 看護1~i座山,且第i座山上修建看護點 ,則顯然有 f[i] = min{f[j]} + C_i (R_j >= L_i )

不難發現也可以偶那個線段樹維護,單點查詢,區間修改

Codes:

 1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<vector> 5 #include<cstring> 6 #include<cstdlib> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std;10 const int N = 1000010;11 typedef long long lld;12 const lld inf = lld(21474836470000000);13 #define Ch1 (i<<1)14 #define Ch2 (Ch1|1)15 #define For(i,n) for(int i=1;i<=n;i++)16 #define Rep(i,l,r) for(int i=l;i<=r;i++)17 #define Down(i,r,l) for(int i=r;i>=l;i--)18 struct tnode{19     int l,r,mid;20     lld min;21 }T[N<<2];22 int Left[N],Right[N],top,n,H[N],C[N],st[N];23 lld f[N];24 25 void read(int &v){26     int num = 0; char ch = getchar();27     while(ch>‘9‘||ch<‘0‘) ch = getchar();28     while(ch>=‘0‘&&ch<=‘9‘){29         num = num * 10 + ch - ‘0‘;30         ch = getchar();31     }32     v = num;33 }34 35 void init(){36     read(n);37     For(i,n) read(H[i]);38     For(i,n) read(C[i]);39     st[top=1] = 0;H[0] = H[n+1] = 2147483647;40     For(i,n){41         while(H[st[top]]<H[i]) top--;42         Left[i] = max(st[top],1);43         st[++top] = i;44     }45     st[top=1] = n+1;46     Down(i,n,1){47         while(H[st[top]]<H[i]) top--;48         Right[i] = min(st[top],n);49         st[++top] = i;50     }51 }52 53 void Build(int l,int r,int i){54     T[i].l = l;T[i].r = r;T[i].mid = (l+r)>>1;55     T[i].min = inf;56     if(l==r) return;57     Build(l,T[i].mid,Ch1); Build(T[i].mid+1,r,Ch2);58 }59 vector< pair<int,int> > G[N];60 //f[i] = min{f[t] + cost[k]}61 62 void Modify(int i,int x,lld delta){63     if(T[i].l==T[i].r){64         T[i].min = delta;65         return;66     }67     if(x<=T[i].mid)  Modify(Ch1,x,delta);68     else             Modify(Ch2,x,delta);69     T[i].min = min(T[Ch1].min,T[Ch2].min);70 }71 72 lld query(int l,int r,int i){73     if(l<=T[i].l&&T[i].r<=r) return T[i].min;74     if(r<=T[i].mid)  return query(l,r,Ch1);75     if(l>T[i].mid)   return query(l,r,Ch2);76     return min(query(l,T[i].mid,Ch1) , query(T[i].mid+1,r,Ch2));77 }78 79 int main(){80     freopen("fish1.in","r",stdin);81     freopen("fish1.out","w",stdout);82     init();83     Build(0,n,1);84     Modify(1,0,0);85     For(i,n){86         G[Right[i]].push_back(make_pair(Left[i],C[i]));87         G[i].push_back(make_pair(Left[i],C[i]));88     }89     For(i,n){90         f[i] = inf;91         for(int j = 0;j<G[i].size();j++) 92            f[i] = min(f[i],query(G[i][j].first-1,i,1) + G[i][j].second);93         Modify(1,i,f[i]);94     }95     cout<<f[n]<<endl;96     return 0;97 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.