Taming the Herd(2)dp

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Early in the morning, Farmer John woke up to the sound of splintering wood. It was the cows, and they were breaking out of the barn again!

Farmer John was sick and tired of the cows’ morning breakouts, and he decided enough was enough: it was time to get tough. He nailed to the barn wall a counter tracking the number of days since the last breakout. So if a breakout occurred in the morning, the counter would be 0 that day; if the most recent breakout was 3 days ago, the counter would read 3. Farmer John meticulously logged the counter every day.

The end of the year has come, and Farmer John is ready to do some accounting. The cows will pay, he says! But something about his log doesn’t look quite right…

Farmer John wants to find out how many breakouts have occurred since he started his log. However, he suspects that the cows have tampered with his log, and all he knows for sure is that he started his log on the day of a breakout. Please help him determine, for each number of breakouts that might have occurred since he started the log, the minimum number of log entries that must have been tampered with.

Input
The first line contains a single integer N (1≤N≤100), denoting the number of days since Farmer John started logging the cow breakout counter.

The second line contains N space-separated integers. The ith integer is a non-negative integer ai (at most 100), indicating that on day i the counter was at ai, unless the cows tampered with that day’s log entry.

Output
The output should consist of N integers, one per line. The ith integer should be the minimum over all possible breakout sequences with i breakouts, of the number of log entries that are inconsistent with that sequence.

Example
inputCopy
6
1 1 2 0 0 1
outputCopy
4
2
1
2
3
4
Note
If there was only 1 breakout, then the correct log would look like 0 1 2 3 4 5, which is 4 entries different from the given log.

If there were 2 breakouts, then the correct log might look like 0 1 2 3 0 1, which is 2 entries different from the given log. In this case, the breakouts occurred on the first and fifth days.

If there were 3 breakouts, then the correct log might look like 0 1 2 0 0 1, which is just 1 entry different from the given log. In this case, the breakouts occurred on the first, fourth, and fifth days.

And so on.
思路:dp

		if(!k) dp[i][j][k]=g[i-1][j-1]+(a[i]!=0);
		else dp[i][j][k]=dp[i-1][j][k-1]+(a[i]!=k);
		g[i][j]=min(g[i][j],dp[i][j][k]);
#include <bits/stdc++.h>
using namespace std;
const int M=110,inf=0x3f3f3f3f;
int n,a[M],dp[M][M][M],g[M][M];
int main(){
	memset(dp,inf,sizeof(dp));
	memset(g,inf,sizeof(g));
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	dp[0][0][0]=g[0][0]=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=i;j++)
			for(int k=0;k<i;k++){
				if(!k) dp[i][j][k]=g[i-1][j-1]+(a[i]!=0);
				else dp[i][j][k]=dp[i-1][j][k-1]+(a[i]!=k);
				g[i][j]=min(g[i][j],dp[i][j][k]);
			}
	for(int i=1;i<=n;i++)
		cout<<g[n][i]<<'\n';
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值