0% found this document useful (0 votes)
117 views13 pages

Codechef A3-1

The document is an assignment submission by Dipanshu Verma that includes their name, UID, branch, semester, and code submissions for various coding problems/assignments. The coding problems include PSORT3 to sort pairs based on a condition, FRTIME to calculate free time between schedules, SMPAIR to sum the first two sorted elements of an array, and others.

Uploaded by

Saket Nisan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views13 pages

Codechef A3-1

The document is an assignment submission by Dipanshu Verma that includes their name, UID, branch, semester, and code submissions for various coding problems/assignments. The coding problems include PSORT3 to sort pairs based on a condition, FRTIME to calculate free time between schedules, SMPAIR to sum the first two sorted elements of an array, and others.

Uploaded by

Saket Nisan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ASSIGNMENT-1

NAME-DIPANSHU VERMA SECTION-51


UID-20BCS7660 GROUP-A
BRANCH-CSE SEM-3RD

ASSIGNMENT:

https://www.codechef.com/CUPA2101

PSORT3:

#include <iostream>

#include<utility>

#include<algorithm>

#include<vector>

using namespace std;

#define ull unsigned long long int

bool conditionSort(const pair<ull, ull> &x, const pair<ull, ull> &y)

if (x.second == y.second)

return (x.first < y.first);

else

return (x.second > y.second);

int main()

{
ull size, i, j;

cin >> size;

vector<pair<ull, ull>> vect;

ull a[size], b[size];

for (i = 0; i < size; i++)

cin >> a[i];

for (i = 0; i < size; i++)

cin >> b[i];

for (i = 0; i < size; i++)

vect.push_back(make_pair(a[i], b[i]));

sort(vect.begin(), vect.end(), conditionSort);

for (i = 0; i < size; i++)

cout << vect[i].first << " " << vect[i].second << " ";

FRTIME:
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main()

ios_base::sync_with_stdio(false);

cin.tie(NULL);

ll n, m, i;

cin >> n >> m;

array<ll, 2> alice[n], bob[m];

for (i = 0; i < n; i++)

cin >> alice[i][0] >> alice[i][1];

for (i = 0; i < m; i++)

cin >> bob[i][0] >> bob[i][1];

sort(alice, alice + n);

sort(bob, bob + m);

ll alice_pointer = 0, bob_pointer = 0;

ll freetime = 0;

while ((alice_pointer < n) && (bob_pointer < m))

int tempa, tempb;


tempa = max(alice[alice_pointer][0], bob[bob_pointer][0]);

tempb = min(bob[bob_pointer][1], alice[alice_pointer][1]);

if (tempb > tempa)

freetime = freetime + tempb - tempa;

if (alice[alice_pointer][1] > bob[bob_pointer][1])

bob_pointer += 1;

else

alice_pointer += 1;

cout << freetime;

return 0;

PRACTICE QUES:

https://www.codechef.com/CUPP2101

SMPAIR:

#include <iostream>

#include<algorithm>

using namespace std;

int main() {
// your code goes here

int t;

cin>>t;

while(t--)

int size;

cin>>size;

int* arr = new int[size];

for(int i=0; i<size; i++)

cin>>arr[i];

sort(arr,arr+size);

cout<<arr[0] + arr[1]<<endl;

delete []arr;

return 0;

QUELPREL:

#include<bits/stdc++.h>

using namespace std;


int main()

int t;

cin>>t;

vector<int> res;

for(int m=0;m<t;m++)

int n,k;

cin>>n>>k;

vector<int> arr(n); // Using vector to use reverse sort

for(int i=0;i<n;i++)

int x;

cin>>x;

arr.push_back(x);

sort(arr.rbegin(),arr.rend());

// Gives Descending ordered vector

int val = arr[k-1];

int j=0;

while(arr[j]>=val)

j++;

res.push_back(j);
}

for(int i=0;i<t;i++)

cout<<res[i]<<endl;

BOOKCHEF:

#include<iostream>

#include<algorithm>

#include<string.h>

#include<cmath>

#include<set>

#define ll long long

using namespace std;

//long a[1000100],b[1000100];

int main(){

ll t,n,k,i,mid,max,m,j;

struct friends{

ll f,p;

string s;

bool bo;

};
cin >> n >> m;

ll b[n];

for(i = 0;i<n;i++){

cin >> b[i];

friends a[m+1];

for(i = 1;i<=m;i++){

cin >> a[i].f >> a[i].p >> a[i].s;

a[i].bo = false;

for(i = 0;i<n;i++){

for(j = 1;j<=m;j++){

if(a[j].f == b[i]) a[j].bo = true;

ll c[m+1];

for(i = 1;i<=m;i++){

c[i] = a[i].p;

sort(c+1,c+m+1,greater<ll>());

for(i = 1;i<=m;i++){

for(j = 1;j<=m;j++){

if(c[i] == a[j].p and a[j].bo == true){

cout << a[j].s << endl;

break;
}

for(i = 1;i<=m;i++){

for(j = 1;j<=m;j++){

if(c[i] == a[j].p and a[j].bo == false){

cout << a[j].s << endl;

break;

TOTCRT:

#include<bits/stdc++.h>

using namespace std;

int main()

long int t;

cin>>t;

while (t--)

unordered_map<string,int> mp;
int n;

cin>>n;

for (int i = 0; i < n*3; i++)

string s;

int temp;

cin>>s>>temp;

if (mp.find(s)!=mp.end())

mp[s]+=temp;

else

mp.insert({s,temp});

vector<int>v;

for (auto i: mp)

v.push_back(i.second);

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

for (int i = 0; i < v.size(); i++)

cout<<v[i]<<" ";
}

cout<<endl;

return 0;

PTMSSNG:

#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int main()

int t;

long int i,j,xans,yans,input1,input2,n;

vector<long int> x;

vector<long int> y;

scanf("%d",&t);

for(i=0;i<t;i++)

scanf("%ld",&n);

for(j=0;j<(4*n)-1;j++)

scanf("%ld",&input1);
scanf("%ld",&input2);

x.push_back(input1);

y.push_back(input2);

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

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

for(j=0;j<=(4*n)-2;j=j+2)

if(x[j]!=x[j+1])

xans=x[j];

break;

for(j=0;j<=(4*n)-2;j=j+2)

if(y[j]!=y[j+1])

yans=y[j];

break;

printf("%ld %ld\n",xans,yans);
x.clear();

y.clear();

You might also like