日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

2019 GDUT Rating Contest II : A. Taming the Herd

發(fā)布時(shí)間:2023/12/10 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2019 GDUT Rating Contest II : A. Taming the Herd 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題面:

A. Taming the Herd

Input ?le: standard input Output ?le: 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 con?dent 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 ?rst 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 de?nitely 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.

題目描述:

奶牛搞破壞,把奶牛棚弄壞了。從奶牛棚被破壞的那一天起,農(nóng)夫就開始寫日志,并且每一天都會(huì)寫。這個(gè)日志記錄的是:離上次奶牛棚被破壞的天數(shù)。如果當(dāng)天奶牛棚被損壞,就記錄為0;如果前3天奶牛棚被損壞,就記錄為3。年末,農(nóng)夫拿這個(gè)日志找奶牛“算賬”的時(shí)候,發(fā)現(xiàn)這個(gè)日志被損壞了(有些部分被丟失了)。現(xiàn)在要幫助農(nóng)夫判斷:這個(gè)日志是否“合法”。如果“合法”,就輸出最小可能和最大可能奶牛棚被破壞的天數(shù);如果不“合法”,就輸出-1。(當(dāng)天日志被丟失的部分用-1表示)

題目分析:

這道題是一道水題,但是由于看錯(cuò)題目(英語渣的我),當(dāng)場(chǎng)沒有做出來?。這道題大概是這樣的: 1.第一天要么就是0,要么就是-1(被丟失)。如果不是0和-1,直接可以判斷不合法。 2.我們可以根據(jù)日志上的其他剩余信息推算出其他天的日志信息,最簡(jiǎn)單的:第一天無論是0還是-1,一定是可以推算出是0。其他:比如: 這里第6天記錄了一個(gè)“2”,代表前2天奶牛棚被壞了,所以第4天一定是0,第5天一定是1: 如果第4天不是0,或者第5天不是1,那么一定不合法: 我們?cè)傧胍幌?#xff1a;第7,8,9天我們能推出來嗎?根據(jù)現(xiàn)有條件第7,8,9天的日志內(nèi)容是不一定的:假如第6天后奶牛棚沒有被破壞,那么后面的所有內(nèi)容就可以被推出來,否則也不一定。所以現(xiàn)在就先要編寫一個(gè)把能推的都推出來的代碼,看究竟是否合法。 在寫這個(gè)代碼的時(shí)候,一般我們會(huì)從1遍歷倒N,可是這里反過來遍歷(N-1)代碼會(huì)更容易些,為什么?假設(shè)第6天記錄的是3,那么第5天是不是記錄的是3-1,也就是2: 然后就一直進(jìn)行減1的操作,直到是0為止: 這樣想的結(jié)果是不是和剛剛按從1-N的順序想的結(jié)果一樣,但是代碼只需要從(N-1)遍歷一次就行了。 推算完全部結(jié)果后(這時(shí)肯定是合法的,如果中間檢查到不合法就直接輸出-1然后結(jié)束程序),奶牛棚被破壞的天數(shù)最少的天數(shù)就是推算出來后,日志內(nèi)容是0的總天數(shù)。 (注:這里最少天數(shù)為2) 最多的天數(shù)怎樣計(jì)算?只要把不能推算出來的日志內(nèi)容全部變?yōu)?: 然后統(tǒng)計(jì)0的個(gè)數(shù)(不能推算出來日志內(nèi)容的天數(shù) + 最少天數(shù))就是最大天數(shù)的(這里最大天數(shù)為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; //最簡(jiǎn)單的不合法情況 14 return 0; 15 } 16 17 a[1] = 0; //這個(gè)不要漏 18 for(int i = n; i >= 1;){ 19 while(a[i] == -1 && i >= 1) i--; //寫這種代碼時(shí)一定要記得 "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++){ //簡(jiǎn)單的計(jì)數(shù) 35 if(a[i] == 0) minn++; 36 if(a[i] == -1) cnt++; 37 } 38 39 cout << minn << " " << minn+cnt << endl; 40 return 0; 41 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/happy-MEdge/p/10530860.html

總結(jié)

以上是生活随笔為你收集整理的2019 GDUT Rating Contest II : A. Taming the Herd的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。