gym101480G Greenhouse Growth
题意:有一群卷王坐成一排,第 $i$ 个人卷了 $a_i$ 题,每次所有人会往同一个方向看(往前或者往后),如果最近的人题数比自己多,就多卷一个题(如果这个人和你一样,但他卷了一题,那你也要卷一题),求 $m$ 次后 每个卷王的题数。
有$n$个数$a_1\dots a_n$,进行 $m$ 次操作,分两种:
for(int i=2;i<=n;++i)if(a[i-1]>a[i])++a[i];//1
for(int i=n-1;i;--i)if(a[i+1]>a[i])++a[i];//2
求操作结束后$a_1\dots a_n$的值。
$n,m\leq 3\times 10^5,6s$
题解
如果相邻两个卷王题数一样,显然他们会同时卷题,可以把他们合并。
注意到相邻两个卷王合并前大小关系一定不变,那么我们可以记一下每个卷王向前的时候会不会卷题 向后的时候会不会卷题,然后题数可以延迟更新。
显然相邻两个不同题数的卷王 合并只可能需要 永远不可能/几次向前 不考虑向后/几次向后 不考虑向前/几次任意操作。
用链表维护当前的所有卷王,然后动态记一下相邻两个卷王什么时候会合并就行了(在合并后 和前面/和后面 什么时候会合并 这个时间可能会修改)。
时间复杂度$O(n+m)$。
/*
Author: QAQAutoMaton
Lang: C++
Code: G.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cp;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;}
template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
template<class T>bool sort2(T &a,T &b){return a>b?swap(a,b),1:0;}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
__INIT__(){
fill((unsigned char*)&inf,(unsigned char*)&inf+sizeof(inf),0x3f);
}
}__INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
template<typename A>
inline bool read (A &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
while((x=gc())==' '||x=='\n' || x=='\r');
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r');
if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
template<typename A>
inline bool write (A x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int n;
int a[300005],xl[300005],xr[300005],las[300005];
char s[300005];
int s0[300005],s1[300005];
int A[300005],B[300005];
int at[300005],lu[300005],ru[300005];
int ct;
int fa[300005];
int ned[300005];
int req[300005];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void push_up(int x){
// write("===",x,' ',at[x],' ',a[x],' ',lu[x],' ',ru[x],'\n');
if(lu[x])a[x]+=s0[ct]-s0[at[x]];
if(ru[x])a[x]+=s1[ct]-s1[at[x]];
at[x]=ct;
}
vector<int> nd;
vector<int> wd[300005];
int AT[300005],C;
void merge(int x){
x=find(x);
push_up(x);
push_up(xr[x]);
ru[x]=ru[xr[x]];
ned[x]=ned[xr[x]];
fa[xr[x]]=x;
xl[xr[x]=xr[xr[x]]]=x;
nd.emplace_back(x);
}
int m;
int chk(int x){
int y=xr[x];
push_up(x);push_up(y);
if(a[x]<a[y]){
if(!lu[x] && ru[y])return 0;
if(lu[x] && !ru[y])return min(ct+a[y]-a[x],m+1);
if(!lu[x] && !ru[y])return B[min(s1[ct]+a[y]-a[x],s1[m]+1)];
return A[min(s0[ct]+a[y]-a[x],s0[m]+1)];
}
else{
if(lu[x] && !ru[y])return 0;
if(!lu[x] && ru[y])return min(ct+a[x]-a[y],m+1);
if(!lu[x] && !ru[y])return A[min(s0[ct]+a[x]-a[y],s0[m]+1)];
return B[min(s1[ct]+a[x]-a[y],s1[m]+1)];
}
}
void rb(int x){
if(fa[x]!=x)return;
if(AT[x]==C)return;
AT[x]=C;
if(xr[x] && AT[xr[x]]!=C){
int s=chk(x);
if(s!=ned[x]){
ned[x]=s;wd[s].emplace_back(x);
}
}
if(xl[x] && AT[xl[x]]!=C){
int y=xl[x];
int s=chk(y);
if(s!=ned[y]){
ned[y]=s;wd[s].emplace_back(y);
}
}
}
void rc(int);
void rebuild(){
++C;
for(auto i:nd){rb(i);}
nd.clear();
}
signed main(){
#ifdef QAQAutoMaton
freopen("G.in","r",stdin);
freopen("G.out","w",stdout);
#endif
read(n,m);
for(int i=1;i<=n;++i){read(a[i]);}
read(s+1);
for(int i=1;i<=m;++i){
s0[i]=s0[i-1];s1[i]=s1[i-1];
if(s[i]=='A')
A[++s0[i]]=i;
else
B[++s1[i]]=i;
}
for(int i=1;i<=n;++i){
fa[i]=i;
xl[i]=i-1;xr[i]=(i==n?0:i+1);
}
for(int i=1;i<n;++i)if(a[i]==a[i+1])merge(i);
nd.clear();
++C;
for(int i=1;i<=n;++i)if(fa[i]==i){
lu[i]=a[i]<a[xl[i]];
ru[i]=a[i]<a[xr[i]];
}
for(int i=1;i<=n;++i)if(fa[i]==i){
rb(i);
}
for(ct=1;ct<=m;++ct){
for(auto i:wd[ct]){
if(ned[find(i)]==ct){
merge(i);
}
}
rebuild();
/* for(int i=1;i;i=xr[i])push_up(i);
for(int i=1;i<=n;++i)write(a[find(i)],i==n?'\n':' ');*/
}
ct=m;
for(int i=1;i;i=xr[i])push_up(i);
for(int i=1;i<=n;++i)write(a[find(i)],i==n?'\n':' ');
return 0;
}
lk是卷王
不对,我才是