Skip to content

Make the code Easy #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions 2020/Div 3/661 Div 3/Programs/Gifts Fixing.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
#include <iostream>
#include <vector>
#include <algorithm>

#define all(v) (v).begin(), (v).end()
#include<bits/stdc++.h>
#include<set>
#define lli long long int
#define pb push_back
#define cio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define rep(i,a,n) for(i=a;i<n;i++)
#define all(v) (v.begin(),v.end())
#define ppb pop_back
#define vsz v.size()
#define pi 3.141592653589793238
using namespace std;

void solve()
{
int no_of_elements;
cin >> no_of_elements;

vector <int> A(no_of_elements + 1);
for(int i = 1; i <= no_of_elements; i++)
{
cin >> A[i];
}

vector <int> B(no_of_elements + 1);
for(int i = 1; i <= no_of_elements; i++)
{
cin >> B[i];
}

const int oo = 1e9 + 9;
int target_A = oo, target_B = oo;

for(int i = 1; i <= no_of_elements; i++)
{
target_A = min(target_A, A[i]);
target_B = min(target_B, B[i]);
}

long long no_of_moves = 0;
for(int i = 1; i <= no_of_elements; i++)
{
long long a_moves_here = A[i] - target_A, b_moves_here = B[i] - target_B;

no_of_moves += max(a_moves_here, b_moves_here);
}

cout << no_of_moves << "\n";
}

int main()
{
int no_of_test_cases;
cin >> no_of_test_cases;

while(no_of_test_cases--)
solve();

return 0;
cio;
lli t,n,i,ma,mb,k,ans=0;
cin>>t;
while(t--){
cin>>n;
ma=1e18;
mb=1e18;
ans=0;
lli a[n],b[n];
for(i=0;i<n;i++){
cin>>a[i];
ma=min(ma,a[i]);
}
for(i=0;i<n;i++){
cin>>b[i];
mb=min(mb,b[i]);
}
for(i=0;i<n;i++){
while(a[i]!=ma||b[i]!=mb){
if(a[i]>ma&&b[i]>mb){
k=min(a[i]-ma,b[i]-mb);
ans+=k;
a[i]=a[i]-k;
b[i]=b[i]-k;
}
else if(a[i]>ma){
k=a[i]-ma;
ans+=k;
a[i]=ma;
}
else if(b[i]>mb){
k=b[i]-mb;
ans+=k;
b[i]=mb;
}
}
}
cout<<ans<<endl;
}
return 0;
}
75 changes: 30 additions & 45 deletions Contests/Div 2 600/Programs/Sweets Eating.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,38 @@
#include <iostream>
#include <vector>
#include <algorithm>

#define all(v) (v).begin(), (v).end()
using namespace std;

int main()
{
int no_of_elements, max_per_day;
cin >> no_of_elements >> max_per_day;

vector <int> A(no_of_elements + 1, 0);
for(int i = 1; i <= no_of_elements; i++)
{
cin >> A[i];
}

sort(all(A));

vector <long long> sum_till(no_of_elements + 1, 0);
for(int i = 1; i <= no_of_elements; i++)
{
sum_till[i] = sum_till[i - 1] + A[i];
}

vector <long long> penalty(no_of_elements + 1, 0);
for(int i = 1; i <= no_of_elements; i++)
{
long long day_1 = 0, remaining_days = 0;

if(i <= max_per_day)
{
day_1 = sum_till[i];
}
else
{
day_1 = sum_till[i] - sum_till[i - max_per_day];

remaining_days = penalty[i - max_per_day] + sum_till[i - max_per_day];
}

penalty[i] = day_1 + remaining_days;
}

for(int i = 1; i <= no_of_elements; i++)
{
cout << penalty[i] << " ";
}

return 0;
ios::sync_with_stdio(false);
cin.tie(0);

int nbSweets, maxPerDay;
cin >> nbSweets >> maxPerDay;

vector<int> val(nbSweets);

for (int iSweet = 0; iSweet < nbSweets; ++iSweet) {
cin >> val[iSweet];
}

sort(val.begin(), val.end());

vector<long long> ans(nbSweets);

long long curSum = 0;

for (int lastTaken = 0; lastTaken < nbSweets; ++lastTaken) {
curSum += val[lastTaken];
ans[lastTaken] = curSum;

if (lastTaken >= maxPerDay) {
ans[lastTaken] += ans[lastTaken - maxPerDay];
}

cout << ans[lastTaken] << (lastTaken == nbSweets-1 ? '\n' : ' ');
}

return 0;
}