Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <algorithm>
- using namespace std;
- ifstream fi("marmot.in");
- ofstream fo("marmot.out");
- const int NMAX=1e6+5,INF=1e9;
- int n,s[NMAX],sum;
- pair <int,int> m[NMAX],in[NMAX];
- void structure(int nr)
- {
- if(in[-nr].first<0)
- {
- fo<<"{";
- structure(in[-nr].first);
- fo<<"}";
- }
- else fo<<"("<<s[in[-nr].first]<<")";
- if(nr==-n+1)
- fo<<"-root-";
- else fo<<"-i-";
- if(in[-nr].second<0)
- {
- fo<<"{";
- structure(in[-nr].second);
- fo<<"}";
- }
- else fo<<"("<<s[in[-nr].second]<<")";
- }
- int main()
- {
- ///n=number of marmots
- fi>>n;
- for(int i=1;i<=n;i++)
- {
- fi>>m[i].first;///m[i].first=number of awakenings for marmot i
- m[i].second=i;///m[i].second=the indice before sorting of the marmot i
- ///(we need this number for the structure)
- s[i]=m[i].first;
- }
- sort(m+1,m+n+1);
- for(int i=1;i<n;i++)
- {
- sum+=m[1].first+m[2].first;
- m[1].first=m[1].first+m[2].first; /// we give m[1] the value of the new intersection created
- in[i]={m[1].second,m[2].second};
- m[1].second=-i;
- m[2].first=INF;///we give m[2] a big value in order to avoid using it again
- sort(m+1,m+n+1);
- }///we repeat this process n-1 times, because in the end we need to have one intersection left, which is the root
- fo<<"minimal sum is "<<sum<<"\n";///sum=answer to our problem, the sum of weights
- ///In this algorithm we find the minimal sum in complexity O(N^2*log2N)
- structure(-(n-1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement