题面:

A. Taming the Herd

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
 
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.
 
Example
Input
4
-1 -1 -1 1
Output
2 3
 
Note
In this example, we can deduce that a breakout had to occur on day 3. Knowing that a breakout also occurred on day 1, the only remaining bit of uncertainty is whether a breakout occurred on day 2. Hence, there were between 2 and 3 breakouts in total.
 

题目描述:

奶牛搞破坏,把奶牛棚弄坏了。从奶牛棚被破坏的那一天起,农夫就开始写日志,并且每一天都会写。这个日志记录的是:离上次奶牛棚被破坏的天数。如果当天奶牛棚被损坏,就记录为0;如果前3天奶牛棚被损坏,就记录为3。年末,农夫拿这个日志找奶牛“算账”的时候,发现这个日志被损坏了(有些部分被丢失了)。现在要帮助农夫判断:这个日志是否“合法”。如果“合法”,就输出最小可能和最大可能奶牛棚被破坏的天数;如果不“合法”,就输出-1。(当天日志被丢失的部分用-1表示)
 

题目分析:

这道题是一道水题,但是由于看错题目(英语渣的我),当场没有做出来。这道题大概是这样的:
1.第一天要么就是0,要么就是-1(被丢失)。如果不是0和-1,直接可以判断不合法。
2.我们可以根据日志上的其他剩余信息推算出其他天的日志信息,最简单的:第一天无论是0还是-1,一定是可以推算出是0。其他:比如:
这里第6天记录了一个“2”,代表前2天奶牛棚被坏了,所以第4天一定是0,第5天一定是1:
如果第4天不是0,或者第5天不是1,那么一定不合法:
我们再想一想:第7,8,9天我们能推出来吗?根据现有条件第7,8,9天的日志内容是不一定的:假如第6天后奶牛棚没有被破坏,那么后面的所有内容就可以被推出来,否则也不一定。所以现在就先要编写一个把能推的都推出来的代码,看究竟是否合法。
 
在写这个代码的时候,一般我们会从1遍历倒N,可是这里反过来遍历(N-1)代码会更容易些,为什么?假设第6天记录的是3,那么第5天是不是记录的是3-1,也就是2:
然后就一直进行减1的操作,直到是0为止:
这样想的结果是不是和刚刚按从1-N的顺序想的结果一样,但是代码只需要从(N-1)遍历一次就行了。
 
推算完全部结果后(这时肯定是合法的,如果中间检查到不合法就直接输出-1然后结束程序),奶牛棚被破坏的天数最少的天数就是推算出来后,日志内容是0的总天数。
(注:这里最少天数为2)
最多的天数怎样计算?只要把不能推算出来的日志内容全部变为0:
然后统计0的个数(不能推算出来日志内容的天数 + 最少天数)就是最大天数的(这里最大天数为6)。
 
 
AC代码:
 1 #include <cstdio>
2 #include <iostream>
3 using namespace std;
4 int n, a[105];
5
6 int main(){
7 cin >> n;
8 for(int i = 1; i <= n; i++){
9 cin >> a[i];
10 }
11
12 if(a[1] != 0 && a[1] != -1){
13 cout << -1 << endl; //最简单的不合法情况
14 return 0;
15 }
16
17 a[1] = 0; //这个不要漏
18 for(int i = n; i >= 1;){
19 while(a[i] == -1 && i >= 1) i--; //写这种代码时一定要记得 "i >= 1" 这样的限制条件
20 if(i == 0) break; //遍历完
21
22 int t = a[i];
23 while(t >= 0 && i >= 1){
24 if(a[i] != t && a[i] != -1){ //推算出的不合法
25 cout << -1 << endl;
26 return 0;
27 }
28 a[i--] = t--;
29 }
30 }
31
32 int minn, cnt;
33 minn = cnt = 0;
34 for(int i = 1; i <= n; i++){ //简单的计数
35 if(a[i] == 0) minn++;
36 if(a[i] == -1) cnt++;
37 }
38
39 cout << minn << " " << minn+cnt << endl;
40 return 0;
41 }
 
 
 

2019 GDUT Rating Contest II : A. Taming the Herd的更多相关文章

  1. 2019 GDUT Rating Contest II : Problem F. Teleportation

    题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...

  2. 2019 GDUT Rating Contest II : Problem G. Snow Boots

    题面: G. Snow Boots Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  3. 2019 GDUT Rating Contest II : Problem C. Rest Stops

    题面: C. Rest Stops Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  4. 2019 GDUT Rating Contest II : Problem B. Hoofball

    题面: 传送门 B. Hoofball Input file: standard input Output file: standard output Time limit: 5 second Memor ...

  5. 2019 GDUT Rating Contest III : Problem D. Lemonade Line

    题面: D. Lemonade Line Input file: standard input Output file: standard output Time limit: 1 second Memo ...

  6. 2019 GDUT Rating Contest I : Problem H. Mixing Milk

    题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  7. 2019 GDUT Rating Contest I : Problem A. The Bucket List

    题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...

  8. 2019 GDUT Rating Contest I : Problem G. Back and Forth

    题面: G. Back and Forth Input file: standard input Output file: standard output Time limit: 1 second Mem ...

  9. 2019 GDUT Rating Contest III : Problem E. Family Tree

    题面: E. Family Tree Input file: standard input Output file: standard output Time limit: 1 second Memory ...

随机推荐

  1. HDU 4675 GCD of Sequence(莫比乌斯反演 + 打表注意事项)题解

    题意: 给出\(M\)和\(a数组\),询问每一个\(d\in[1,M]\),有多少组数组满足:正好修改\(k\)个\(a\)数组里的数使得和原来不同,并且要\(\leq M\),并且\(gcd(a_ ...

  2. 利用设置新数据存储结构解决vue中折叠面板双向绑定index引起的问题

    问题背景是,在进行机器性能可视化的前端开发时,使用折叠面板将不同机器的性能图表画到不同的折叠面板上去.而机器的选择利用select下拉选项来筛选. 由于在折叠面板中,通过 如下v-model双向绑定了 ...

  3. filter_var 函数()绕过执行命令

    escape 过滤器来过滤link,而实际上这里的 escape 过滤器,是用PHP内置函数 htmlspecialchars 来实现的 htmlspecialchars 函数定义如下: htmlsp ...

  4. 全方位构造免杀 webshell 小结[一]

    转载自https://klionsec.github.io/2017/10/11/bypasswaf-for-webshell/   全方位构造免杀 webshell 小结[一]   前言:    本 ...

  5. 80行Python代码搞定全国区划代码

    微信搜索:码农StayUp 主页地址:https://gozhuyinglong.github.io 源码分享:https://github.com/gozhuyinglong/blog-demos ...

  6. 013.NET5_MVC_Razor扩展Html控件01

    Razor扩展控件  第一种方式: 1. 定义一个静态类 2. 定义静态扩展方法,扩展IHtmlHelper类型,返回IHtmlContent类型: 本质:通过后台方法,返回一个已经存在的Html标签 ...

  7. .pyc & Python

    .pyc & Python Python bytecode / 字节码 Python compiles the .py files and saves it as .pyc files , s ...

  8. How to implement an accurate countdown timer with js

    How to implement an accurate countdown timer with js 如何用 js 实现一个精确的倒计时器 原理剖析 web worker js custom ti ...

  9. lightning & web components & templates & slots

    lightning & web components & templates & slots Web components, Custom elements, Template ...

  10. 看超额担保免信任的NGK DeFi 乐高如何打造下一个千倍币?

    2020年中,DeFi的高收益率吸引了大量热钱涌入,DeFi总锁仓量破百亿美金.如今,流动性挖矿的热潮暂时停歇,但对于 NGK DeFi项目来说,它背后的演变进化从未停止. 免信任是 NGK DeFi ...