0% found this document useful (0 votes)
74 views2 pages

Magic Star Intensity Code

Tcs code

Uploaded by

govindeduway
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views2 pages

Magic Star Intensity Code

Tcs code

Uploaded by

govindeduway
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Magic Star Intensity Code

C++
TCS CodeVita Round 1 Zone 1

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
struct Line
{
int x1, y1, x2, y2;
};

int countCells(Line line, pair<int, int> star, bool split)


{
if (line.x1 == line.x2)
{
if (split)
{
return min(abs(star.second - line.y1), abs(star.second - line.y2)) + 1;
}
else
{
return abs(line.y1 - line.y2) + 1;
}
}
else
{
if (split)
{
return min(abs(star.first - line.x1), abs(star.first - line.x2)) + 1;
}
else
{
return abs(line.x1 - line.x2) + 1;
}
}
}

bool intersects(Line a, Line b, pair<int, int>& intersection)


{
if (a.x1 == a.x2 && b.y1 == b.y2) {
if (b.x1 <= a.x1 && a.x1 <= b.x2 && a.y1 <= b.y1 && b.y1 <= a.y2)
{
intersection = {a.x1, b.y1};
return true;
}
}
if (a.y1 == a.y2 && b.x1 == b.x2)
{
if (a.x1 <= b.x1 && b.x1 <= a.x2 && b.y1 <= a.y1 && a.y1 <= b.y2)
{
intersection = {b.x1, a.y1};
return true;
}
}
return false;
}

int main()
{
int N, K;
cin >> N;
vector<Line> lines(N);
for (int i = 0; i < N; ++i)
{
cin >> lines[i].x1 >> lines[i].y1 >> lines[i].x2 >> lines[i].y2;
if (lines[i].x1 > lines[i].x2 || (lines[i].x1 == lines[i].x2 && lines[i].y1
> lines[i].y2))
{
swap(lines[i].x1, lines[i].x2);
swap(lines[i].y1, lines[i].y2);
}
}
cin >> K;

map<pair<int, int>, vector<Line>> stars;


for (int i = 0; i < N; ++i)
{
for (int j = i + 1; j < N; ++j)
{
pair<int, int> intersection;
if (intersects(lines[i], lines[j], intersection))
{
stars[intersection].push_back(lines[i]);
stars[intersection].push_back(lines[j]);
}
}
}

int placementlelo = 0;
for (auto& star : stars)
{
if (star.second.size() / 2 == K)
{
vector<int> intensities;
for (auto& line : star.second)
{
intensities.push_back(countCells(line, star.first, true));
}
placementlelo += *min_element(intensities.begin(), intensities.end());
}
}
cout << placementlelo << endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy