萌新求助~~只过了第一个,剩下都是MLE。。。。

P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G

QAQ
by Andy_chen @ 2020-02-18 11:23:37


@[17843327022wsh](/user/298189) STL好呀
by 菜鸡gyf @ 2020-02-18 11:31:28


应该是sort函数有问题,递归层数过多导致爆栈了。
by zztqwq @ 2020-02-18 11:56:26


sort递归爆栈,还是学学STL的priority_queue(优先队列)吧
by ych153 @ 2020-02-18 11:59:39


@[juruo_zzt](/user/125913) 谢谢大家,我改了以后变成第一个AC其他都WA了(苦笑) 请大佬们帮忙看看,谢谢~~~ ``` #include<stdio.h> #include<algorithm> using namespace std; int a[10005]; bool cmp(const int &a,const int &b) { return a>b; } int main() { int i,j,n,f=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n,cmp); while(n>1) { j=a[n-1]+a[n-2]; f+=j; n--; for(i=n-1;i>0;i--) { if(j>a[i-1]) a[i]=a[i-1]; else {a[i]=j;break;} } } printf("%d",f); return 0; } ```
by _biu_ @ 2020-02-19 17:25:12


``` //用优先队列吧 #include <bits/stdc++.h> using namespace std; typedef long long LL; priority_queue<int, vector<int>, greater<int> > q; int main(){ int n, a; cin >> n; for(int i = 0; i < n; ++ i){ cin >> a; q.push(a); } int sum = 0; int b, c; a = 0; while(q.size() != 1){ b = q.top(); q.pop(); c = q.top(); q.pop(); a = b + c; //cout << b << " " << c << endl; sum += a; q.push(a); } cout << sum << endl; return 0; } ```
by xiaoxiaoAK @ 2020-03-09 12:43:43


|