0% found this document useful (0 votes)
4 views

code_1

Uploaded by

Anshul Somani
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)
4 views

code_1

Uploaded by

Anshul Somani
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/ 5

b

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int calculate_cost_and_hamming(const string& original, const string& rearranged,


int A, int B) {
int cost = 0;
int hamming_distance = 0;

// Calculate the cost of the rearranged string


for (size_t i = 0; i < original.size() - 1; i++) { // Use size_t instead of int
if (original[i] == '0' && original[i + 1] == '1') {
cost += A;
} else if (original[i] == '1' && original[i + 1] == '0') {
cost += B;
}

// Calculate Hamming distance


if (original[i] != rearranged[i]) {
hamming_distance++;
}
}

// Check the last character for Hamming distance


if (original[original.size() - 1] != rearranged[rearranged.size() - 1]) {
hamming_distance++;
}

return cost * 1000 + hamming_distance; // Return combined value for easier


comparison
}

void solve() {
int T;
cin >> T;

while (T--) {
string binary_string;
cin >> binary_string;

int A, B;
cin >> A >> B;

// Check if the binary string is valid


if (binary_string.find_first_not_of("01") != string::npos) {
cout << "INVALID" << endl;
continue;
}

int count_0 = count(binary_string.begin(), binary_string.end(), '0');


int count_1 = binary_string.size() - count_0;

// Create the two possible rearranged strings


string rearranged_1(count_0, '0');
rearranged_1.append(count_1, '1');

string rearranged_2(count_1, '1');


rearranged_2.append(count_0, '0');

// Calculate the cost and Hamming distance for both rearrangements


int result_1 = calculate_cost_and_hamming(binary_string, rearranged_1, A,
B);
int result_2 = calculate_cost_and_hamming(binary_string, rearranged_2, A,
B);

// Compare results to find the one with the minimum cost and Hamming
distance
if (result_1 < result_2) {
cout << result_1 % 1000 << endl;
} else {
cout << result_2 % 1000 << endl;
}
}
}

int main() {
solve();
return 0;
}

c
#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <iomanip>
#include <tuple>

using namespace std;

struct Connection {
int target;
double dx, dy;
};

pair<double, double> polarToCartesian(double dist, double angle) {


double radians = angle * M_PI / 180.0;
double x = dist * cos(radians);
double y = dist * sin(radians);
return {x, y};
}

double computeDistance(double x, double y) {


return sqrt(x * x + y * y);
}

double shortestPath(int start, int end, unordered_map<int, vector<Connection>>&


graph) {
priority_queue<tuple<double, int, double, double>, vector<tuple<double, int,
double, double>>, greater<>> pq;
pq.emplace(0, start, 0, 0);
unordered_map<int, bool> visited;

while (!pq.empty()) {
auto [currentDist, device, x, y] = pq.top();
pq.pop();

if (visited[device]) continue;
visited[device] = true;

if (device == end) return round(currentDist * 100) / 100.0;

for (auto& conn : graph[device]) {


if (visited[conn.target]) continue;

double newX = x + conn.dx;


double newY = y + conn.dy;
double dist = computeDistance(newX, newY);

pq.emplace(dist, conn.target, newX, newY);


}
}

return -1;
}

int main() {
int N;
cin >> N;

vector<pair<int, int>> devices(N);


for (int i = 0; i < N; ++i) {
int id, count;
char colon;
cin >> id >> colon >> count;
devices[i] = {id, count};
}

unordered_map<int, vector<Connection>> graph;


for (int i = 0; i < N; ++i) {
int device;
cin >> device;
int count = devices[i].second;

for (int j = 0; j < count; ++j) {


int target, dist, angle;
cin >> target >> dist >> angle;
auto [dx, dy] = polarToCartesian(dist, angle);
graph[device].push_back({target, dx, dy});
graph[target].push_back({device, -dx, -dy});
}
}

int start, end;


cin >> start >> end;

double result = shortestPath(start, end, graph);


cout << fixed << setprecision(2) << result << endl;

return 0;
}

d
#include <iostream>
#include <vector>
using namespace std;

void computeScore(int n, int m, const vector<vector<int>>& grid, int x, int y,


vector<vector<int>>& score) {
int currentValue = grid[x][y];

if (x + 1 < n && grid[x + 1][y] >= currentValue) {


score[x + 1][y]++;
computeScore(n, m, grid, x + 1, y, score);
}

if (y + 1 < m && grid[x][y + 1] >= currentValue) {


score[x][y + 1]++;
computeScore(n, m, grid, x, y + 1, score);
}
}

int main() {
int n, m;
cin >> n >> m;

vector<vector<int>> grid(n, vector<int>(m));


for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> grid[i][j];

int k;
cin >> k;

vector<vector<int>> score(n, vector<int>(m, 0));

for (int i = 0; i < n; ++i) {


for (int j = 0; j < m; ++j) {
computeScore(n, m, grid, i, j, score);
}
}

bool found = false;


for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (score[i][j] == k) {
cout << i << " " << j << endl;
found = true;
}
}
}

if (!found) {
cout << "NO" << endl;
}

return 0;
}
d
#include <iostream>
#include <vector>
using namespace std;

void computeScore(int n, int m, const vector<vector<int>>& grid, int x, int y,


vector<vector<int>>& score) {
int currentValue = grid[x][y];

if (x + 1 < n && grid[x + 1][y] >= currentValue) {


score[x + 1][y]++;
computeScore(n, m, grid, x + 1, y, score);
}

if (y + 1 < m && grid[x][y + 1] >= currentValue) {


score[x][y + 1]++;
computeScore(n, m, grid, x, y + 1, score);
}
}

int main() {
int n, m;
cin >> n >> m;

vector<vector<int>> grid(n, vector<int>(m));


for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> grid[i][j];

int k;
cin >> k;

vector<vector<int>> score(n, vector<int>(m, 0));

for (int i = 0; i < n; ++i) {


for (int j = 0; j < m; ++j) {
computeScore(n, m, grid, i, j, score);
}
}

bool found = false;


for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (score[i][j] == k) {
cout << i << " " << j << endl;
found = true;
}
}
}

if (!found) {
cout << "NO" << endl;
}

return 0;
}

You might also like