QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#715323 | #8212. Football in Osijek | ucup-team5071# | WA | 0ms | 3776kb | C++20 | 1.9kb | 2024-11-06 11:28:49 | 2024-11-06 11:28:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf=1e9;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<int> vis(n+1);
vector<int> a(n+1);
vector<int> cnt(n+1),num(n+1,1),flag(n+1,0);
for(int i=1;i<=n;i++){
cin>>a[i];
cnt[a[i]]++;
}
queue<int> qu;;
for(int i=1;i<=n;i++)if(cnt[i]==0){
qu.push(i);
}
while(!qu.empty()){
int x=qu.front();qu.pop();
num[a[x]]+=num[x];
vis[x]=1;
if(--cnt[a[x]]==0)qu.push(a[x]);
}
vector<int> all;
for(int i=1;i<=n;i++)if(!vis[i]){
int now=a[i],l=1,r=num[i];
vis[i]=1;
while(now!=i){
vis[now]=1;
l++;r+=num[now];
now=a[now];
}
all.push_back(r);
for(int j=l;j<=r;j++)flag[j]=1;
}
sort(all.begin(),all.end());
vector<int> dp(n+1,inf);
dp[0]=0;
for(int i=0;i<all.size();i++){
int j=i;
while(j+1<all.size()&&all[j+1]==all[i])j++;
int cnt=j-i+1;
// cout<<"x="<<all[i]<<" cnt="<<cnt<<endl;
for(int now=0,t=1;now<=cnt;now+=t,t*=2){
int q=min(cnt-now,t);
int g=q*all[i];
deque<pair<int,int>> qu;
for(int j=0;j<=n;j++){
while(!qu.empty()&&qu.back().first>dp[j])qu.pop_back();
qu.push_back({dp[j],j});
while(qu.front().second<j-g)qu.pop_front();
dp[j]=min(dp[j],qu.front().first+q);
}
// cout<<"now="<<now<<" q="<<q<<" g="<<g<<endl;
// for(int j=1;j<=n;j++)cout<<dp[j]<<" \n"[j==n];
}
i=j;
}
auto ans=dp;
for(int i=1;i<=n;i++)ans[i]--;
for(int i=1;i<=n;i++)if(!flag[i])ans[i]=max(ans[i],1);
for(int i=1;i<=n;i++)cout<<ans[i]<<" \n"[i==n];
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3616kb
input:
5 2 3 1 3 5
output:
0 1 0 0 1
result:
ok 5 number(s): "0 1 0 0 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3612kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 0 0 0 0 0
result:
wrong answer 8th numbers differ - expected: '1', found: '0'