Taming the Herd [洛谷] (贪心) /*rank*/

本文介绍了一道编程题目,农夫John记录奶牛逃跑的天数,但部分记录缺失。任务是根据残留记录推断最少和最多可能的逃跑次数。通过贪心策略,修复记录并计算最小和最大值。
摘要由CSDN通过智能技术生成

原题传送门


题面:

Taming the Herd

                            time limit per test : 1 second
                         memory limit per test : 256 megabytes
                                 input : standard input
                                output : standard output

Problem Description

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.

Sample Input

4
-1 -1 -1 1

Sample Output

2 3

题意描述

John的记录牛牛们破坏大门的日子的记录条目被牛牛们毁坏了,丢失的部分为-1,还有一些地方则是被修改了(做题的时候被坑死了 ),所以要修复记录,但被修改过的地方是没法修复了,需要输出-1,若能修复成功则输出John记录牛牛们破坏大门的天数的最大可能值和最小可能值.

题目分析

直接对输入的记录进行往前修复操作,遇到第一个非0和非-1的数时往前修复,若修复途中出现不正常的地方,则跳出输出-1.需要注意在第一天的时候,牛牛们必定破坏了大门,所以第一天不为0必定错误.

在修复成功之后,由贪心思想可以知道,最大可能值是修复后记录中0和-1的个数,而最小可能值是修复后记录中的0的个数(能摸鱼就摸鱼的John )

具体代码

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int day[maxn];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &day[i]);
    }
    if(day[1] > 0)
    {
        printf("-1\n");
        return 0;
    }
    day[1] = 0;
    for(int i = 1; i <= n; i++)
    {
        if(day[i] != -1)
        {
            int temp = day[i]-1;
            int num = i-1;
            while(temp >= 0)
            {
                if(day[num] == -1)
                {
                    day[num] = temp;
                }
                else if(day[num] != temp)
                {
                    printf("-1\n");
                    return 0;
                }
                num--;
                temp--;
            }
        }
    }
    int MaxAns = 0, MinAns = 0;
    for(int i = 1; i <= n; i++)
    {
        if(day[i] == 0 || day[i] == -1)
        {
            MaxAns++;
        }
        if(day[i] == 0)
        {
            MinAns++;
        }
    }
    printf("%d %d\n", MinAns, MaxAns);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值