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

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

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

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

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std; #define D(x) typedef long long LL;
const int MAXN = ; int R, N;
LL m;
int data[MAXN];
LL ans;
int ansi, ansj; void update(LL a, LL b)
{
LL temp = (b - a) * R + data[a] + data[b];
a %= N - m;
b %= N - m;
if (a > b)
swap(a, b);
if (temp > ans)
{
D(printf("a %lld b %lld\n", a, b));
ans = temp;
ansi = a;
ansj = b;
return;
}
if (temp < ans)
return;
if (ans == )
if (ansi > a || (ansi == a && ansj > b))
{
ansi = a;
ansj = b;
return;
}
} LL work() { deque<LL> q;
q.push_back();
long long cur_pos = ;
long long ret = ;
for (int i = ; i < N; i++)
{
cur_pos += R;
while (!q.empty() && q.front() < i - m)
q.pop_front();
update(q.front(), i);
while (!q.empty() && (i - q.back()) * R + data[q.back()] < data[i])
q.pop_back();
q.push_back(i);
}
return ret;
} int main() {
int T;
scanf("%d", &T);
for (int t = ; t <= T; t++) {
scanf("%d %d", &N, &R);
for (int i = ; i < N; i++) {
scanf("%d", &data[i]);
}
m = N / ;
for (int i = ; i < m; i++) {
data[i + N] = data[i];
}
N += m;
ans = ;
work();
printf("Case #%d\n%d %d\n", t, ansi + , ansj + );
}
return ;
}

下面是我的优先队列的:

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
const int MAXN = ;
typedef struct Mont {
int height;
int index;
LL value;
Mont(int ii = , int hh = , LL vv = ) {
height = hh;
index = ii;
value = vv;
}
} Mont;
bool inline operator<(const Mont& m1, const Mont& m2) {
return m1.value < m2.value;
}
LL R;
int N, m;
int data[MAXN];
LL ans;
int ansi, ansj; inline void treat(int &i, int &j) {
if (i >= N - m) {
i = i % (N - m);
}
if (j >= N - m) {
j = j % (N - m);
}
if (i > j) {
int t = i;
i = j;
j = t;
}
} void work() {
priority_queue<Mont> mont;
mont.push(Mont(, data[], data[]));
for (int i = ; i < N; i++) {
while (mont.top().index < (i - m)) {
mont.pop();
}
Mont topm = mont.top();
LL tans = (i - topm.index) * R + data[i] + data[topm.index];
if (tans > ans) {
ans = tans;
ansi = topm.index;
ansj = i;
treat(ansi, ansj);
} else if(tans == ans) {
int x = topm.index;
int y = i;
treat(x, y);
if (x < ansi || (x == ansi && y < ansj)) {
ansi = x;
ansj = y;
}
}
mont.push(Mont(i, data[i], data[i] - i * R));
}
} int main() {
int T;
scanf("%d", &T);
for (int t = ; t <= T; t++) {
scanf("%d %lld", &N, &R);
for (int i = ; i < N; i++) {
scanf("%d", &data[i]);
}
m = N / ;
for (int i = ; i < m; i++) {
data[i + N] = data[i];
}
N += m;
ans = ;
work();
printf("Case #%d:\n%d %d\n", t, ansi + , ansj + );
}
return ;
}

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. OSU!(bzoj 4318)

    Description osu 是一款群众喜闻乐见的休闲软件.  我们可以把osu的规则简化与改编成以下的样子:  一共有n次操作,每次操作只有成功与失败之分,成功对应1,失败对应0,n次操作对应为1 ...

  2. 剑指Offer 二进制中一的个数

    链接:https://www.nowcoder.com/questionTerminal/8ee967e43c2c4ec193b040ea7fbb10b8 来源:牛客网 public class So ...

  3. [Codeforces Round #170 Div. 1] 277A Learning Languages

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes input standa ...

  4. Codevs 数字三角形 问题合集

    1220 数字三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得 ...

  5. 【CF676D】Theseus and labyrinth(BFS,最短路)

    题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门.每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等.现在给出起点和终点,每 ...

  6. C语言实验报告二

    实验一:第11次实验作业报告 题目:方阵循环右移 实验要求:将给定n×n方阵中的每个元素循环向右移m个位置,即将第0.1.⋯.n−1列变换为第n−m.n−m+1.⋯.n−1.0.1.⋯.n−m−1列. ...

  7. Error querying database找不到数据库的错误可能发生的原因..

    这个问题纠结了大概两个小时.原因是这样的,我刚刚换了一台新的电脑,准备把以前电脑上自己搭建的小项目放到新电脑上面,用myeclipse引入项目之后,启动项目在浏览器跑起来.然后输入账号密码登录主页,报 ...

  8. HDU 1018.Big Number-Stirling(斯特林)公式 取N阶乘近似值

    最近一堆题目要补,一直咸鱼,补了一堆水题都没必要写题解.备忘一下这个公式. Stirling公式的意义在于:当n足够大时,n!计算起来十分困难,虽然有很多关于n!的等式,但并不能很好地对阶乘结果进行估 ...

  9. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  10. django使用logging记录日志

    django使用logging记录日志,我没有用这方式去记录日志,主要还是项目小的原因吧, 有机会遇见大项目的话可以回头研究. 配置setting.py配置文件 import logging impo ...