Cod sursa(job #541837)

Utilizator feelshiftFeelshift feelshift Data 25 februarie 2011 14:48:21
Problema Walls Scor 10
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 2.14 kb
// http://infoarena.ro/problema/walls
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream in("walls.in");
ofstream out("walls.out");

struct stuff {
    int x;
    int width;
    int height;

    vector<int> damaged;
};

int nrWalls,nrAttacks;
vector<stuff> wall;

void readWalls();
void procesAttacks();

bool comp(stuff one,stuff two) {
    return one.height < two.height;
}

int main() {
    readWalls();
    procesAttacks();

    return (0);
}

void readWalls() {
    int currentWidth;
    stuff tmp;

    in >> nrWalls;

    tmp.x = 1;
    in >> tmp.width;
    in >> tmp.height;
    wall.push_back(tmp);

    for(int i=1;i<nrWalls;i++) {
        in >> currentWidth;
        tmp.x += currentWidth + 1;

        in >> tmp.height;

        wall.push_back(tmp);
    }
}

void procesAttacks() {
    in >> nrAttacks;
    int x,y;
    bool ok = false;
    int maxHeight;
    stuff tmp;

    tmp = *max_element(wall.begin(),wall.end(),comp);
    maxHeight = tmp.height;

    for(int i=1;i<=nrAttacks;i++) {
        in >> x >> y;
        ok = false;

        if( y <= maxHeight)
        for(int it=nrWalls-1;it>=0;it--)
            if(wall[it].x + wall[it].width <= x && wall[it].height >= y) {
                int counter = 1;
                ok = true;

                counter = count(wall[it].damaged.begin(),wall[it].damaged.end(),y);

                // daca e feliat zidul
                if(counter + 1 == wall[it].width) {
                    wall[it].height = wall[it].height - (wall[it].height - y);
                    if(!wall[it].height)
                        wall.erase(wall.begin()+it);
                    out << "HIT " << wall[it].x+wall[it].width-counter-1 << " " << it+1 << " " << "YES\n";

                }
                else {
                    wall[it].damaged.push_back(y);
                    out << "HIT " << wall[it].x+wall[it].width-counter-1 << " " << it+1 << " " << "NO\n";
                }

                break;
            }

        if(!ok)
            out << "MISS\n";
    }

    in.close();
    out.close();
}