Skip to content

Instantly share code, notes, and snippets.

@bunnykek
Created October 18, 2023 15:20
Show Gist options
  • Save bunnykek/3e6d66f7682364050ac461b4ccd4edc5 to your computer and use it in GitHub Desktop.
Save bunnykek/3e6d66f7682364050ac461b4ccd4edc5 to your computer and use it in GitHub Desktop.
Moving Rabbits
Problem Description
There are N rabbits standing on an infinite number line. Every rabbit has some specific charcateristic represented by lowercase latin letters given by string A
Initially they are at some integer position By They will start moving when you order them with 1 unit per second Their initial direction will be given by an integer array C, which contains only 1 or-1 I mean that rabbit will move in positive direction or right side of the number line, and -1 mean opposite direction. If two rabbits with same characteristic value meet they will change their direction of movement. Find the sum of distance of all the the possible pair of rabbits after D seconds when you ordered them to move.
Problem Constraints
1N105
A= {lowercase Latin apihabets) -10% B, 10
C=(1,-1) 0 <= D <= 10
Input Format
Second argument is an integer array B.
First argument is a string A. Third argument is an integer array C Fourth argument is an integer D.
long solve(string &a, vector<int> &b, vector<int> &c, int &d){
long n = a.size();
vector<long> v(n);
for(long i=0; i<n; i++){
v[i] = (long)b[i] + (long)d*c[i];
}
sort(v.begin(), v.end());
long ans = 0;
for(long i = 0; i < n; i++){
ans += i*v[i] - (n-1-i)*v[i];
}
return ans;
}
int main() {
string a = "cd";
vector<int> b = {1, 0};
vector<int> c = {1, -1};
int d = 2;
cout<<solve(a, b, c, d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment