| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Here we have to apply a bitmask of 2^n state so as to generate all possible bitmasked state. Now it is understood the for any subset {1,2,3}. we can generate all numbers between low[1]+low[2]+low[3] to high[1]+high[2]+high[3] Now we can form a simple array and update freq[low]++ and freq[high+1]-- . And after forming the prefix all indices that have value greater than 1 can be generated. But this solution gives a memory timed out exception . So we need to realise the points in form of segments and apply a line sweep through them.The code is there for understanding |
#include<bits/stdc++.h>
using namespace std;
int cost[20];
int unit[20];
int dp[210];
int idx=0;
#define inf 10000000
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
int countPossibilities(vector<int> low, vector<int> high )
{
int n=low.size();
int val=(1<<n)-1;
int mx=0;
vector<pair<int,int> > inv;
for(int i=1;i<=val;i++)
{
int sumi=0;
int sumx=0;
for(int j=0;j<n;j++)
{
if(i>>j &1 )
{
sumi+=low[j];
sumx+=high[j];
}
}
// cout<<"sumi "<<sumi<<" "<<sumx<<endl;
mx=max(sumx,mx);
inv.pb(mp(sumi,sumx));
}
int ans=0;
sort(inv.begin(),inv.end());
n=inv.size();
// for(int i=0;i<n;i++)
// {
// cout<<inv[i].ff<<" "<<inv[i].ss<<endl;
//}
int s=inv[0].ff;
int end=inv[0].ss;
for(int i=1;i<n;i++)
{
if(end>=inv[i].ff)
{
end=max(end,inv[i].ss);
}
else
{
if(end<=9000)
{
ans+=0;
}
else
{
ans+=(end-max(9001,s))+1;
}
s=inv[i].ff;
end=inv[i].ss;
}
}
if(end>9000)
{
ans+=(end-max(s,9001))+1;
}
return ans;
}
int main()
{
int n,m;
cin>>n;
int xx;
vector<int> a,b;
for(int i=0;i<n;i++)
{
cin>>xx;
a.push_back(xx);
}
for(int i=0;i<n;i++)
{
cin>>xx;
b.push_back(xx);
}
cout<< countPossibilities(a,b)<<endl;
return 0;
}
No comments:
Post a Comment