Cod sursa(job #3130405)

Utilizator RobertCNMBrobertM RobertCNMB Data 17 mai 2023 18:40:29
Problema Aprindere Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <fstream>
#include <deque>
#include <vector>

using namespace std;
ifstream fin("aprindere.in");
ofstream fout("aprindere.out");

struct swit {
    int room;
    int time;
    int nr;
    deque<int> bulbs;
} ;

vector<swit> switches;
vector<swit>::iterator maxi;
int n, m, timecounter = 0;
int state[1000], done = false;


int scoreof (vector<swit>::iterator s){
    int result = 0;
    for (deque<int>::iterator dit = s->bulbs.begin(); dit != s->bulbs.end(); ++dit){
        if (! state[*dit])
            ++result;
        else --result;
    }
    return result;
}

int main(){
    fin >> n >> m;
    switches.resize(m, {0, 0, 0, deque<int>()});
    for (int i = 0; i < n; i++)
        fin >> state[i];
    for (int i = 0; i < m; i++){
        fin >> switches[i].room >> switches[i].time >> switches[i].nr;
        for (int j = 0; j < switches[i].nr; j++){
            int aux;
            fin >> aux;
            switches[i].bulbs.push_back(aux);
        }
    }
    while (! done){
        //we find the optimal switch to trigger
        maxi = switches.begin();
        for (vector<swit>::iterator it = switches.begin(); it != switches.end(); ++it){
            if (it != switches.begin()){
                int scoreit = scoreof(it), scoremaxi = scoreof(maxi);
                if (scoreit * maxi->time > scoremaxi * it->time)
                    maxi = it;
            }
        }

        //we actually trigger the said switch
        for (int i = 0; i < maxi->nr; i++)
            state[maxi->bulbs[i]] ^= 1;

        //we count the time
        timecounter += maxi->time;

        //we remove the switch such that we dont trigger it again
        switches.erase(maxi);
        
        //we check to see if we're done
        done = true;
        for (int i = 0; i < n; i++){
            if (state[i] == 0)
                done = false;
        }
    }

    fout << timecounter;
    return 0;
}