# 概况

Rating:$\color{green}{+185}$($\color{orange}{2159}$)

(上$\color{orange}{橙}$啦!)

# 题目

## A. Stickers and Toys

AC时间:3min

$n-t$个蛋里没有玩具,$n-s$个蛋里没有贴纸。

code:

#include<bits/stdc++.h>
using namespace std;
int T,a,b,c;
int main(){
scanf("%d",&T);
while(T--)scanf("%d%d%d",&a,&b,&c),printf("%d\n",max(a-b+1,a-c+1));
return 0;
}


## B.Letters Shop

AC时间:9min

code:

#include<bits/stdc++.h>
using namespace std;
int n,m,len,lt[26][200010],tt[26],ans;
char c[200010],na[200010];
int main(){
scanf("%d%s",&n,c+1);
for(int i=1;i<=n;++i)lt[c[i]-'a'][++tt[c[i]-'a']]=i;
scanf("%d",&m);
while(m--){
scanf("%s",na+1),len=strlen(na+1),ans=0,memset(tt,0,sizeof(tt));
for(int i=1;i<=len;++i)++tt[na[i]-'a'];
for(int i=0;i<26;++i)ans=max(ans,lt[i][tt[i]]);
printf("%d\n",ans);
}
return 0;
}


## C.Vasya And Array

AC时间:25min

code:

#include<bits/stdc++.h>
using namespace std;
struct Q{
int l,r;
}s[1010],ns[1010];
int n,m,t,ss,sn,nws=1,nwn=1,ans[1010],st,v;
bool cmp(Q x,Q y){
return x.l<y.l;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)scanf("%d",&t),t?++ss,scanf("%d%d",&s[ss].l,&s[ss].r),++s[ss].l:(++sn,scanf("%d%d",&ns[sn].l,&ns[sn].r));
sort(s+1,s+ss+1,cmp);
ans[0]=n+1;
for(int i=1;i<=n;++i){
while(nws<=ss&&s[nws].l<=i)st=max(st,s[nws++].r);
if(st>=i)ans[i]=ans[i-1];
else ans[i]=ans[i-1]-1;
}
for(int i=1;i<=sn;++i){
v=ans[ns[i].l];
for(int j=ns[i].l;j<=ns[i].r;++j)if(ans[j]!=v)goto PASS;
return puts("NO"),0;
PASS:
;
}
puts("YES");
for(int i=1;i<=n;++i)printf("%d ",ans[i]);
return 0;
}


## D.Subarray Sorting

AC时间:51min

code:

#include<bits/stdc++.h>
#define ci const int&
#define Upd(x) (t[x].mn=min(t[x<<1].mn,t[x<<1|1].mn))
using namespace std;
const int INF=1e9;
struct node{
int l,r,mn;
}t[1200010];
int q,n,a[300010],g[300010],ap[300010],ue[300010];
vector<int>p[300010];
void Build(ci x,ci l,ci r){
t[x].l=l,t[x].r=r;
if(l==r)return(void)(t[x].mn=a[l]);
int mid=l+r>>1;
Build(x<<1,l,mid),Build(x<<1|1,mid+1,r),Upd(x);
}
void Change(ci x,ci id,ci v){
if(t[x].l==t[x].r)return(void)(t[x].mn=v);
int mid=t[x].l+t[x].r>>1;
id<=mid?Change(x<<1,id,v):Change(x<<1|1,id,v),Upd(x);
}
int Query(ci x,ci l,ci r){
if(l>r)return INF;
if(t[x].l==l&&t[x].r==r)return t[x].mn;
int mid=t[x].l+t[x].r>>1;
return l>mid?Query(x<<1|1,l,r):(r<=mid?Query(x<<1,l,r):min(Query(x<<1,l,mid),Query(x<<1|1,mid+1,r)));
}
int main(){
scanf("%d",&q);
for(int fr=1;fr<=q;++fr){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",&a[i]),ap[a[i]]<fr?p[a[i]].clear(),ue[a[i]]=0,ap[a[i]]=fr:0,p[a[i]].push_back(i);
Build(1,1,n);
for(int i=1;i<=n;++i)scanf("%d",&g[i]);
for(int i=1;i<=n;++i){
if(ap[g[i]]<fr||ue[g[i]]>=p[g[i]].size()){
puts("NO");
goto END;
}
if(Query(1,1,p[g[i]][ue[g[i]]]-1)<g[i]){
puts("NO");
goto END;
}
Change(1,p[g[i]][ue[g[i]]],INF),++ue[g[i]];
}
puts("YES");
END:
;
}
return 0;
}


## E.Tree Painting

AC时间:1h30min

$sz_x$表示节点$x$在1为根时的子树大小,$fsz_x=n-sz_x$

code:

#include<bits/stdc++.h>
#define ci const int&
using namespace std;
struct edge{
int t,nxt;
}e[400010];
int n,u,v,cnt,be[200010],sz[200010],fsz[200010],vis[200010];
long long ans;
void add(ci x,ci y){
e[++cnt].t=y,e[cnt].nxt=be[x],be[x]=cnt;
}
void Get(ci x){
vis[x]=sz[x]=1;
for(int i=be[x];i;i=e[i].nxt)!vis[e[i].t]?Get(e[i].t),sz[x]+=sz[e[i].t]:0;
fsz[x]=n-sz[x],ans+=sz[x];
}
void dfs(ci x,long long nw){
ans=max(ans,nw),vis[x]=0;
for(int i=be[x];i;i=e[i].nxt)vis[e[i].t]?dfs(e[i].t,nw-sz[e[i].t]+(fsz[x]+sz[x]-sz[e[i].t])),0:0;
}
int main(){
scanf("%d",&n);
Get(1),dfs(1,ans);
printf("%I64d",ans);
return 0;
}


# 总结

posted @ 2019-09-09 19:37  xryjr233  阅读(54)  评论(0编辑  收藏