CF1420E Battle Lemmings
CF1420E Battle Lemmings
大菜鸡参考了两位哥哥的博客才写出来的
//Proudly using c++11 by gwt
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
//#define endl '\n'
#define count2(x) __builtin_popcount(x)
#define countleadingzero(x) __builtin_clz(x)
#define debug(x) cerr << #x << " = " << x << endl;
inline ll read(){//not solve LLONG_MIN LMAX=9,223,372,036,854,775,807
ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
int dp[81][81][81*41];
// i\j\k i in pos j cost k
// show the minnimize numbers of part i "0..0"
int main(){
int n;
cin>>n;
vector<int>nums(n+1);
int tot1=0;
vector<int>pos1;
for(int i=1;i<=n;++i){
cin>>nums[i];
if(nums[i]==1)pos1.pb(i);
}
int sum=0;
tot1=pos1.size();
sum+=n*(n-1)/2;//every pair of n;
sum-=tot1*(tot1-1)/2;//every pair of 11
sum-=tot1*(n-tot1);//every pair of 10/01
int cost=0;
memset(dp,0x3f,sizeof(dp));
dp[0][0][0]=0;
for(int i=0;i<pos1.size();++i){
if(i==0)
cost+=max(0,(pos1[i]-1)*(pos1[i]-2)/2);
else
cost+=max(0,(pos1[i]-pos1[i-1]-1)*(pos1[i]-pos1[i-1]-2)/2);
dp[i+1][pos1[i]][0]=cost;
//cout<<cost<<'\n';
}
int i=0;
const int MAXM=n*(n-1)/2;
for(auto posi:pos1){
i++;
for(int j=1;j<=n;++j){
for(int t=0;t<j;++t){
for(int k=0;k<=MAXM;++k){
int &PT=dp[i][j][abs(j-posi)+k];
PT=min(PT,dp[i-1][t][k]+max(0,(j-t-1)*(j-t-2)/2));
}
}
}
// cout<<cnt1<<' '<<tot1<<'\n';
}
int ans=1e9;
for(int k=0;k<=MAXM;++k){
for(int j=1;j<=n;++j){
//sum-pastito
// cout<<dp[tot1][j][k]<<" ";
ans=min(ans,dp[tot1][j][k]+max(0,(n-j)*(n-j-1)/2));
// cout<<ans<<'\n';
}
// cout<<endl;
if(tot1==0)
sum=ans;
cout<<sum-ans<<" \n"[k==MAXM];
}
// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
return 0;
}