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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

POJ 3253 Fence Repair C++ STL multiset 可解

發布時間:2024/4/19 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3253 Fence Repair C++ STL multiset 可解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 53106 Accepted: 17508
Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input

3
8
5
8
Sample Output

34
Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

思路:在集合中每次找最短的兩個加起來,算到ans里。
   再將這兩個數從集合中刪除,把他們的和加入到集合里。

  用multiset解的話,插入和查找都是O(log(n)),再加上遍歷一遍,總復雜度是:O(nlog(n))。

代碼:

#include <iostream> #include <set> using namespace std; typedef long long ll;multiset <int> s;int main() {int n,key;cin >> n;for(int i = 0;i < n; i++) cin >> key,s.insert(key);ll ans = 0;while(s.size() > 1){int k1 = *s.begin();s.erase(s.begin());int k2 = *s.begin();s.erase(s.begin());int t = k1+k2;ans += t;s.insert(t);}cout << ans << endl;return 0; } // writen by zhangjiuding

總結

以上是生活随笔為你收集整理的POJ 3253 Fence Repair C++ STL multiset 可解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 精品婷婷色一区二区三区蜜桃 | 亚洲欧美另类在线视频 | 青草青青视频 | 在线播放日韩av | 爱综合网| 中文字幕一区二区三区乱码 | 五月丁香花 | 国产一区二区三区高清视频 | 成人18在线 | 人人精品久久 | 精品婷婷色一区二区三区蜜桃 | 国产精品色在线网站 | 欧美人与禽zozzo性之恋的特点 | 亚洲成av人片在线观看无码 | 不卡久久 | 亚洲欧美日韩图片 | 在线天堂中文字幕 | 久久无码专区国产精品s | 国产做a视频 | 精品国产一区二区三区久久久 | 极品新婚夜少妇真紧 | 欧美日韩图片 | 偷拍老头老太高潮抽搐 | 午夜毛片在线观看 | 欧美大片免费观看网址 | 91丝袜国产在线观看 | 不用播放器的av网站 | 麻豆成人久久精品一区二区三区 | 亚洲高清视频免费观看 | 国产精品成人va在线观看 | 在线观看一区视频 | 黄视频在线免费看 | 夜夜嗨av一区二区三区 | 亚洲综合色网 | 成人a视频| 欧美瑟瑟 | 日韩欧美一区二区三区四区五区 | 亚洲天堂免费视频 | 欧美 国产 日本 | 中文字幕av一区二区三区 | 亚洲av无码一区二区三区dv | 午夜视频网 | 99热这里 | 国产欧美一区二区三区国产幕精品 | 51精品国自产在线 | 男女爱爱福利视频 | 综合网亚洲 | 一区视频在线播放 | 欧美h视频在线观看 | 久久精品国内 | 韩国三级国产 | 小毛片在线观看 | 国产av电影一区二区三区 | 中日黄色片 | 欧美xxxxx少妇 | 国产女主播在线 | 欧美a v在线播放 | 中文字幕在线乱 | 中文字幕第一区 | 国产做a| 91蝌蚪91九色白浆 | 亚洲无吗在线观看 | 精品国产精品国产偷麻豆 | 中文字幕中文字幕 | 99热这里只有精品首页 | 影音先锋制服丝袜 | 91国内产香蕉 | 日韩精品一卡 | 成人黄色小说在线观看 | 免费在线一区二区 | 国产av成人一区二区三区高清 | 国产色频| 亚洲激情在线观看视频 | 免费福利av | 欧美日韩亚洲在线观看 | 国产91精品久久久久 | 麻豆伦理片 | 亚洲欧美激情在线 | 欧美天天 | 麻豆免费av| 成人av电影免费观看 | 国产精品18久久久久久vr下载 | 国产高清视频免费 | 人人爽人人 | 久久久久噜噜噜亚洲熟女综合 | 奇米影视狠狠 | 四虎影库在线播放 | 国产十八熟妇av成人一区 | 破处视频在线观看 | 精品国产乱码久久久久久牛牛 | 91老师片黄在线观看 | 韩国av三级 | 丁香婷婷深情五月亚洲 | 亚洲国产精品女人久久久 | www嫩草| 久久久久夜| 亚洲一区在线播放 | 国产永久免费无遮挡 | 国产一区二区在线精品 |