题意特难懂,我看了好多遍,最后还是看讨论版里别人的问答,才搞明白题意,真是汗。

其实题目等价于给n个点,这n个点均匀分布在一个圆上(知道圆半径),点与点之间的路程(弧长)已知,点是有权值的,已知,点与点的距离等于其最短路程(弧长)加上两点的权值,问距离最远的两个点的下标。

因为是环状,不好处理,所以我在输入的时候就简单处理了一下,使问题变成直线上的等价问题了。做法就是在输入序列后面再加上前半段序列,例如样例5 2 1 10 1 10 10,可以处理成1 10 1 10 10 1 10,这样就只需要顺序处理了,不过最后输成的下标是需要处理的,因为要求的是字典序最小的下标对。

变成直线后的问题就比较简单了。便于理解的做法是维护一个长度为半圆的队列,当处理i时,把队列里的点和i点比较,更新最值,然后i入队,队列头元素出队。这是n^2的效率,换成单调队列,就能过了。不过,我是用优先队列做的,代码比用单调队列稍复杂一丁点,也是脑子木了,还没打完,金牛就把我代码要了去用单调队列接着打了。先贴上金牛的单调队列的代码,再贴我的,都是1a:

  1. /*
  2. * Author : ben
  3. */
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <queue>
  12. #include <set>
  13. #include <map>
  14. #include <stack>
  15. #include <string>
  16. #include <vector>
  17. #include <deque>
  18. #include <list>
  19. #include <functional>
  20. #include <numeric>
  21. #include <cctype>
  22. using namespace std;
  23.  
  24. #define D(x)
  25.  
  26. typedef long long LL;
  27. const int MAXN = ;
  28.  
  29. int R, N;
  30. LL m;
  31. int data[MAXN];
  32. LL ans;
  33. int ansi, ansj;
  34.  
  35. void update(LL a, LL b)
  36. {
  37. LL temp = (b - a) * R + data[a] + data[b];
  38. a %= N - m;
  39. b %= N - m;
  40. if (a > b)
  41. swap(a, b);
  42. if (temp > ans)
  43. {
  44. D(printf("a %lld b %lld\n", a, b));
  45. ans = temp;
  46. ansi = a;
  47. ansj = b;
  48. return;
  49. }
  50. if (temp < ans)
  51. return;
  52. if (ans == )
  53. if (ansi > a || (ansi == a && ansj > b))
  54. {
  55. ansi = a;
  56. ansj = b;
  57. return;
  58. }
  59. }
  60.  
  61. LL work() {
  62.  
  63. deque<LL> q;
  64. q.push_back();
  65. long long cur_pos = ;
  66. long long ret = ;
  67. for (int i = ; i < N; i++)
  68. {
  69. cur_pos += R;
  70. while (!q.empty() && q.front() < i - m)
  71. q.pop_front();
  72. update(q.front(), i);
  73. while (!q.empty() && (i - q.back()) * R + data[q.back()] < data[i])
  74. q.pop_back();
  75. q.push_back(i);
  76. }
  77. return ret;
  78. }
  79.  
  80. int main() {
  81. int T;
  82. scanf("%d", &T);
  83. for (int t = ; t <= T; t++) {
  84. scanf("%d %d", &N, &R);
  85. for (int i = ; i < N; i++) {
  86. scanf("%d", &data[i]);
  87. }
  88. m = N / ;
  89. for (int i = ; i < m; i++) {
  90. data[i + N] = data[i];
  91. }
  92. N += m;
  93. ans = ;
  94. work();
  95. printf("Case #%d\n%d %d\n", t, ansi + , ansj + );
  96. }
  97. return ;
  98. }

下面是我的优先队列的:

  1. /*
  2. * Author : ben
  3. */
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <queue>
  12. #include <set>
  13. #include <map>
  14. #include <stack>
  15. #include <string>
  16. #include <vector>
  17. #include <deque>
  18. #include <list>
  19. #include <functional>
  20. #include <numeric>
  21. #include <cctype>
  22. using namespace std;
  23. typedef long long LL;
  24. const int MAXN = ;
  25. typedef struct Mont {
  26. int height;
  27. int index;
  28. LL value;
  29. Mont(int ii = , int hh = , LL vv = ) {
  30. height = hh;
  31. index = ii;
  32. value = vv;
  33. }
  34. } Mont;
  35. bool inline operator<(const Mont& m1, const Mont& m2) {
  36. return m1.value < m2.value;
  37. }
  38. LL R;
  39. int N, m;
  40. int data[MAXN];
  41. LL ans;
  42. int ansi, ansj;
  43.  
  44. inline void treat(int &i, int &j) {
  45. if (i >= N - m) {
  46. i = i % (N - m);
  47. }
  48. if (j >= N - m) {
  49. j = j % (N - m);
  50. }
  51. if (i > j) {
  52. int t = i;
  53. i = j;
  54. j = t;
  55. }
  56. }
  57.  
  58. void work() {
  59. priority_queue<Mont> mont;
  60. mont.push(Mont(, data[], data[]));
  61. for (int i = ; i < N; i++) {
  62. while (mont.top().index < (i - m)) {
  63. mont.pop();
  64. }
  65. Mont topm = mont.top();
  66. LL tans = (i - topm.index) * R + data[i] + data[topm.index];
  67. if (tans > ans) {
  68. ans = tans;
  69. ansi = topm.index;
  70. ansj = i;
  71. treat(ansi, ansj);
  72. } else if(tans == ans) {
  73. int x = topm.index;
  74. int y = i;
  75. treat(x, y);
  76. if (x < ansi || (x == ansi && y < ansj)) {
  77. ansi = x;
  78. ansj = y;
  79. }
  80. }
  81. mont.push(Mont(i, data[i], data[i] - i * R));
  82. }
  83. }
  84.  
  85. int main() {
  86. int T;
  87. scanf("%d", &T);
  88. for (int t = ; t <= T; t++) {
  89. scanf("%d %lld", &N, &R);
  90. for (int i = ; i < N; i++) {
  91. scanf("%d", &data[i]);
  92. }
  93. m = N / ;
  94. for (int i = ; i < m; i++) {
  95. data[i + N] = data[i];
  96. }
  97. N += m;
  98. ans = ;
  99. work();
  100. printf("Case #%d:\n%d %d\n", t, ansi + , ansj + );
  101. }
  102. return ;
  103. }

hdu5261单调队列的更多相关文章

  1. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  2. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  3. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  4. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  5. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  6. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  7. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. 【转】单调队列优化DP

    转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...

  9. hdu3530 单调队列

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. linux内核学习之四:进程切换简述【转】

    转自:http://www.cnblogs.com/xiongyuanxiong/p/3531884.html 在讲述专业知识前,先讲讲我学习linux内核使用的入门书籍:<深入理解linux内 ...

  2. git 中遇到的问题

    刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...

  3. AC日记——Milking Grid poj 2185

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8314   Accepted: 3586 Desc ...

  4. es6 --数组--Array.from() 、Array.isArray()、Array.of()、find()、findIndex()、fill()、entries() 、keys() ,values()

    将两类对象转为真正的数组 Array.from()方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Se ...

  5. 0.从零开始搭建spring mvc + mybatis + memcached+ dubbo\zookper的maven项目

    1.首先创建maven 项目,配置相关pom信息 2.配置spring mvc 4, 测试,提交代码 3.引入配置mybatis3,测试,提交代码 4.配置事务,测试,提交代码 5.配置memcach ...

  6. javascript好文 --- 深入理解可视区尺寸client

    可视区大小 可视区大小client又称为可见大小或客户区大小,指的是元素内容及其内边距所占据的空间大小 clientHeight clientHeight属性返回元素节点的可见高度 clientHei ...

  7. Examples osgparticleshader例子学习

    Examples osgparticleshader  粒子与shader的使用 参考文档 http://blog.csdn.net/csxiaoshui/article/details/234345 ...

  8. C---指针篇

    指针变量:专门存放内存地址的一种变量 听说C因为指针而强大 一段代码来解释 指针 *指针 &指针 &指向变量 的关系 /* * 返回指针所指向内存地址中存放的值 它是单目运算符 也称作 ...

  9. 基于bootstrap的纯静态网站目录

    一.博客页面 二.登陆页面 三.信息采集 四.管理后台 五.网站汇总(基于上边四个功能) 因为样式统一采用bootstrap的样式,所以不做介绍 样式导入可以将bootstrap下载至本地(有自动补齐 ...

  10. nyoj84 阶乘的0

    阶乘的0 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示測试数据的组数(1<=N<=1 ...