Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class Quadruplet{
- public:
- int ai, bi, pi, mi;
- Quadruplet(int ai, int bi, int pi, int mi){
- this-> ai = ai;
- this-> bi = bi;
- this-> pi = pi;
- this-> mi = mi;
- }
- void display(){
- cout << this-> ai << " " << this-> bi << " " << this-> pi << " " << this-> mi << '\n';
- }
- };
- // Check if the all the stalls are cooled enough
- bool ifStallValid(vector<int> &stalls){
- for(int i = 1; i < stalls.size(); i++){
- if(stalls[i] > 0){
- return false;
- }
- }
- return true;
- }
- // Add heat of each cow as per their range
- void addCowHeatToStall(int &si, int &ti, int &ci, vector<int> &stalls){
- for(int i = si; i <= ti; i++){
- stalls[i] += ci;
- }
- }
- void GetMinCostToCoolCows(int csf, int &minCost, vector<int> &stalls, unordered_map<int, Quadruplet> &acs,vector<bool> &usedAc){
- //base case
- if(ifStallValid(stalls)){
- minCost = min(minCost, csf);
- return;
- }
- // iterate over each ac, to see which should be turned on
- // Mark the ac as used & add the cost, if ac is turned on
- for(auto ac : acs){
- if(!usedAc[ac.first]){
- int acNo = ac.first;
- usedAc[acNo] = true;
- for(int i = ac.second.ai ; i <= ac.second.bi; i++){
- stalls[i] -= ac.second.pi;
- }
- GetMinCostToCoolCows(csf + ac.second.mi, minCost, stalls, acs, usedAc);
- for(int i = ac.second.ai; i <= ac.second.bi; i++){
- stalls[i] += ac.second.pi;
- }
- usedAc[acNo] = false;
- }
- }
- }
- /*
- Control of positions - Which AC is to be turned on at which level
- Consider if the answer was a subset of 4 ACs, and total ACs = 10
- ----- , ----- , ----- , -----
- At each level we choose which AC to turn on.
- AC1,
- AC1, AC3
- AC1, AC3, AC4
- ...
- OR
- AC4,
- AC4, AC1
- AC4, AC1, AC9...
- We decide which AC would be turned on at which position. At each level we check if the ACs turned on till now cooled down all the stalls or not
- AC1, AC2, AC3, AC4 ...
- AC2, AC1, AC8, AC7 ...
- AC5, AC8, AC1, AC10 ...(Answer)
- At each level, we check if we have found the answer(all required stalls cooled off)
- */
- int main() {
- // your code goes here
- int n, m, si, ti, ci, ai, bi, pi, mi;
- cin >> n >> m;
- unordered_map<int, Quadruplet> acs;
- vector<int> stalls (101, 0);
- // Add heat of cows to stall
- for(int i = 0; i < n; i++){
- cin >> si >> ti >> ci;
- addCowHeatToStall(si, ti, ci, stalls);
- }
- // Create AC array
- for(int i = 0; i < m; i++){
- cin >> ai >> bi >> pi >> mi;
- Quadruplet newAc(ai, bi, pi, mi);
- acs.insert({i, newAc});
- }
- // Answer
- int minCost = INT_MAX;
- vector<bool> usedAc(m, false);
- GetMinCostToCoolCows(0, minCost, stalls, acs, usedAc);
- cout << minCost << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement