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
- Problem A. Ascending Rating
- Unused Block Compression和Null Block CompressionBloCNull
- 『杭電1810』Rating of Tetris
- Encoder Data compression
- expdp+compression效能測試
- Enhanced Invertible Encoding for Learned Image CompressionEncoding
- LZ4 compression algorithm on FPGAGoFPGA
- [LeetCode] 3163. String Compression IIILeetCode
- 題單5:基礎練習(rating1200)
- MODEL COMPRESSION VIA DISTILLATION AND QUANTIZATION翻譯
- linux CentOS出現Error: xz compression not availableLinuxCentOSErrorAI
- [20181127]12c Advanced Index Compression.txtIndex
- iOS AutoLayout進階(三)Content Compression Resistance PriorityiOS
- Oracle Hybrid Columnar Compression(HCC) 混合列壓縮Oracle
- [20181127]12c Advanced Index Compression 2.txtIndex
- [20190311]12cR2 Advanced index compression.txtIndex
- 開啟 Gzip後打包 報錯compression-webpack-pluginWebPlugin
- 並查集系列之「路徑壓縮( path compression ) 」並查集路徑壓縮
- 閱讀文獻《DCRNet:Dilated Convolution based CSI Feedback Compression for Massive MIMO Systems》
- 文獻閱讀——Dynamic Range Compression Preserving Local Image Contrast for Digital Video CameraASTGitIDE
- 論文翻譯:2021_Towards model compression for deep learning based speech enhancement
- 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筆記
- Nginx R31 doc-11-Compression and Decompression 壓縮與解壓縮Nginx
- 專案效能優化之用compression-webpack-plugin外掛開啟gzip壓縮,以vue為例優化WebPluginVue