BZOJ 1724 USACO 2006 Nov. 切割木板
倒过来的合并果子?
做法与合并果子一样
维护一个小根堆,每次取出最小的两个数进行合并
1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 using namespace std; 5 int n; 6 long long ans=0; 7 struct cmp{ 8 bool operator() (const int a,const int b)const{return a>b;} 9 }; 10 priority_queue<int,vector<int>,cmp>a; 11 void read(int &k){ 12 int f=1; k=0; char c=getchar(); 13 while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 14 while ('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 15 k*=f; 16 } 17 int main(){ 18 read(n); 19 for (int i=1;i<=n;i++){ 20 int x; read(x); 21 a.push(x); 22 } 23 for (int i=1;i<n;i++){ 24 int x,y; 25 x=a.top(); a.pop(); 26 y=a.top(); a.pop(); 27 ans+=x+y; a.push(x+y); 28 } 29 printf("%lld",ans); 30 return 0; 31 }