【单调栈】hdu6325 Problem G. Interstellar Travel

从后往前更新,维护一个递减单调栈(队列)

最近很多题都是单调栈。。。

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
//#include<deque>
using namespace std;
const int maxn = 1e7 + 5;
int n, m, k, P, Q, R, MOD,T;
int a[maxn];
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))

int main() {
        scanf("%d", &T);
        while (T--) {
            deque<int> que;
            long long A = 0, B = 0,cnt=0,mx=-1;

            scanf("%d%d%d%d%d%d%d", &n, &m, &k, &P, &Q, &R, &MOD);
            rep(i,1,k)scanf("%d", &a[i]);
            rep(i,k+1,n)a[i] = (1LL * P*a[i - 1] + 1LL * Q*i + R) % MOD;
            per(i, n,1) {
                while (!que.empty() && que.back() <= a[i])que.pop_back();
                que.push_back(a[i]);
                if (i <= n - m + 1) {
                    if (a[i + m ] == que.front())que.pop_front();
                    A += i ^ que.front();
                    B += i ^ (que.size());
                }
            }

            printf("%lld %lld\n", A, B);
    }
        cin >> n;
}

原文地址:https://www.cnblogs.com/SuuT/p/9397641.html

时间: 2024-10-25 00:33:26

【单调栈】hdu6325 Problem G. Interstellar Travel的相关文章

2018 Multi-University Training Contest 3 1007 / hdu6325 Problem G. Interstellar Travel 凸包

Problem G. Interstellar Travel 题意: 给定平面上n个点,起点1 为(0,0),终点 n 为(Xn, 0),其它点的横坐标 0 <Xi<Xn,纵坐标 Xi >=0.每次可以飞到一个横坐标严格更大的点,代价为两个坐标的叉积.求起点到终点总代价最小的飞行路线,并输出字典序最小的路线.2≤n≤200000. Shortest judge solution: 979 bytes 题解: 显然坐标相同的点里只保留编号最小的点最优. 将起点到终点的路径补全为终点往下走到

hdu6325 Interstellar Travel 凸包变形

题目传送门 题目大意: 给出n个平面坐标,保证第一个点和第n个点y值为0,其余点的x坐标都在中间,要从 i 点走到 j 点的要求是 i 点的横坐标严格小于 j 的横坐标,并且消耗的能量是(xi * yj - xj * yi),要求消耗的能量最小(能量可以为负),并且字典序要求最小. 思路: 消耗能量的式子就是两个坐标的叉积,叉积的几何意义就是两个向量对应的平行四边形的面积,但是这个面积会有正负,如果向量 j 在向量 i 的顺时针方向,则这个面积是负的,如果我希望能量最小,那么就尽可能使向量是顺时

POJ2452---Sticks Problem(单调栈+RMQ,方法不够优秀)

Description Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented by S1, S2, S3, -Sn. After measuring the length of each stick Sk (1 <= k <= n), she finds that for some sticks Si and Sj (1<= i < j &

Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)

#include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];int mx[4000007];int sum[4000007];int head[1000007],to[2000007],nex[2000007];int n,k;int a[10000077];int dfn;int tot;void pushup(int rt){    mx[rt]=max(mx[rt

G - Most Dangerous Shark dp 单调栈 单调队列

题意: 一条线段上有n张骨牌(n=1e7), 相邻骨牌距离为1,每张骨牌有其高度和推倒的花费,问最少的花费推倒所有的骨牌. 题解: 首先用单调栈维护每个位置往左(右)推能推倒的最远的骨牌 dp[i]表示1-i倒下的最小花费 转移显然只有两种  一种是第i张往左推动 另一种是找到往右推动能推倒i的最小花费的地方进行转移  这里可以用单调栈维护最小值 #include<bits/stdc++.h> using namespace std; typedef long long ll; const i

hdu1506---Largest Rectangle in a Histogram(单调栈)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

HDU 5033 Building(北京网络赛B题) 单调栈 找规律

做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1257    Accepted Submission(s): 358 Special Judg

Gym 100971D Laying Cables 单调栈

Description One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it was decided to place the main server

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the