码迷,mamicode.com
首页 > 其他好文 > 详细

[TyvjP1313] [NOIP2010初赛]烽火传递(单调队列 + DP)

时间:2017-05-15 13:10:15      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:return   span   while   target   click   print   code   log   http   

传送门

 

就是个单调队列+DP嘛。

 

——代码

技术分享
 1 #include <cstdio>
 2 
 3 const int MAXN = 1000001;
 4 int n, m, h = 1, t = 1, ans = ~(1 << 31);
 5 int q[MAXN], a[MAXN], f[MAXN];
 6 
 7 inline int min(int x, int y)
 8 {
 9     return x < y ? x : y;
10 }
11 
12 int main()
13 {
14     int i;
15     scanf("%d %d", &n, &m);
16     for(i = 1; i <= n; i++) scanf("%d", &a[i]);
17     for(i = 1; i <= n; i++)
18     {
19         while(h <= t && q[h] < i - m) h++;
20         f[i] = f[q[h]] + a[i];
21         while(h <= t && f[q[t]] > f[i]) t--;
22         q[++t] = i;
23      }
24      for(i = n - m + 1; i <= n; i++) ans = min(ans, f[i]);
25      printf("%d\n", ans);
26      return 0;
27 }
View Code

 

[TyvjP1313] [NOIP2010初赛]烽火传递(单调队列 + DP)

标签:return   span   while   target   click   print   code   log   http   

原文地址:http://www.cnblogs.com/zhenghaotian/p/6855668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!