2019 GDUT Rating Contest II : A. Taming the Herd
题面:
A. Taming the Herd
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!
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.
题目描述:
题目分析:








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的更多相关文章
- 2019 GDUT Rating Contest II : Problem F. Teleportation
题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...
- 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 ...
- 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 ...
- 2019 GDUT Rating Contest II : Problem B. Hoofball
题面: 传送门 B. Hoofball Input file: standard input Output file: standard output Time limit: 5 second Memor ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 给你的SpringBoot项目定制一个牛年专属banner吧
新春快乐,牛年大吉! 新的一年是牛年,在SpringBoot项目里自定义了一个牛年相关的banner,看起来可真不错. 上面是自己制作的一个banner,相关的ASCII字符在文末. SpringBo ...
- Win10 Nodejs搭建http-server注意点
下载安装,并用命令行查看版本:如果提示输入命令找不到等,可能是没有安装成功,或者是环境变量引起的: 如果在提示安装不成功可能是win10权限问题,最好使用管理员模式运行cmd,再用cmd命令打开安装文 ...
- SpringBoot 全局视角看springboot
从单体架构到微服务 单体架构 任何一个网站在发布初期几乎都不可能立马就拥有庞大的用户流量和海量数据,都是在不停 的试错过程中一步一步演变其自身架构,满足其自身业务.比如现在能够抗住双十一这么大 流量的 ...
- flex 布局占位符
flex 布局占位符 空 span bug .popover-custom-class .system-guide-container .buttons-box { display: flex; fl ...
- MBP 屏幕分辨率 All In One
MBP 屏幕分辨率 All In One screen size bug https://stackoverflow.com/questions/65462643/how-to-get-the-rea ...
- style element & web components
style element & web components style.textContent style.setContent bug style.textContent const st ...
- how to auto open a url in the browser by using terminal
how to auto open a url in the browser by using terminal Linux / MacOS # bash / zsh $ open http://loc ...
- 教你玩转CSS Position(定位)
CSS Position(定位) position 属性指定了元素的定位类型. position 属性的五个值: static relative fixed absolute sticky 元素可以使 ...
- socket通信框架——boost asio
boost asio是一个封装了基本socket的跨平台通信框架.它支持异步访问,并支持tcp的自动封闭控制等操作. 一个简单的通信协议可以为: header body body长 数据 通过boos ...
- java线程池趣味事:这不是线程池
要想写出高性能高并发的应用,自然有许多关键,如io,算法,异步,语言特性,操作系统特性,队列,内存,cpu,分布式,网络,数据结构,高性能组件. 胡说一通先. 回到主题,线程池.如果说多线程是提高系统 ...