当前位置:网站首页>2021hncpc-e-difference, thinking
2021hncpc-e-difference, thinking
2022-07-25 15:34:00 【Brother Tazi is here】
The main idea of the topic :
Solve each f ( i ) f(i) f(i) representative m a x ( a j , . . . , a i ) − ( j − i + 1 ) ≥ m max(a_j,...,a_i)-(j-i+1) \geq m max(aj,...,ai)−(j−i+1)≥m Of j j j The number of . j ≤ i j \leq i j≤i
n ≤ 1 e 6 n \leq 1e6 n≤1e6
Ideas :
The key : notice m a x ( a j , . . . , a i ) max(a_j,...,a_i) max(aj,...,ai), Think of enumerating a i a_i ai, Consider its jurisdiction ( Monotone stack solution ).
PS: When there is the same value , It can be specified that the interval contribution is recorded on the rightmost value .
Then it turns into ( j − i + 1 ) ≤ m a x ( a j , . . . , a i ) − m (j-i+1) \leq max(a_j,...,a_i)-m (j−i+1)≤max(aj,...,ai)−m.
Then enumerate the right half , Discuss according to the situation Interval plus isochronous sequence that will do .
Code warehouse :
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
int a[maxn] , l[maxn] , r[maxn] , b[maxn];
int n , m;
template <typename T>
void read(T & x){
x = 0;T f = 1;char ch = getchar();while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();}while (isdigit(ch)){
x = x * 10 + (ch ^ 48);ch = getchar();}x *= f;}
template <typename T>
void write(T x){
if(x < 0){
putchar('-');x = -x;}if (x > 9)write(x / 10);putchar(x % 10 + '0');}
void add (int l , int r , int s , int d){
if (l > r) return ;
int t = s + (r - l) * d;
b[l] += s;
b[l+1] += d - s;
b[r+1] -= d + t;
b[r+2] += t;
}
void solve (){
for (int i = 1 ; i <= n ; i++) b[i] = 0;
for (int i = 1 ; i <= n ; i++){
int g = a[i] - m;
if (g <= 0) continue;
int v = i - l[i] + 1;
if (v >= g){
add(i , min(i + g - 1 , r[i]) , g , -1);
}else{
int gap = g - v;
int e1 = min(i + gap , r[i]);
add(i , e1 , v , 0);
add(e1 + 1 , min(r[i] , e1 + v - 1) , v - 1 , -1);
}
}
for (int i = 1 ; i <= n ; i++) b[i] += b[i - 1];
for (int i = 1 ; i <= n ; i++) b[i] += b[i - 1];
}
int main()
{
while (~scanf("%d%d" , &n, &m)){
for (int i = 1 ; i <= n ; i++) read(a[i]);
stack<int> s;
for (int i = 1 ; i <= n ; i++){
while (s.size() && a[s.top()] <= a[i]) s.pop();
if (s.size()) l[i] = s.top() + 1;
else l[i] = 1;
s.push(i);
}
while (s.size()) s.pop();
for (int i = n ; i >= 1 ; i--){
while (s.size() && a[i] > a[s.top()]) s.pop();
if (s.size()) r[i] = s.top() - 1;
else r[i] = n;
s.push(i);
}
solve();
for (int i = 1 ; i <= n ; i++) printf("%d " , b[i]);
printf("\n");
}
return 0;
}
边栏推荐
猜你喜欢

Understanding the difference between wait() and sleep()

Application of C language array in Sanzi chess -- prototype of Queen n problem

p4552-差分

为什么PrepareStatement性能更好更安全?

Phased summary of the research and development of the "library management system -" borrowing and returning "module

MATLAB读取显示图像时数据格式转换原因

Spark memory management mechanism new version

解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题

ML - Speech - advanced speech model

BPSK调制系统MATLAB仿真实现(1)
随机推荐
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
Qtime定义(手工废物利用简单好看)
2021上海市赛-B-排序后dp
MySQL transactions and mvcc
ZOJ - 4114 Flipping Game-dp,合理状态表示
How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?
The difference between Apple buy in and apple pay
C # fine sorting knowledge points 10 generic (recommended Collection)
Once spark reported an error: failed to allocate a page (67108864 bytes), try again
Spark memory management mechanism new version
Get the ask code corresponding to the key pressed by the keyboard
Pytorch学习笔记--常用函数总结3
苹果内购和Apple Pay 的区别
Local cache --ehcache
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
2021上海市赛-D-卡特兰数变种,dp
CF888G-巧妙字典树+暴力分治(异或最小生成树)
Phased summary of the research and development of the "library management system -" borrowing and returning "module
数据系统分区设计 - 分区与二级索引
MySQL优化总结二