有没有大佬看下这个Dhack哪里了QAQ

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
LL a[maxn];
LL sum0[maxn],sum1[maxn];
LL ones=0,zeros=0;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++){
  	cin>>a[i];
  	
	sum0[i]=sum0[i-1];sum1[i]=sum1[i-1];
	
	if(a[i]) sum1[i]++,ones++;
	
	else sum0[i]++,zeros++; 
  }
  LL p=0x3f3f3f3f;
  p=min(p,ones);
  p=min(p,zeros+1);
  for(LL i=1;i<=n;i++){
  	 //到i这个位置的对前面0的操作,对后面的数进行单点
	 p=min(p,sum0[i]+1+ones-sum1[i]);   
  	 
  }
  LL k=0;
  for(LL i=1;i<=n;i++){
  	 if(a[i]!=a[i+1]&&i+1!=n+1) k++; 
  }
  if(a[n]==1) k++;
   
  cout<<max(min(p,k),(LL)0)<<endl;
return 0;
}


全部评论
D贪心其实能过 qwq
1 回复 分享
发布于 2020-08-16 13:10
hack数据: 20 1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   0   1 你的代码输出5,正解是4 这题其实是要dp,dp[i][0]表示到第i个数前面全为0的最少翻转次数,dp[i][1]表示到第i个数前面全为1的最小次数,转移方程: dp[i][0]=min(dp[i-1][0]+a[i],dp[i-1][1]+1); dp[i][1]=min(dp[i-1][0]+1,dp[i-1][1]+1-a[i]);
点赞 回复 分享
发布于 2020-08-14 22:16
给个反例 12 0 1 0 1 0 1 0 0 0 1 1 1 答案应该是5吧
点赞 回复 分享
发布于 2020-08-14 22:11

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务