This media is not supported in your browser
VIEW IN TELEGRAM
#include <bits/stdc++.h>
using namespace std;
int minimumCost(int N, vector<int> A, vector<vector<int>> Cost) {
int minCost = INT_MAX;
vector<int> perm(N);
iota(perm.begin(), perm.end(), 1);
do {
bool valid = true;
for (int i = 0; i < N; ++i) {
if (perm[i] > A[i]) {
valid = false;
break;
} else if (perm[i] < A[i]) {
break;
}
}
if (valid) {
int cost = 0;
for (int i = 0; i < N; ++i) {
cost += Cost[i][perm[i] - 1];
}
minCost = min(minCost, cost);
}
} while (next_permutation(perm.begin(), perm.end()));
return minCost;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<int>> Cost(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> Cost[i][j];
}
}
cout << minimumCost(N, A, Cost) << endl;
return 0;
}
Cheapest permutation ✅
using namespace std;
int minimumCost(int N, vector<int> A, vector<vector<int>> Cost) {
int minCost = INT_MAX;
vector<int> perm(N);
iota(perm.begin(), perm.end(), 1);
do {
bool valid = true;
for (int i = 0; i < N; ++i) {
if (perm[i] > A[i]) {
valid = false;
break;
} else if (perm[i] < A[i]) {
break;
}
}
if (valid) {
int cost = 0;
for (int i = 0; i < N; ++i) {
cost += Cost[i][perm[i] - 1];
}
minCost = min(minCost, cost);
}
} while (next_permutation(perm.begin(), perm.end()));
return minCost;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<int>> Cost(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> Cost[i][j];
}
}
cout << minimumCost(N, A, Cost) << endl;
return 0;
}
Cheapest permutation ✅
👍1
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i;
TreeMap<Long,Integer> map=new TreeMap<>();
long dp[]=new long[n];
long r[]=new long[n];
long h[]=new long[n];
long mod=(long)1e9+7;
for(i=0;i<n;i++) r[i]=sc.nextLong();
for(i=0;i<n;i++) h[i]=sc.nextLong();
for(i=0;i<n;i++){
long volume=(h[i]*r[i]*r[i])%mod;
Long low=map.floorKey(volume);
if(low!=null) dp[i]=(dp[i]+dp[map.get(low)]+volume)%mod;
else dp[i]=(dp[i]+volume)%mod;
map.put(volume,i);
}
long max=0;
for(long it:dp) max=Math.max(max,it);
System.out.println(max);
}
}
Increasing mex counting ✅
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i;
TreeMap<Long,Integer> map=new TreeMap<>();
long dp[]=new long[n];
long r[]=new long[n];
long h[]=new long[n];
long mod=(long)1e9+7;
for(i=0;i<n;i++) r[i]=sc.nextLong();
for(i=0;i<n;i++) h[i]=sc.nextLong();
for(i=0;i<n;i++){
long volume=(h[i]*r[i]*r[i])%mod;
Long low=map.floorKey(volume);
if(low!=null) dp[i]=(dp[i]+dp[map.get(low)]+volume)%mod;
else dp[i]=(dp[i]+volume)%mod;
map.put(volume,i);
}
long max=0;
for(long it:dp) max=Math.max(max,it);
System.out.println(max);
}
}
Increasing mex counting ✅
👍1
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
vector<int> A, P;
vector<vector<int>> tree;
vector<int> ranges;
long long result = 0;
void dfs(int node, int parent, set<int>& values) {
values.insert(A[node]);
set<int> child_values;
int num_ranges = 1, last = -1;
for (int val : values) {
if (last != -1 && val != last + 1) num_ranges++;
last = val;
}
ranges[node] = num_ranges;
for (int child : tree[node]) {
if (child == parent) continue;
child_values = values;
dfs(child, node, child_values);
}
result = (result + ranges[node]) % MOD;
}
int main() {
int N;
cin >> N;
A.resize(N);
P.resize(N);
tree.resize(N);
ranges.resize(N);
for (int i = 0; i < N; ++i) {
cin >> P[i];
if (i > 0) tree[P[i]].push_back(i);
}
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
set<int> values;
dfs(0, -1, values);
cout << result << endl;
return 0;
}
path coverage range ✅
👍3👌1
Coding | EXAMS | IBM ACCENTURE | VIRTUSA | IBM | AMAZON | TCS | EPAM | WILEY EDGE | TECH MAHINDRA | JPMORGAN | HCL | WIPRO
Photo
Colourd ball
Kth palindrome
String container
Play with stacks
Perfectly even
Divisible string
Kth palindrome
String container
Play with stacks
Perfectly even
Divisible string
Kth Palindrome Code
Infosys SP Exam
C++
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
int v(int k) {
return (1 << k) - 1;
}
bool p(int n) {
while (n % 2 == 0) n /= 2;
return n == 1;
}
long long s(int n) {
vector<long long> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int k = 1; v(k) <= i; ++k) {
int l = v(k - 1) - 1;
if (i >= 2 * l + 1) {
dp[i] = (dp[i] + dp[i - (2 * l + 1)] * k) % MOD;
}
}
}
return dp[n];
}
int main() {
int n;
cin >> n;
cout << s(n) << endl;
return 0;
}
Kth Palindrome Code
Infosys SP Exam
C++
Infosys SP Exam
C++
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
int v(int k) {
return (1 << k) - 1;
}
bool p(int n) {
while (n % 2 == 0) n /= 2;
return n == 1;
}
long long s(int n) {
vector<long long> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int k = 1; v(k) <= i; ++k) {
int l = v(k - 1) - 1;
if (i >= 2 * l + 1) {
dp[i] = (dp[i] + dp[i - (2 * l + 1)] * k) % MOD;
}
}
}
return dp[n];
}
int main() {
int n;
cin >> n;
cout << s(n) << endl;
return 0;
}
Kth Palindrome Code
Infosys SP Exam
C++
👍1
👍1
👍5
Any exam help available 🔥💯
Book your slot 👨💻
Contact here @ILOVEU_143❤️
100% clearance guarantee 🔥🔥
Note: it's paid help
💯 Clearance and genuine help😊✌️
Scroll up and check all proofs we helped😁👨💻
Share @Coding_000❤️
Book your slot 👨💻
Contact here @ILOVEU_143❤️
100% clearance guarantee 🔥🔥
Note: it's paid help
💯 Clearance and genuine help😊✌️
Scroll up and check all proofs we helped😁👨💻
Share @Coding_000❤️
👍3❤2🥰1