#include <bits/stdc++.h>
#define ll long long
using namespace std;
string mergePalindrome(string&a,string&b)
{
ll n=a.length();
ll m=b.length();
unordered_map<char,ll> m1,m2;
for(char it:a) m1[it]++;
for(char it:b) m2[it]++;
string mid="",ans="";
for (char i='a';i<='z';i++)
{
if(m1[i]%2 and m2[i]%2 and mid.length()<=1) mid=string(2,i);
if((m1[i]%2 or m2[i]%2) and mid.length()==0) mid=i;
ans+=string((m1[i]/2+m2[i]/2),i);
}
string tt=ans;
if (mid.length()==2)
{
tt+=mid[0];
ans+=mid[0];
sort(begin(ans),end(ans));
tt=ans+string(rbegin(ans),rend(ans));
}
else tt+=mid+string(rbegin(ans),rend(ans));
return tt;
}
signed main()
{
string s,t; cin>>s>>t;
cout<<solve(s,t);
return 0;
}
Merging palindromes โ
#define ll long long
using namespace std;
string mergePalindrome(string&a,string&b)
{
ll n=a.length();
ll m=b.length();
unordered_map<char,ll> m1,m2;
for(char it:a) m1[it]++;
for(char it:b) m2[it]++;
string mid="",ans="";
for (char i='a';i<='z';i++)
{
if(m1[i]%2 and m2[i]%2 and mid.length()<=1) mid=string(2,i);
if((m1[i]%2 or m2[i]%2) and mid.length()==0) mid=i;
ans+=string((m1[i]/2+m2[i]/2),i);
}
string tt=ans;
if (mid.length()==2)
{
tt+=mid[0];
ans+=mid[0];
sort(begin(ans),end(ans));
tt=ans+string(rbegin(ans),rend(ans));
}
else tt+=mid+string(rbegin(ans),rend(ans));
return tt;
}
signed main()
{
string s,t; cin>>s>>t;
cout<<solve(s,t);
return 0;
}
Merging palindromes โ
#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((lli)a.size())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long
typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;
struct Line {
int x1, y1, x2, y2;
bool vertical() const { return x1 == x2; }
bool horizontal() const { return y1 == y2; }
bool diagonal() const { return abs(x2 - x1) == abs(y2 - y1); }
};
int N, K;
vector<Line> lines;
map<pair<int, int>, vector<int>> ptsMap;
void add(const Line& line, int idx) {
int x1 = line.x1, y1 = line.y1;
int x2 = line.x2, y2 = line.y2;
if (line.vertical()) {
int yStart = min(y1, y2);
int yEnd = max(y1, y2);
for(int y = yStart; y <= yEnd; y++) {
ptsMap[{x1, y}].push_back(idx);
}
}
else if (line.horizontal()) {
int xStart = min(x1, x2);
int xEnd = max(x1, x2);
for(int x = xStart; x <= xEnd; x++) {
ptsMap[{x, y1}].push_back(idx);
}
}
else if (line.diagonal()) {
int steps = abs(x2 - x1);
int dx = (x2 - x1) / steps;
int dy = (y2 - y1) / steps;
for(int i = 0; i <= steps; i++) {
int x = x1 + i * dx;
int y = y1 + i * dy;
ptsMap[{x, y}].push_back(idx);
}
}
}
int ff(int x1, int y1, int x2, int y2) {
if(x1 == x2) return abs(y1 - y2);
if(y1 == y2) return abs(x1 - x2);
if(abs(x1 - x2) == abs(y1 - y2)) return abs(x1 - x2);
return 0;
}
int solve(const pair<int, int>& pt, const vector<int>& lst) {
vector<int> d;
for(auto lIdx : lst) {
const Line& ln = lines[lIdx];
bool oneSided = (pt.first == ln.x1 && pt.second == ln.y1) ||
(pt.first == ln.x2 && pt.second == ln.y2);
if(oneSided) {
int ex = (pt.first == ln.x1 && pt.second == ln.y1) ? ln.x2 : ln.x1;
int ey = (pt.first == ln.x1 && pt.second == ln.y1) ? ln.y2 : ln.y1;
d.push_back(ff(pt.first, pt.second, ex, ey));
}
else {
d.push_back(ff(pt.first, pt.second, ln.x1, ln.y1));
d.push_back(ff(pt.first, pt.second, ln.x2, ln.y2));
}
}
return d.empty() ? 0 : *min_element(d.begin(), d.end());
}
void solve() {
cin >> N;
lines.resize(N);
for(int i = 0; i < N; i++) {
cin >> lines[i].x1 >> lines[i].y1 >> lines[i].x2 >> lines[i].y2;
add(lines[i], i);
}
cin >> K;
int total = 0;
for(auto &[pt, lst] : ptsMap) {
if(sz(lst) == K) {
total += solve(pt, lst);
}
}
cout << total << "\n";
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
Magic Stars โ
TCS Codevita
Source : ishaan
using namespace std;
#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((lli)a.size())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long
typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;
struct Line {
int x1, y1, x2, y2;
bool vertical() const { return x1 == x2; }
bool horizontal() const { return y1 == y2; }
bool diagonal() const { return abs(x2 - x1) == abs(y2 - y1); }
};
int N, K;
vector<Line> lines;
map<pair<int, int>, vector<int>> ptsMap;
void add(const Line& line, int idx) {
int x1 = line.x1, y1 = line.y1;
int x2 = line.x2, y2 = line.y2;
if (line.vertical()) {
int yStart = min(y1, y2);
int yEnd = max(y1, y2);
for(int y = yStart; y <= yEnd; y++) {
ptsMap[{x1, y}].push_back(idx);
}
}
else if (line.horizontal()) {
int xStart = min(x1, x2);
int xEnd = max(x1, x2);
for(int x = xStart; x <= xEnd; x++) {
ptsMap[{x, y1}].push_back(idx);
}
}
else if (line.diagonal()) {
int steps = abs(x2 - x1);
int dx = (x2 - x1) / steps;
int dy = (y2 - y1) / steps;
for(int i = 0; i <= steps; i++) {
int x = x1 + i * dx;
int y = y1 + i * dy;
ptsMap[{x, y}].push_back(idx);
}
}
}
int ff(int x1, int y1, int x2, int y2) {
if(x1 == x2) return abs(y1 - y2);
if(y1 == y2) return abs(x1 - x2);
if(abs(x1 - x2) == abs(y1 - y2)) return abs(x1 - x2);
return 0;
}
int solve(const pair<int, int>& pt, const vector<int>& lst) {
vector<int> d;
for(auto lIdx : lst) {
const Line& ln = lines[lIdx];
bool oneSided = (pt.first == ln.x1 && pt.second == ln.y1) ||
(pt.first == ln.x2 && pt.second == ln.y2);
if(oneSided) {
int ex = (pt.first == ln.x1 && pt.second == ln.y1) ? ln.x2 : ln.x1;
int ey = (pt.first == ln.x1 && pt.second == ln.y1) ? ln.y2 : ln.y1;
d.push_back(ff(pt.first, pt.second, ex, ey));
}
else {
d.push_back(ff(pt.first, pt.second, ln.x1, ln.y1));
d.push_back(ff(pt.first, pt.second, ln.x2, ln.y2));
}
}
return d.empty() ? 0 : *min_element(d.begin(), d.end());
}
void solve() {
cin >> N;
lines.resize(N);
for(int i = 0; i < N; i++) {
cin >> lines[i].x1 >> lines[i].y1 >> lines[i].x2 >> lines[i].y2;
add(lines[i], i);
}
cin >> K;
int total = 0;
for(auto &[pt, lst] : ptsMap) {
if(sz(lst) == K) {
total += solve(pt, lst);
}
}
cout << total << "\n";
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
Magic Stars โ
TCS Codevita
Source : ishaan
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
\#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((int)a.size())
#define YES cout << "YES";
#define NO cout << "NO";
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long
typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;
struct game {
int start;
int end;
string type;
};
bool solve(vector<int>& dieRolls, unordered_map<int, int>& board, int finalPos) {
int position = 1;
for (int roll : dieRolls) {
if (position + roll <= 100) {
position += roll;
}
while (board.find(position) != board.end()) {
position = board[position];
}
}
if (board.find(position) != board.end()) {
return false;
}
return position == finalPos;
}
void solve()
{
int N;
cin >> N;
vector<game> snakesLadders;
unordered_map<int, int> board;
for (int i = 0; i < N; ++i) {
int start, end;
cin >> start >> end;
game sl;
sl.start = start;
sl.end = end;
if (start > end) {
sl.type = "Snake";
} else {
sl.type = "Ladder";
}
snakesLadders.push_back(sl);
board[start] = end;
}
vector<int> remainingInput;
int num;
while (cin >> num) {
remainingInput.push_back(num);
}
if (remainingInput.empty()) {
cout << "Not reachable";
return;
}
int finalPos = remainingInput.back();
remainingInput.pop_back();
vector<int> dieRolls = remainingInput;
if (solve(dieRolls, board, finalPos)) {
cout << "Not affected";
return;
}
for (size_t i = 0; i < snakesLadders.size(); ++i) {
game& sl = snakesLadders[i];
board.erase(sl.start);
int newStart = sl.end;
int newEnd = sl.start;
string newType = (sl.type == "Snake") ? "Ladder" : "Snake";
if (newStart == 1 || board.find(newStart) != board.end()) {
board[sl.start] = sl.end;
continue;
}
board[newStart] = newEnd;
if (solve(dieRolls, board, finalPos)) {
cout << newType << " " << newStart << " " << newEnd;
return ;
}
board.erase(newStart);
board[sl.start] = sl.end;
}
cout << "Not reachable";
return ;
}
int32_t main()
{
int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}
using namespace std;
#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((int)a.size())
#define YES cout << "YES";
#define NO cout << "NO";
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long
typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;
struct game {
int start;
int end;
string type;
};
bool solve(vector<int>& dieRolls, unordered_map<int, int>& board, int finalPos) {
int position = 1;
for (int roll : dieRolls) {
if (position + roll <= 100) {
position += roll;
}
while (board.find(position) != board.end()) {
position = board[position];
}
}
if (board.find(position) != board.end()) {
return false;
}
return position == finalPos;
}
void solve()
{
int N;
cin >> N;
vector<game> snakesLadders;
unordered_map<int, int> board;
for (int i = 0; i < N; ++i) {
int start, end;
cin >> start >> end;
game sl;
sl.start = start;
sl.end = end;
if (start > end) {
sl.type = "Snake";
} else {
sl.type = "Ladder";
}
snakesLadders.push_back(sl);
board[start] = end;
}
vector<int> remainingInput;
int num;
while (cin >> num) {
remainingInput.push_back(num);
}
if (remainingInput.empty()) {
cout << "Not reachable";
return;
}
int finalPos = remainingInput.back();
remainingInput.pop_back();
vector<int> dieRolls = remainingInput;
if (solve(dieRolls, board, finalPos)) {
cout << "Not affected";
return;
}
for (size_t i = 0; i < snakesLadders.size(); ++i) {
game& sl = snakesLadders[i];
board.erase(sl.start);
int newStart = sl.end;
int newEnd = sl.start;
string newType = (sl.type == "Snake") ? "Ladder" : "Snake";
if (newStart == 1 || board.find(newStart) != board.end()) {
board[sl.start] = sl.end;
continue;
}
board[newStart] = newEnd;
if (solve(dieRolls, board, finalPos)) {
cout << newType << " " << newStart << " " << newEnd;
return ;
}
board.erase(newStart);
board[sl.start] = sl.end;
}
cout << "Not reachable";
return ;
}
int32_t main()
{
int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}
from collections import defaultdict
import sys
def parse_recipes(n, recipes):
graph = defaultdict(list)
for recipe in recipes:
potion, ingredients = recipe.split('=')
graph[potion].append(ingredients.split('+'))
return graph
def min_orbs(target, graph, cache):
if target in cache:
return cache[target]
if target not in graph:
cache[target] = 0
return 0
min_cost = sys.maxsize
for recipe in graph[target]:
cost = len(recipe) - 1
for ingredient in recipe:
cost += min_orbs(ingredient, graph, cache)
min_cost = min(min_cost, cost)
cache[target] = min_cost
return min_cost
n = int(input())
recipes = [input().strip() for _ in range(n)]
target_potion = input().strip()
graph = parse_recipes(n, recipes)
cache = {}
result = min_orbs(target_potion, graph, cache)
print(result, end="")
Frenkitsen
TCS Codevita โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((int)a.size())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long
typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;
struct Flight {
string src;
string to;
int dt;
int at;
int cost;
};
int parse_time(const string& time_str) {
int hour = stoi(time_str.substr(0, 2));
int minute = stoi(time_str.substr(3, 2));
string am_pm = time_str.substr(5, 2);
if (am_pm == "Am") {
if (hour == 12) hour = 0;
} else if (am_pm == "Pm") {
if (hour != 12) hour += 12;
}
return hour * 60 + minute;
}
void solve()
{
int N;
cin >> N;
vector<Flight> fff(N);
unordered_map<string, vector<Flight>> mp;
for (int i = 0; i < N; ++i) {
string src, to, des, arrival_str;
int cost;
cin >> src >> to >> des >> arrival_str >> cost;
int dt = parse_time(des);
int at = parse_time(arrival_str);
Flight ff = {src, to, dt, at, cost};
fff[i] = ff;
mp[src].push_back(ff);
}
string src, des;
cin >> src >> des;
string edes, last;
cin >> edes >> last;
int earliest_dt = parse_time(edes);
int latest_at = parse_time(last);
typedef tuple<int, int, string> State;
priority_queue<State, vector<State>, greater<State>> pq;
for (const auto& ff : mp[src]) {
if (ff.dt >= earliest_dt) {
if (ff.at <= latest_at) {
pq.emplace(ff.cost, ff.at, ff.to);
}
}
}
unordered_map<string, int> ans;
unordered_map<string, int> at;
while (!pq.empty()) {
int cost_so_far, current_at;
string current_city;
tie(cost_so_far, current_at, current_city) = pq.top();
pq.pop();
if (ans.find(current_city) != ans.end()) {
if (cost_so_far > ans[current_city]) continue;
if (cost_so_far == ans[current_city] && current_at >= at[current_city]) continue;
}
ans[current_city] = cost_so_far;
at[current_city] = current_at;
if (current_city == des) {
cout << cost_so_far;
return ;
}
for (const auto& ff : mp[current_city]) {
if (ff.dt >= current_at) {
if (ff.dt >= earliest_dt && ff.at <= latest_at) {
int new_cost = cost_so_far + ff.cost;
pq.emplace(new_cost, ff.at, ff.to);
}
}
}
}
cout << "Impossible";
return ;
}
int32_t main()
{
int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}
Flight optimizationโ
using namespace std;
#define loop(i, a, n) for (lli i = (a); i < (n); ++i)
#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(a) ((int)a.size())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define fi first
#define si second
#define v(a) vector<int>(a)
#define vv(a) vector<vector<int>>(a)
#define present(c, x) ((c).find(x) != (c).end())
#define set_bits __builtin_popcountll
#define MOD 1000000007
#define int long long
typedef long long lli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
typedef unordered_map<int, int> umpi;
typedef map<int, int> mpi;
typedef vector<pii> vp;
typedef vector<lli> vll;
typedef vector<vll> vvll;
struct Flight {
string src;
string to;
int dt;
int at;
int cost;
};
int parse_time(const string& time_str) {
int hour = stoi(time_str.substr(0, 2));
int minute = stoi(time_str.substr(3, 2));
string am_pm = time_str.substr(5, 2);
if (am_pm == "Am") {
if (hour == 12) hour = 0;
} else if (am_pm == "Pm") {
if (hour != 12) hour += 12;
}
return hour * 60 + minute;
}
void solve()
{
int N;
cin >> N;
vector<Flight> fff(N);
unordered_map<string, vector<Flight>> mp;
for (int i = 0; i < N; ++i) {
string src, to, des, arrival_str;
int cost;
cin >> src >> to >> des >> arrival_str >> cost;
int dt = parse_time(des);
int at = parse_time(arrival_str);
Flight ff = {src, to, dt, at, cost};
fff[i] = ff;
mp[src].push_back(ff);
}
string src, des;
cin >> src >> des;
string edes, last;
cin >> edes >> last;
int earliest_dt = parse_time(edes);
int latest_at = parse_time(last);
typedef tuple<int, int, string> State;
priority_queue<State, vector<State>, greater<State>> pq;
for (const auto& ff : mp[src]) {
if (ff.dt >= earliest_dt) {
if (ff.at <= latest_at) {
pq.emplace(ff.cost, ff.at, ff.to);
}
}
}
unordered_map<string, int> ans;
unordered_map<string, int> at;
while (!pq.empty()) {
int cost_so_far, current_at;
string current_city;
tie(cost_so_far, current_at, current_city) = pq.top();
pq.pop();
if (ans.find(current_city) != ans.end()) {
if (cost_so_far > ans[current_city]) continue;
if (cost_so_far == ans[current_city] && current_at >= at[current_city]) continue;
}
ans[current_city] = cost_so_far;
at[current_city] = current_at;
if (current_city == des) {
cout << cost_so_far;
return ;
}
for (const auto& ff : mp[current_city]) {
if (ff.dt >= current_at) {
if (ff.dt >= earliest_dt && ff.at <= latest_at) {
int new_cost = cost_so_far + ff.cost;
pq.emplace(new_cost, ff.at, ff.to);
}
}
}
}
cout << "Impossible";
return ;
}
int32_t main()
{
int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}
Flight optimizationโ
def solve(skills, p):
n = len(skills)
if n < 2 or p < 1 or p > (n * (n-1)) // 2:
return []
skills.sort()
from heapq import heappush, heappop
inefficiencies = []
for i in range(n-1):
for j in range(i+1, n):
inefficiency = abs(skills[i] - skills[j])
heappush(inefficiencies, inefficiency)
result = []
for _ in range(p):
if inefficiencies:
result.append(heappop(inefficiencies))
return result
Amazon โ
n = len(skills)
if n < 2 or p < 1 or p > (n * (n-1)) // 2:
return []
skills.sort()
from heapq import heappush, heappop
inefficiencies = []
for i in range(n-1):
for j in range(i+1, n):
inefficiency = abs(skills[i] - skills[j])
heappush(inefficiencies, inefficiency)
result = []
for _ in range(p):
if inefficiencies:
result.append(heappop(inefficiencies))
return result
Amazon โ
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Company Name: Microsoft
Role: Research Science Intern
Batch eligible: 2024 and 2025 grads
Apply: https://jobs.careers.microsoft.com/global/en/job/1695728
Role: Research Science Intern
Batch eligible: 2024 and 2025 grads
Apply: https://jobs.careers.microsoft.com/global/en/job/1695728
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Company Name: S&P Global
Role: Software Engineer
Batch eligible: 2025 grads
Apply: https://spgi.wd5.myworkdayjobs.com/SPGI_Careers/job/Hyderabad-Telangana/Software-Engineer_309607-1
Best of luck!!
Role: Software Engineer
Batch eligible: 2025 grads
Apply: https://spgi.wd5.myworkdayjobs.com/SPGI_Careers/job/Hyderabad-Telangana/Software-Engineer_309607-1
Best of luck!!
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Good internship opportunity โ
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
๐Paytm Hiring Interns
Apply Link: https://jobs.lever.co/paytm/3b92e7ba-1ac9-4818-bee0-d8b53c47b197
Apply Link: https://jobs.lever.co/paytm/3b92e7ba-1ac9-4818-bee0-d8b53c47b197
long getMaxRequests(vector<int>& sc, vector<int>& ir, int k) {
int n = sc.size();
long total = 0;
vector<int> extra(n, 0);
for (int i = 0; i < n; i++) {
int h = min(sc[i], ir[i]);
total += h;
if (sc[i] < ir[i]) {
int dch = min(2 * sc[i], ir[i]);
extra[i] = dch - h;
}
}
sort(extra.rbegin(), extra.rend());
for (int i = 0; i < k; i++) {
total += extra[i];
}
return total;
}
Server Management โ
int n = sc.size();
long total = 0;
vector<int> extra(n, 0);
for (int i = 0; i < n; i++) {
int h = min(sc[i], ir[i]);
total += h;
if (sc[i] < ir[i]) {
int dch = min(2 * sc[i], ir[i]);
extra[i] = dch - h;
}
}
sort(extra.rbegin(), extra.rend());
for (int i = 0; i < k; i++) {
total += extra[i];
}
return total;
}
Server Management โ
def numPaths(warehouse):
paths = [[0]*len(warehouse[0]) for i in warehouse]
if warehouse[0][0] == 1:
paths[0][0] = 1
for i in range(1, len(warehouse)):
if warehouse[i][0] == 1:
paths[i][0] = paths[i-1][0]
for j in range(1, len(warehouse[0])):
if warehouse[0][j] == 1:
paths[0][j] = paths[0][j-1]
for i in range(1, len(warehouse)):
for j in range(1, len(warehouse[0])):
if warehouse[i][j] == 1:
paths[i][j] = paths[i-1][j] + paths[i][j-1]
return paths[-1][-1]%(10**9+7)
path in warehouse โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <bits/stdc++.h>
using namespace std;
long long dfs(long long src,long long par,vector<long long> adj[],vector<long long> &sum,vector<long long> &depth,vector<long long> &a,vector<long long> &c)
{
if(a[src]!=0)
{
depth[src]=0;
sum[src]=a[src];
return a[src];
}
long long maxi=0;
for(auto x:adj[src])
{
if(x!=par)
{
long long temp=dfs(x,src,adj,sum,depth,a,c);
sum[src]+=(temp-depth[x]);
depth[src]=depth[x]+1;
maxi=max(maxi,temp);
}
}
return (maxi + min(c[src],sum[src]-(maxi-depth[src])));
}
int main() {
long long n;
cin>>n;
vector<long long> a(n+1);
vector<long long> c(n+1);
for(long long i=0;i<n;i++)
cin>>a[i+1];
for(long long i=0;i<n;i++)
cin>>c[i+1];
vector<long long> adj[n+1];
for(long long i=1;i<n;i++)
{
long long u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<long long> sum(n+1,0);
vector<long long> depth(n+1);
cout<<dfs(1,0,adj,sum,depth,a,c);
return 0;
}
fruits on treeโ
Media. Net
using namespace std;
long long dfs(long long src,long long par,vector<long long> adj[],vector<long long> &sum,vector<long long> &depth,vector<long long> &a,vector<long long> &c)
{
if(a[src]!=0)
{
depth[src]=0;
sum[src]=a[src];
return a[src];
}
long long maxi=0;
for(auto x:adj[src])
{
if(x!=par)
{
long long temp=dfs(x,src,adj,sum,depth,a,c);
sum[src]+=(temp-depth[x]);
depth[src]=depth[x]+1;
maxi=max(maxi,temp);
}
}
return (maxi + min(c[src],sum[src]-(maxi-depth[src])));
}
int main() {
long long n;
cin>>n;
vector<long long> a(n+1);
vector<long long> c(n+1);
for(long long i=0;i<n;i++)
cin>>a[i+1];
for(long long i=0;i<n;i++)
cin>>c[i+1];
vector<long long> adj[n+1];
for(long long i=1;i<n;i++)
{
long long u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<long long> sum(n+1,0);
vector<long long> depth(n+1);
cout<<dfs(1,0,adj,sum,depth,a,c);
return 0;
}
fruits on treeโ
Media. Net
#include <bits/stdc++.h>omega prime
using namespace std;
#define ll long long
const ll M = 1e9+7 ;
int main() {
ll n;cin>>n ;
vector<ll> v(31) ;
for(ll i = 1;i<=n;i++){
ll x;cin>>x ;
v[x]++ ;
}
vector<ll> p ;
for(ll i = 2;i<=30;i++){
bool ch = 1 ;
for(ll j = 2;j*j<=i;j++){
if(i%(j*j)==0)ch = 0 ;
}
if(ch)p.push_back(i) ;
}
ll one = 1 ;
while(v[1]){
one = one*2%M ;
v[1]-- ;
}
n = p.size() ;
ll ans = 0 ;
for(ll i = 1;i<(1ll<<n);i++){
ll pro = 1 ;
ll tot = 1 ;
bool ch = 1 ;
for(ll j =0;j<n;j++){
if((1ll<<j)&i){
if(__gcd(pro,p[j])!=1){
ch = 0 ;
break;
}
pro = pro*p[j] ;
tot = tot*v[p[j]]%M ;
}
}
tot = tot*(one)%M ;
if(ch){
ans += tot ;
ans %= M ;
}
}
cout<<(ans+M)%M<<endl;
return 0;
}
}
media .netโ