巴啦啦能量之超級無敵連接嚶嚶嚶(我莫不是個傻子)
這題。。。好水。。。
暴力加一丟丟優化都能過。。
可是咱們老師要咱們用堆,那也沒辦法 咱也不敢說咱也不敢問
具體註釋在代碼裏面了,本身看看嘍(比心心)~~ios
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<queue> 5 #include<cmath> 6 #include<cstring> 7 using namespace std; 8 9 int n,ans; 10 priority_queue<int,vector<int>,greater<int> > zy; 11 12 int main(){ 13 scanf("%d",&n); 14 for(int i=1;i<=n;i++){ 15 int r; 16 scanf("%d",&r); 17 zy.push(r);//讀入,自主維護 18 } 19 for(int i=1;i<=n-1;i++){ 20 int z=q.top(); 21 zy.pop();//第一個讀取,彈出 22 int y=zy.top(); 23 zy.pop();//第二個讀取,彈出 24 zy.push(z+y);//合併,再放入堆中 25 ans+=z+y; 26 } 27 printf("%d\n",ans); 28 return 0; 29 }
那。。新人開博鼓勵一下吧。。優化