poj Fence Repair 贪心
反过来做,就是要合成一个板,容易想到每次选取最小的两块合并,结果最优。#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define ll long long using namespace std; const int maxn=2e4+9; int main() { int n; priority_queue <ll,vector<ll>,greater<ll> > q; while(scanf("%d",&n)!=EOF) { while(!q.empty()) q.pop(); for(int i=1,tmp;i<=n;i++) { scanf("%d",&tmp); q.push(tmp); } long long ans=0; for(int i=1;i<n;i++) { long long tmp=0; tmp+=q.top(),q.pop(); tmp+=q.top(),q.pop(); ans+=tmp; q.push(tmp); } cout<<ans<<endl; } return 0; }
补充:软件开发 , C++ ,