codeforces1450D. Rating Compression
https://codeforces.com/contest/1450/problem/D
D題做晚了啊草,C想了一年還是不會啊
我們求出每個a[i]他作為最小值的範圍l[i],r[i],我們令一段區間中最小值出現的為這段區間最小值出現的最左邊或最右邊,那麼用單調棧左閉右開或者左開右閉就可以求得了
len[i]=r[i]-l[i]+1,也就是說a[i]作為最小值出現時,長度最大是len[i],再大a[i]就被別人給消除了
我們從大到小列舉ans[i]
如果n-i+1這個數字沒有,說明後面也一定都沒這個數字,直接break
如果有len[i]=i的,就給res[i]++,然後維護一個權值為下標的樹狀陣列,如果res[i]=1,那麼就是1,否則為0
在樹狀陣列上二分求字首全是1的最右邊在x,如果x>=n-i+1,說明1到 n-i+1都恰好出現一次,ans[i]=1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxl=3e5+10;
int n,m,k,cnt,tot,cas,mex;
int a[maxl],ans[maxl],l[maxl],r[maxl],s[maxl];
int b[maxl],res[maxl];
bool vis[maxl];
vector<int> in[maxl];
inline void prework()
{
scanf("%d",&n);
for(int i=1;i<=n+1;i++)
{
vis[i]=false,ans[i]=0;
in[i].clear();
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]),vis[a[i]]=true;
}
int top=0;s[0]=n+1;
for(int i=n;i>=1;i--)
{
while(top>0 && a[i]<=a[s[top]])
top--;
r[i]=s[top]-1;
s[++top]=i;
}
top=0;s[0]=0;
for(int i=1;i<=n;i++)
{
while(top>0 && a[i]<a[s[top]])
top--;
l[i]=s[top]+1;
in[r[i]-l[i]+1].push_back(a[i]);
s[++top]=i;
}
}
inline void add(int i,int x)
{
while(i<=n+1)
{
b[i]+=x;
i+=i&-i;
}
}
inline int find()
{
int sum=0,num=0;
for(int i=20;i>=0;i--)
if(num+(1<<i)<=n+1 && sum+b[num+(1<<i)]==num+(1<<i))
num+=1<<i,sum+=b[num];
return num;
}
inline void mainwork()
{
for(int i=0;i<=n+1;i++) b[i]=0,res[i]=0;
for(int i=n;i>=1;i--)
{
if(!vis[n-i+1])
break;
for(int d:in[i])
{
++res[d];
if(res[d]==1)
add(d,1);
if(res[d]==2)
add(d,-1);
}
int x=find();
if(x>=(n-i+1))
ans[i]=1;
}
}
inline void print()
{
for(int i=1;i<=n;i++)
printf("%d",ans[i]);
puts("");
}
int main()
{
int t=1;
scanf("%d",&t);
for(cas=1;cas<=t;cas++)
{
prework();
mainwork();
print();
}
return 0;
}
相關文章
- D. Rating System
- Oracle Database Compression 1 - Basic CompressionOracleDatabase
- Unused Block Compression和Null Block CompressionBloCNull
- Oracle Database Compression 3 - Hybrid Columnar CompressionOracleDatabase
- Oracle Database Compression 2 - Advanced/OLTP CompressionOracleDatabase
- Advanced Index CompressionIndex
- mysql 5.7 Transparent PageIO CompressionMySql
- LZ4 compression algorithm on FPGAGoFPGA
- MySQL5.7 InnoDB Page CompressionMySql
- What’s new in Cassandra 1.0: Compression
- 題單5:基礎練習(rating1200)
- Oracle Index Key Compression索引壓縮OracleIndex索引
- Availability and Compression of Free Space in a Data BlockAIBloC
- MODEL COMPRESSION VIA DISTILLATION AND QUANTIZATION翻譯
- Oracle 11g Advanced Compression(上)Oracle
- Oracle 11g Advanced Compression(下)Oracle
- Enhanced Invertible Encoding for Learned Image CompressionEncoding
- zt:Different type of RMAN backup compression in 11G
- Oracle Hybrid Columnar Compression(HCC) 混合列壓縮Oracle
- linux CentOS出現Error: xz compression not availableLinuxCentOSErrorAI
- Oracle 表壓縮(Table Compression)技術介紹Oracle
- Oracle OCP 1Z0-053(Rman Compression Algorithm)OracleGo
- iOS AutoLayout進階(三)Content Compression Resistance PriorityiOS
- 開啟 Gzip後打包 報錯compression-webpack-pluginWebPlugin
- 並查集系列之「路徑壓縮( path compression ) 」並查集路徑壓縮
- [LeetCode] 3163. String Compression IIILeetCode
- 該過程統計 RATING.ALL_PARAM_TABLE?表時包含的所有表的資料量
- Oracle OCP 1Z0 053 Q688(RMAN Backup Compression)Oracle
- Oracle OCP 1Z0 053 Q105(Table Compression)Oracle
- iOS autolayout的Content Hugging 和 Content Compression ResistanceiOS
- Oracle10g備份集壓縮新特性(Backupset Compression)Oracle
- 10G、11G expdp的compression引數的區別
- [20181127]12c Advanced Index Compression.txtIndex
- 實施vertex compression所遇到的各種問題和解決辦法
- MySQL 8.0 Reference Manual(讀書筆記78節-- InnoDB Table and Page Compression (1))MySql筆記
- MySQL 8.0 Reference Manual(讀書筆記79節-- InnoDB Table and Page Compression (2))MySql筆記
- [20181127]12c Advanced Index Compression 2.txtIndex
- Nginx R31 doc-11-Compression and Decompression 壓縮與解壓縮Nginx