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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 5914 】Triangle (打表 或 set维护)

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 5914 】Triangle (打表 或 set维护) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

Mr. Frog has n sticks, whose lengths are 1,2, 3??n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides to steal some sticks! Output the minimal number of sticks he should steal so that Mr. Frog cannot form a triangle with?
any three of the remaining sticks.

Input

The first line contains only one integer T (T≤20T≤20), which indicates the number of test cases.?

For each test case, there is only one line describing the given integer n (1≤n≤201≤n≤20).

Output

For each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of sticks Wallice should steal.

Sample Input

3 4 5 6

Sample Output

Case #1: 1 Case #2: 1 Case #3: 2

解題報告:

? ? 看網(wǎng)上的題解都說是打表,那怎么打的表啊?手推?萬一不正確呢?有點僥幸啊。。其實這題也可以搜索來找答案,但是還有更好的方法,比如用set,也是0ms過,我看好多打表的都是15ms,不知道是什么情況。

AC代碼:

#include<bits/stdc++.h>using namespace std;int main() {int t,n;int iCase = 0;scanf("%d",&t);while(t--) {scanf("%d",&n);printf("Case #%d: ",++iCase);if(n <= 3) {printf("0\n");continue;}int ans = 0;set<int> st;for(int i = 1; i<=n; i++) st.insert(i);set<int>::iterator it,itt,ittt,qq;int flag = 1;while(flag) {qq=st.end();qq--;qq--;for(it = st.begin(); it!=qq; ++it) {itt=++it;--it;ittt=++itt;--itt;if(*it + *itt > *ittt) {st.erase(ittt);ans++;break;}++it;if(it == qq) flag=0;--it;}}printf("%d\n",ans);}return 0 ; }

總結(jié):

? ? 剛開始寫的時候錯誤還是比較多的,比如for遍歷的時候it != st.end(),但是這題不行,因為itt和ittt就越界了。。所以這題qq迭代器就是記錄一下st.end()往前兩個是多少。還有一個坑點就是需要實時計算qq的值,因為你有erase操作,所以st.end()就在變,所以qq也要跟著變才對。再就是n<=3的時候要特判,輸出格式的空格要注意。


還有一個類似的題目在別的題解上看到了這里粘貼一下:

切金條

一根金條長31厘米,如果把它切成數(shù)段,使其中的一段或幾段能夠接成長為1到31中任何整數(shù)的金條,請問要切幾次?

題解:第一反應(yīng)是斐波那契數(shù)列1 2 3 5 8 12,切5刀,結(jié)果錯了,這道題應(yīng)該用二進制做。123是不需要3的,因為12可以構(gòu)成3,所以124是更好的選擇。

這兩道題好像,但是這是兩道題,一定要仔細區(qū)分開。

1 2 4 8 16,和正好31,切4刀,答案是4。

總結(jié)

以上是生活随笔為你收集整理的【HDU - 5914 】Triangle (打表 或 set维护)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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