codeforces Taming the Herd

版权声明:博客作者为blue bear,严禁他人在未经作者同意下进行商用转载 https://blog.csdn.net/weixin_44354699/article/details/88378389

题面

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 lo and behold, some entries of his log are missing!

Farmer John is confident that the he started his log on the day of a breakout. Please help him determine, out of all sequences of events consistent with the log entries that remain, the minimum and maximum number of breakouts that may have take place over the course of the logged time.

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 either −1, indicating that the log entry for day i is missing, or a non-negative integer ai (at most 100), indicating that on day i the counter was at ai.

Output
If there is no sequence of events consistent with Farmer John’s partial log and his knowledge that the cows definitely broke out of the barn on the morning of day 1, output a single integer −1. Otherwise, output two space-separated integers m followed by M, where m is the minimum number of breakouts of any consistent sequence of events, and M is the maximum.
input
4
-1 -1 -1 1

output
2 3

题意

从第一次发生破坏开始记录(所以第一天必然为0或者看不清的-1),之后每一天都记录距离上次发生破坏的时间,如果那天发生破坏则记为0.但是有些数据被搞了,看不清,就是-1.问你最少的破坏次数和破坏次数。如果数据有矛盾那么输出-1.
比如从样例我们可以尽可能地还原记录
0 -1 0 1
可以确定的是两个0是两次破坏而-1处可能为0也可能为1,所以最少为2,最多为3

分析

首先第一天如果不是0或-1,那么输出-1.
然后对每个非0和-1的数字往前倒推,如果出现4 6这种情况 输出-1
最后数0个数和-1的个数
0个数是最少天数 0和-1个数加起来是最大天数

代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
int a[105];
int main(){
	int n,i,j,least,more;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	if(a[1]>0){
		puts("-1");
		return 0;
	}
	a[1]=0;
	int flag=0;
	for(i=1;i<=n;i++){
		if(a[i]!=-1) {
			int zero=0;
			for(j=a[i];j>0;j--){
				if(a[i-j]==-1) a[i-j]=zero++;
				else if(a[i-j]!=zero++){
					flag=1;
					break;
				}
			}
			if(flag){
				puts("-1");
				return 0;
			}
		}
	}
	//for(i=1;i<=n;i++) printf("%d ",a[i]);
	//puts("");
	least=more=0;
	for(i=1;i<=n;i++){
		if(a[i]==0) least++;
		if(a[i]==-1) more++;
	}
	printf("%d %d\n",least,least+more);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44354699/article/details/88378389