0% found this document useful (0 votes)
6 views3 pages

Array Code

The document contains multiple C++ code snippets that solve different problems. The first code calculates the total delivery delay based on desired and actual delivery times. The subsequent codes handle various tasks such as counting valid subsets, calculating distances in a graph, and checking if supplied resources meet required amounts.

Uploaded by

shamistha GUPTA
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)
6 views3 pages

Array Code

The document contains multiple C++ code snippets that solve different problems. The first code calculates the total delivery delay based on desired and actual delivery times. The subsequent codes handle various tasks such as counting valid subsets, calculating distances in a graph, and checking if supplied resources meet required amounts.

Uploaded by

shamistha GUPTA
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/ 3

#include <iostream>

using namespace std;


#include <vector>
#include <algorithm>
#include <cmath>

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

std::vector<int> desired(N);
for (int i = 0; i < N; ++i) {
std::cin >> desired[i];
}

// Sort desired times to match them with actual delivery times (1 to N)


std::sort(desired.begin(), desired.end());

int total_delay = 0;

// Greedily match desired times with actual delivery times


for (int i = 0; i < N; ++i) {
total_delay += std::abs(desired[i] - (i + 1));
}

std::cout << total_delay << std::endl;

return 0;
} code 2
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
A[i]--; // 0-based indexing
}
long long total = 0;
for (int start = 0; start < N; ++start) {
vector<int> dist(N, -1);
queue<int> q;
dist[start] = 0;
q.push(start);
while (!q.empty()) {
int u = q.front(); q.pop();
int v = A[u];
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
for (int i = 0; i < N; ++i) {
if (dist[i] > 0) total += dist[i];
}
}
cout << total << endl;
return 0;
}
code1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int N, L, R, X;
cin >> N >> L >> R >> X;
vector<int> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];

int count = 0;
// Enumerate all subsets
for (int mask = 0; mask < (1 << N); ++mask) {
int sum = 0, mn = 1e9, mx = 0, num = 0;
for (int i = 0; i < N; ++i) {
if (mask & (1 << i)) {
sum += A[i];
mn = min(mn, A[i]);
mx = max(mx, A[i]);
num++;
}
}
if (num >= 2 && sum >= L && sum <= R && mx - mn >= X)
count++;
}
cout << count << endl;
return 0;
}
code3
#include <iostream>
#include <vector>
using namespace std;

int main() {
int N, M;
cin >> N >> M;
vector<long long> required(M);
for(int i = 0; i < M; ++i) {
cin >> required[i];
}
vector<long long> supplied(M, 0);
for(int i = 0; i < N; ++i) {
for(int j = 0; j < M; ++j) {
long long x;
cin >> x;
supplied[j] += x;
}
}
bool ok = true;
for(int i = 0; i < M; ++i) {
if(supplied[i] < required[i]) {
ok = false;
break;
}
}
if(ok) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
code6

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