typedef vector <int> vi;//The following operations are defined for iterators: get value of an iterator, int x = *it; , increment and decrement iterators it1++, it2--; , compare iterators by '!=' and by '<' , add an immediate to iterator it += 20; <=> shift 20 elements forward , get the distance between iterators, int n = it2-it1; , Not only can they operate on any container, they may also perform, for example, range checking and profiling of container usage.
//The type of iterator can be constructed by a type of container by appending “::iterator”, “::const_iterator”, “::reverse_iterator” or “::const_reverse_iterator” to it. use '!=' instead of '<', and 'empty()' instead of 'size() != 0' -- for some container types, it’s just very inefficient to determine which of the iterators precedes another.
typedef pair <int, int> ii;//Pairs are compared first-to-second element. If the first elements are not equal, the result will be based on the comparison of the first elements only; the second elements will be compared only if the first ones are equal. The array (or vector) of pairs can easily be sorted by STL internal functions.(convex hull - For example, if you want to sort the array of integer points so that they form a polygon, it’s a good idea to put them to the vector< pair<double, pair<int,int> >, where each element of vector is { polar angle, { x, y } }. One call to the STL sorting function will give you the desired order of points. )
llu f(int i, int j, int cyls, int n)/**oxygen required : i, nitrogen req : j, no of cylinders available : cyls, n : a variable used to alternate between 2 states (0 and 1)**/
{
if(dp[n][i][j]!= infy -1)return dp[n][i][j];/** result already calculated**/
dp[n][i][j]= min((llu)wc[cyls-1]+ dp[n^1][max((llu)0, i - oc[cyls-1])][max((llu)0, j - nc[cyls-1])], (llu)dp[n^1][i][j]);/**note how cleverly we pass only positive i, j this saves alot of checks which might be needed if i or j are -ve, as we need to access dp[n][i][j] **/
return dp[n][i][j];
}
void set_dp(int n)
{
dp[n]= ausi;
dp[0][0][0]=0;
}
int main()
{
auto Start = chrono::steady_clock::now();
// freopen("in.txt", "r", stdin);
int t, O, N, cylinders, n =1;
cin>>t;
while(t--)
{
dp = vector<vector < vector<int>>>(2, ausi);
dp[0][0][0]=0;
n =1;
cin>>O>>N;/**the amount of O and N desired **/
cin>>cylinders;/**the no of available cylinders**/
fi(i, 0, cylinders)
cin>>oc[i]>>nc[i]>>wc[i];/**specifications of available cylinders**/
fi(cyls, 1, cylinders+1)/**starting from cylinder 1 to last**/
{
fi(i, 0, O+1)
fi(j, 0, N+1)
dp[n][i][j]= min((llu)wc[cyls-1]+ dp[n^1][max((llu)0, i - oc[cyls-1])][max((llu)0, j - nc[cyls-1])], (llu)dp[n^1][i][j]);
n ^=1;
/**f(i, j, cyls, n); recursi11`1`on variant, slower than first method as it costs various redundant calls (dp can be used to get immediate result while f() needs to be called with parameters as indices of dp[][][]
set_dp(n);**/
}
n =!n;/**this adjustment gives us the right dp array, as n is toggled various times to alternate bw two states**/