Group
#include <bits/stdc++.h> #define M 3000 #define gibon cin.tie(0); ios::sync_with_stdio(false); using namespace std; int n; int dp[M+5]; vector <int> v[M+5]; int a[M+5],b[M+5]; int from[M+5]; int res[M+5]; int ri; int main(){ gibon //freopen("in.txt","r",stdin); cin>>n; for(int i=1;i<=n;i++){ int tmp; while(true){ cin>>tmp; if(tmp==0) break; if(tmp<i){ a[i]++; }else{ v[tmp].push_back(i); } } } for(int i=1;i<=n;i++){ for(int j=0;j<v[i].size();j++){ b[v[i][j]]++; } dp[i]=1e9; int sum=0; for(int j=i;j>0;j--){ sum+=a[j]-2*b[j]+i-j; if(dp[i]>dp[j-1]+sum){ dp[i]=dp[j-1]+sum; from[i]=j-1; } } } cout<<dp[n]<<endl; for(int i=n;i>0;i=from[i]){ res[ri++]=i-from[i]; } cout<<ri; while(ri--){ cout<<" "<<res[ri]; } cout<<endl; return 0; }
Comments
Post a Comment