Advertisement
Guest User

Untitled

a guest
Jan 4th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <unordered_map>
  4.  
  5. #pragma GCC optimize ("O3")
  6.  
  7. class Particle {
  8. private:
  9.   static const int MAX_POSITION = 65535;
  10.   static const int MIN_POSITION = 0;
  11.  
  12.   bool hasCollided = false;
  13.   bool isMatterType;
  14.   int positionX;
  15.   int positionY;
  16.   int speedX;
  17.   int speedY;
  18.   int lifetime;
  19. public:
  20.   Particle(bool isMatter, int positionX, int positionY, int speedX, int speedY, int lifetime) :
  21.       isMatterType(isMatter),
  22.       positionX(positionX),
  23.       positionY(positionY),
  24.       speedX(speedX),
  25.       speedY(speedY),
  26.       lifetime(lifetime) { }
  27.  
  28.   void tick() {
  29.     --this->lifetime;
  30.     this->positionX += speedX;
  31.     this->positionY += speedY;
  32.   }
  33.  
  34.   bool isAlive() const {
  35.     return this->lifetime > 0 &&
  36.            !this->hasCollided &&
  37.            this->positionX >= MIN_POSITION && this->positionX <= MAX_POSITION &&
  38.            this->positionY >= MIN_POSITION && this->positionY <= MAX_POSITION;
  39.   }
  40.  
  41.   long getPosition() const {
  42.     return this->positionX * MAX_POSITION + this->positionY;
  43.   }
  44.  
  45.   bool isMatter() const {
  46.     return this->isMatterType;
  47.   }
  48.  
  49.   void setCollided() {
  50.     this->hasCollided = true;
  51.   }
  52. };
  53.  
  54. void parseParticles(std::list<Particle>& particles) {
  55.   int particlesCount;
  56.   std::cin >> particlesCount;
  57.  
  58.   for (int i = 0, positionX, positionY, speedX, speedY, lifetime; i < particlesCount; ++i) {
  59.     char type;
  60.     std::cin >> type >> positionX >> positionY >> speedX >> speedY >> lifetime;
  61.     particles.emplace_back(type == 'm', positionX, positionY, speedX, speedY, lifetime);
  62.   }
  63. }
  64.  
  65. void tickParticles(std::list<Particle>& particles,
  66.                    int ticks,
  67.                    size_t& matterParticlesCount,
  68.                    size_t& antimatterParticlesCount,
  69.                    size_t& collidedParticles) {
  70.   std::unordered_map<long, std::list<Particle*>> particlesByPosition;
  71.   while (--ticks >= 0) {
  72.     particlesByPosition.clear();
  73.     particles.remove_if([&](Particle& particle) {
  74.       particle.tick();
  75.       if (particle.isAlive()) {
  76.         particlesByPosition[particle.getPosition()].push_back(&particle);
  77.         return false;
  78.       }
  79.       return true;
  80.     });
  81.  
  82.     for (auto const& position : particlesByPosition) {
  83.       bool hasMatter = false;
  84.       bool hasAntimatter = false;
  85.       std::list<Particle*> particlesList = position.second;
  86.       for (auto const& particle: particlesList) {
  87.         if (particle->isMatter()) {
  88.           hasMatter = true;
  89.         } else {
  90.           hasAntimatter = true;
  91.         }
  92.         if (hasMatter && hasAntimatter) {
  93.           for (auto const& p: particlesList) {
  94.             p->setCollided();
  95.           }
  96.           collidedParticles += particlesList.size();
  97.           break;
  98.         }
  99.       }
  100.     }
  101.   }
  102.  
  103.   for (auto const& particle : particles) {
  104.     if (particle.isAlive()) {
  105.       if (particle.isMatter()) {
  106.         ++matterParticlesCount;
  107.       } else {
  108.         ++antimatterParticlesCount;
  109.       }
  110.     }
  111.   }
  112. }
  113.  
  114. int main() {
  115.   std::ios_base::sync_with_stdio(false);
  116.   std::cin.tie(nullptr);
  117.  
  118.   std::list<Particle> particles;
  119.   parseParticles(particles);
  120.  
  121.   int ticks;
  122.   std::cin >> ticks;
  123.  
  124.   size_t matterParticlesCount = 0, antimatterParticlesCount = 0, collidedParticles = 0;
  125.  
  126.   tickParticles(particles, ticks, matterParticlesCount, antimatterParticlesCount, collidedParticles);
  127.  
  128.   std::cout << matterParticlesCount << " " << antimatterParticlesCount << std::endl << collidedParticles << std::endl;
  129.  
  130.   return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement