QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#320366 | #8213. Graffiti | ucup-team052# | WA | 2ms | 3716kb | C++23 | 3.9kb | 2024-02-03 16:07:47 | 2024-02-03 16:07:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
if(nega==-1) return -ans;
return ans;
}
void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);}
#define N 300005
char _str[5];
int str[3],m;
vector<int> G[N];
int n;
ll f[N][3][3],g[N][3],w12[N],w[N];
int a[N],b[N],dpos[N],reva[N];
vector<int> d[N];
ll solve(int n)
{
for(int i=0;i<=n;i++) w12[i]=0;
for(int i=0;i<n;i++)
{
int dpos=g[i][1]-g[i][2];
dpos=min(dpos,n+1);
dpos=max(dpos,0);
w[i]=g[i][0]-max(g[i][1],g[i][2]);
w12[dpos+1]++,w12[0]+=max(g[i][1],g[i][2]);
::dpos[i]=dpos;
d[dpos].push_back(i);
}
for(int i=1;i<=n;i++) w12[i]+=w12[i-1];
for(int i=1;i<=n;i++) a[i]=b[i]=i-1;
sort(a+1,a+n+1,[&](int x,int y){
return w[x]>w[y];
});
for(int i=1;i<=n;i++) reva[a[i]]=i;
sort(b+1,b+n+1,[&](int x,int y){
return w[x]+dpos[x]>w[y]+dpos[y];
});
ll ans=w12[0]; int posa=0,posb=0,inb=0;
auto gval=[&](int i,int u)
{
if(i<=dpos[u]) return w[u];
else return w[u]-(i-dpos[u]);
};
ll suma=0;
for(int i=1;i<=n;i++)
{
while(posb>=1&&posa<n&&gval(i,b[posb])<gval(i,a[posa]))
{
if(dpos[b[posb]]<i) assert(0);
else if(dpos[a[posa]]>i) posa++;
else
{
posb--,inb--,posa++;
suma++;
}
}
suma-=inb;
while(posb<n&&dpos[b[posb]]>i) posb++;
while(posa<n&&dpos[a[posb]]<i) posa++;
if(posb<n&&(posa==n||gval(i,b[posb+1])>gval(i,a[posa+1])))
{
inb++; posb++;
suma+=gval(i,b[posb]);
}
else
{
posa++;
suma+=gval(i,a[posa]);
}
ans=max(ans,w12[i]+suma);
for(int j:d[i]) if(reva[j]<=posa) inb++;
}
for(int i=0;i<=n+1;i++) d[i].clear();
return ans;
}
void dfs(int u,int fa)
{
for(int v:G[u]) if(v!=fa) dfs(v,u);
for(int c=0;c<m;c++) for(int d=0;d<m;d++)
{
if(u==1) d=m;
if(str[1]==c)
{
vector<ll> tw[m];
for(int v:G[u]) if(v!=fa) for(int j=0;j<m;j++) tw[j].push_back(f[v][j][c]);
for(int j=0;j<m;j++)
{
if(str[0]==j&&str[2]==d) for(int i=0;i<(int)tw[j].size();i++) tw[j][i]++;
if(str[0]==d&&str[2]==j) for(int i=0;i<(int)tw[j].size();i++) tw[j][i]++;
}
ll ans=0;
if(str[0]==str[2])
{
if(m==1)
{
ans=1LL*(int)tw[0].size()*((int)tw[0].size()-1);
for(int i=0;i<(int)tw[0].size();i++) ans+=tw[0][i];
}
else
{
priority_queue<ll> q;
ll cur=0;
for(int i=0;i<(int)tw[0].size();i++) q.push(tw[0][i]-tw[1][i]),cur+=tw[1][i];
ans=cur;
for(int i=1;i<=(int)tw[0].size();i++)
{
cur+=q.top(); q.pop();
ans=max(ans,cur+1LL*i*(i-1));
}
}
}
else
{
for(int j=0;j<3;j++)
{
for(int i=0;i<(int)tw[0].size();i++) g[i][j]=tw[str[j]][i];
}
ans=solve(tw[0].size());
}
f[u][c][min(d,m-1)]=ans;
}
else
{
ll w=0;
for(int v:G[u]) if(v!=fa)
{
ll mx=0;
for(int j=0;j<m;j++) mx=max(mx,f[v][j][c]);
w+=mx;
}
f[u][c][min(d,m-1)]=w;
}
}
}
signed main()
{
cin>>n; scanf("%s",_str);
for(int i=1;i<n;i++)
{
int u=read(),v=read();
G[u].pb(v),G[v].pb(u);
}
if(strlen(_str)==1) cout<<n<<endl,exit(0);
if(strlen(_str)==2)
{
ll ans=0;
for(int i=1;i<=n;i++) ans+=G[i].size();
if(_str[0]==_str[1]) cout<<ans<<endl;
else cout<<ans/2<<endl;
return 0;
}
for(int i=0;i<3;i++)
{
int ok=0;
for(int j=0;j<i;j++) if(_str[i]==_str[j]) str[i]=str[j],ok=1;
if(!ok) str[i]=m++;
}
dfs(1,0);
ll ans=0;
for(int c=0;c<m;c++) ans=max(ans,f[1][c][m-1]);
cout<<ans<<endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3624kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3716kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3624kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3624kb
input:
5 bob 3 2 5 1 1 4 2 4
output:
4
result:
ok 1 number(s): "4"
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 3712kb
input:
50 abc 23 14 24 25 1 3 47 46 2 26 22 41 34 19 7 14 50 24 29 38 17 25 4 26 35 37 21 14 11 4 13 27 8 25 5 10 20 27 44 27 15 39 19 9 30 12 38 27 39 27 41 40 14 48 32 7 16 37 3 13 42 5 48 27 49 25 6 5 26 9 31 17 36 7 43 29 9 5 45 9 18 9 40 42 27 5 25 42 46 10 37 42 12 48 28 26 33 5
output:
33
result:
wrong answer 1st numbers differ - expected: '37', found: '33'