Header Ad

HackerEarth Three problems solution

In this HackerEarth Three problems solution, You are given n problems. The problems are of three types, 'Type1', 'Type2', and 'Type3'. There are t1 'Type1' problems, t2 'Type2' problems, and t3 'Type3' problems. You can solve each problem using any of the three methods, 'A', 'B', and 'C'. You can use a particular method only a limited number of times that is, method 'A' for a times, method 'B' for b times, and method 'C' for c times. 

You are given a  matrix 3 x 3 where A represents the effort to solve a Type i problem using Method j. You are required to find the minimum effort required to solve all the problems.


HackerEarth Three problems solution


HackerEarth Three problems solution.

#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

ll n,t[4],m[4],tt[4],mm[4],A[4][4];

void solve(){
cin>>n;
rep(i,1,3) cin>>t[i];
rep(i,1,3) cin>>m[i];
vector<vi>p;
rep(i,1,3) rep(j,1,3){
cin>>A[i][j];
p.pb({i,j});
}
ll ans=1e18;
do{
rep(i,1,3) tt[i]=t[i],mm[i]=m[i];
ll sum=0;
for(vi v:p){
int x=min(tt[v[0]],mm[v[1]]);
sum+=A[v[0]][v[1]]*x;
tt[v[0]]-=x;
mm[v[1]]-=x;
}
if(tt[1]+tt[2]+tt[3]==0) ans=min(ans,sum);
}
while(next_permutation(all(p)));
cout<<ans<<'\n';
}

signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}

Second solution

#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 114;
const long long INF = 1e18;

int main() {
int t;
cin >> t;
while (t--) {
int a[3], b[3];
cin >> a[0] >> a[0] >> a[1] >> a[2];
cin >> b[0] >> b[1] >> b[2];
int t[9];
for (int &i : t)
cin >> i;
int per[9];
int c[3], d[3];
memcpy(c, a, sizeof a);
memcpy(d, b, sizeof b);
iota(per, per + 9, 0);
long long ans = INF;
do {
long long cur = 0;
for_each(per, per + 9, [&](int i) {
int x = min(a[i / 3], b[i % 3]);
cur += (long long) t[i] * x;
a[i / 3] -= x;
b[i % 3] -= x;
});
ans = min(ans, cur);
memcpy(a, c, sizeof a);
memcpy(b, d, sizeof b);
} while (next_permutation(per, per + 9));
cout << ans << '\n';
}
}


Post a Comment

0 Comments