@vijju123 this when I clicked on the link : https://discuss.codechef.com/questions/91459/explaniation-for-the-salary-editorial?page=1#91462 I am getting followin error, how can I access it will you please help on this
Fixed. Thanks! Apparently, some links to old discuss answers got broken because there is no page number system in new discuss.
Thank you
ya that is the shortest way to get equal sallary of each worker.
i am getting wrong answer if i am using maxW*N-SumW instead of the formula in the editorial . Can anyone explain where i am going wrong
#include<bits/stdc++.h>
using namespace std;
int main() {
int t,n,i,cnt;
cin>>t;
while(t--)
{ int max=INT_MIN;
//int min=INT_MAX;
cnt=0;
cin>>n;
int arr[n];
for(i=0;i<n;i++)
{cin>>arr[i];
if(arr[i]>max)
max=arr[i];
cnt+=arr[i];
}
//cout<<cnt-min*n<<endl;
cout<<max*n-cnt<<endl;
}
return 0;
}.
Hey i used same approach but i considered maxw so accordingly for a single element to reach maximum salary no of operations on particular element would have been maxw-w adding n times making it n*maxw-sumw but its giving mw wrong ans can some one help me with this ?
I tried executing Author’s solution but even it is giving TLE error. Also i tried others solution whose code have been accepted but it is not getting accepted.
no. the answer is 2 in this approach.
for 0 2 2
max = 2;
so for 0 we get sum = 2 and for 2 the difference is 0 so the final value of sum=2 not 4.
correct me if i am wrong.
I made mistake. yes, you are right the answer is 4.
My solution for SALARY:
cases = int(input())
for case in range(cases):
n = int(input())
arr = [int(i) for i in input().split()]
arr.sort()
m = min(arr)
count = [x-m for x in arr if x!=m]
print(sum(count))
So in particular the context of the problem flows around decreasing the salary by 1 wrt the minimum salary, rather then increasing by 1 : We don’t care about
eg : L = [2,4,6]
2 4 6 (min is 2)
2 3 6 --------> 1st operation
2 2 6
2 2 5
2 2 4
2 2 3
2 2 2 ----------> 6th operation
What are we doing here is simple : ( L[1]-min(L) + L[2]-min(L)) : (4-2 + 6-2) = 6
which is equal to the number of operations performed
hence you need to perform same in the code.
REFER TO MY SOLUTION : CodeChef: Practical coding for everyone
So in particular the context of the problem flows around decreasing the salary by 1 wrt the minimum salary, rather then increasing by 1 : We don’t care about
eg : L = [2,4,6]
2 4 6 (min is 2)
2 3 6 --------> 1st operation
2 2 6
2 2 5
2 2 4
2 2 3
2 2 2 ----------> 6th operation
What are we doing here is simple : ( L[1]-min(L) + L[2]-min(L)) : (4-2 + 6-2) = 6
which is equal to the number of operations performed
hence you need to perform same in the code.
REFER TO MY SOLUTION : CodeChef: Practical coding for everyone
The code is giving WA. Can anyone tell me what mistake did I make?
#include <bits/stdc++.h>
using namespace std;
int maxi(int arr[],int n)
{
int max=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
}
return max;
}
int mini(int arr[],int n)
{
int min=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i]<min)
min=arr[i];
}
return min;
}
int main() {
// your code goes here
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int var=mini(arr,n);
for(int j=0;j<n;j++)
{
int max1=maxi(arr,n);
int min1=mini(arr,n);
for(int i=0;i<n;i++)
{
if(arr[i]!=max1)
{
arr[i]=arr[i]+(max1-min1);
}
}
}
cout<<arr[0]-var<<endl;
}
return 0;
}
got it! Thanks a lot, I took 4 hours for this problem…my first two solutions got TLE…btw nice explanation!
ok just a relative concept…
got it…
thanks man
Consider test case:
3
0 2 2
The answer is 4 but your answer is 2.
steps:
- 1 3 2
- 2 3 3
- 3 3 4
- 4 4 4
You have to increase the salaries of n-1 workers . Its a requirement in the question.
can you tell me what’s wrong with my code?
#include<bits/stdc++.h>
using namespace std;
const int N=107;
int ar[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”,stdin);
freopen(“output.txt”,“w”,stdout);
#endif
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int x=1e4;
for(int i=1;i<=n;++i){
cin>>ar[i];
ar[i]+=ar[i-1];
if(ar[i]<x){
x=ar[i];
}
}
cout<<ar[n]-n*x<<endl;
}
}
Alternative solution:
desired salary = minimum of W + moves required
But since we increased each time by (N - 1),
desired * N = moves * (N - 1) + sum(W)
which can be expanded into
min(W) * N + moves * N = moves * (N - 1) + sum(W)
and finally clearing off (moves * N) from both sides and simplifying, we get
moves = sum(W) - min(W) * N