大法师与魔法石

思路:

对于一个ai, 它可以构成区间[ai/v, ai]

假设和它相邻的为aj, 那么ai 和 aj 构成的区间为[(ai+aj) / v, ai+aj]

那么这两个区间能合并的条件是 (ai + aj) / v <= ai

即aj <= (v - 1)ai (v >= 2)

又因为(v - 1) ai >= ai

所以aj <= ai

所以把每个ai和它相邻的比它小的数连接起来考虑

用单调栈求出每个位置之前和之后第一个比它大的数就可以了

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
int a[N], pre[N], nxt[N];
pair<LL, LL> q[N];
LL sum[N];
stack<int> st;
int main() {
int T, n, v;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &v);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
a[] = INT_MAX;
a[n+] = INT_MAX;
while(!st.empty()) st.pop();
st.push();
for (int i = ; i <= n; i++) {
while(a[st.top()] <= a[i]) st.pop();
pre[i] = st.top();
st.push(i);
}
while(!st.empty()) st.pop();
st.push(n+);
for (int i = n; i >= ; i--) {
while(a[st.top()] <= a[i]) st.pop();
nxt[i] = st.top();
st.push(i);
}
for (int i = ; i <= n; i++) sum[i] = sum[i-] + a[i];
for (int i = ; i <= n; i++) {
q[i].fi = (a[i] + v - ) / v;
q[i].se = sum[nxt[i]-] - sum[pre[i]];
}
sort(q+, q++n);
LL ans = ;
q[].se = -;
for (int i = ; i <= n; i++) {
ans += q[i].se - q[i].fi + ;
if(q[i].fi <= q[i-].se) ans -= q[i-].se - q[i].fi + ;
}
printf("%lld\n", ans);
}
return ;
}

牛客OI周赛6-提高组 A 大法师与魔法石的更多相关文章

  1. 牛客OI周赛9-提高组题目记录

    牛客OI周赛9-提高组题目记录 昨天晚上做了这一套比赛,觉得题目质量挺高,而且有一些非常有趣而且非常清奇的脑回路在里边,于是记录在此. T1: 扫雷 题目链接 设 \(f_i\) 表示扫到第 \(i\ ...

  2. 牛客OI周赛8-提高组A-用水填坑

    牛客OI周赛8-提高组A-用水填坑 题目 链接: https://ac.nowcoder.com/acm/contest/403/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制: ...

  3. 牛客OI周赛2-提高组

    A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...

  4. 牛客OI周赛11-普及组 B Game with numbers (数学,预处理真因子)

    链接:https://ac.nowcoder.com/acm/contest/942/B 来源:牛客网 Game with numbers 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C+ ...

  5. 牛客OI周赛7-提高组 A 小睿睿的等式

    链接:https://ac.nowcoder.com/acm/contest/371/A来源:牛客网 小睿睿在游戏开始时有n根火柴棒,他想知道能摆成形如“A+B=n”的等式且使用的火柴棒数也恰好等于n ...

  6. 牛客OI周赛7-提高组 B小睿睿的询问(ST打表)

    链接:https://ac.nowcoder.com/acm/contest/371/B来源:牛客网 小睿睿的n个妹纸排成一排,每个妹纸有一个颜值val[i].有m个询问,对于每一个询问,小睿睿想知道 ...

  7. 牛客OI周赛7-普及组 解题报告

    出题人好评. 评测机差评. A 救救喵咪 二位偏序.如果数据范围大的话直接树状数组,不过才1000就\(O(n^2)\)暴力就ok了. #include <bits/stdc++.h> s ...

  8. 牛客OI周赛10-普及组-A眼花缭乱的街市-(加速+二分)

    https://ac.nowcoder.com/acm/contest/901/A 很简单的一道题,全场只有20+AC,卡时间.新学了cin加速语法和数组二分查找的函数调用. 知道有个读写挂,可以加速 ...

  9. 牛客OI周赛8-普及组

    https://ac.nowcoder.com/acm/contest/543#question A. 代码: #include <bits/stdc++.h> using namespa ...

随机推荐

  1. NLTK 知识整理

    NLTK 知识整理 nltk.corpus模块自带语料 NLTK comes with many corpora, toy grammars, trained models, etc. A compl ...

  2. PHP获取Linux当前目录下文件并实现下载功能

    使用nginx转发过去给php server{ listen 9099; server_name 18.5.6.2; location / { proxy_http_version 1.1; root ...

  3. BootstrapTable(附源码)

    Bootstrap结合BootstrapTable的使用,分为两种模试显示列表. 引用的css: <link href="@Url.Content("~/Css/bootst ...

  4. 基于快速排序思想partition查找第K大的数或者第K小的数。

    快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...

  5. win7记事本txt图标显示异常解决方法

    链接:https://zhidao.baidu.com/question/2076205353435701108.html                                       ...

  6. C# Math类简介运用

    总结了一下几个常用的Math类 /* ######### ############ ############# ## ########### ### ###### ##### ### ####### ...

  7. codevs1048石子归并

    codevs1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 传送门  http://codevs.cn/problem/1048/ 题目描述 ...

  8. win32汇编(ASM)学习资源

    网站 AoGo汇编小站(MASMPlus作者) Win32Asm教程在线版 Win32Asm教程博客园文件备份版 Masm32补充教程系列 Win32 ASM Tutorial Resource Ki ...

  9. nohup 让进程在后台可靠运行的几种方法

    1. nohup nohup 无疑是我们首先想到的办法.顾名思义,nohup 的用途就是让提交的命令忽略 hangup 信号. nohup 的使用是十分方便的,只需在要处理的命令前加上 nohup 即 ...

  10. Apple ID双重认证验证码无法输入问题

    问题:用Apple ID登录老版本ios系统时,会提示“”需要提供Apple ID验证码才能登陆.请键入您的密码,并随后键入显示在您其他设备上的验证码”. 解决:老版本系统无输入验证码的地方,那就把验 ...