Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long int
- using namespace std;
- struct state{
- set<ll> last; //height of last histogram picked
- ll max_val; //value of best possible answer
- map <ll,ll> max_val_count; //count of best possible answer
- state(){
- max_val=0;
- }
- };
- ll heights[100],N,lim,temp;
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- cin>>N;
- lim=pow(2,N)-1;
- for(int i=0;i<N;i++) cin>>heights[i];
- struct state dp[33000]; //you only need up to 0-32,768
- dp[0].max_val=0;
- dp[0].max_val_count[0]=1;
- dp[0].last.insert(0);
- for(int i=1;i<=lim;i++){
- for(int j=0;j<N;j++){
- if(i&(1<<j)){
- // if jth bit is set , unset it and find the answer for the remaining bits
- set <ll>::iterator it;
- for( it=dp[i&~(1<<j)].last.begin();it!=dp[i&~(1<<j)].last.end();it++){
- temp=dp[i&~(1<<j)].max_val - *it + abs(*it-heights[j]) + heights[j] +2;
- if(temp>dp[i].max_val){
- dp[i].max_val=temp;
- dp[i].max_val_count.clear(); dp[i].max_val_count[heights[j]]=(dp[i&~(1<<j)].max_val_count[*it]);
- dp[i].last.clear(); dp[i].last.insert(heights[j]);
- }
- else if(temp==dp[i].max_val){
- dp[i].max_val_count[heights[j]]+=(dp[i&~(1<<j)].max_val_count[*it]);
- dp[i].last.insert(heights[j]);
- }
- }
- }
- }
- cout<<i<<"::"<<dp[i].max_val<<" ";
- for(auto it=dp[i].last.begin();it!=dp[i].last.end();it++) cout<<*it<<":"<<dp[i].max_val_count[*it]<<" ";
- cout<<endl;
- }
- ll ans=0;
- for(auto it=dp[lim].last.begin();it!=dp[lim].last.end();it++) ans+=dp[lim].max_val_count[*it];
- cout<<dp[lim].max_val<<" "<<ans<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment