----------------------------------------------------------editorial---------------------------------------------
Let's use binary search approach. For given number of hamburgers (say, x) let's find the minimal number of money needed to cook them. Say, for one hamburger Polycarpus needs cb bread pieces, cs sausages pieces, cc cheese pieces. So for x hamburgers he needs: cb·x, cs·x and cc·x pieces (by types). Since he already has nb, ns and nc pieces, so he needs to buy:
- bread: max(0, cb·x - nb),
- sausages: max(0, cs·x - ns),
- cheese: max(0, cc·x - nc).
So the formula to calculate money to cook x hamburgers is:
Obviously, the function f(x) is monotonic (increasing). So it is possible to use binary search approach to find largest x such that f(x) ler
-------------------------------------------code--------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int arr[20][20];
typedef long long int lli;
lli dp[1<<15];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(dp,-1,sizeof dp);
int n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
lli ret=1000000000000;
for(int i=0;i<n;i++)
{
ret=min(ret,solve(mask |(1<<i) )+arr[i][i]);
}
cout<<ret<<endl;
}
}
No comments:
Post a Comment