Skip to content

Commit 2e556db

Browse files
authored
Merge pull request #438 from Naman2251/main
Create day005sol01.cpp
2 parents 43b7d25 + 1238871 commit 2e556db

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Submission Link: https://codeforces.com/contest/2014/submission/356013624
2+
/*
3+
It is only not possible to call robinhood if n is 2 or 1.
4+
avg=(sum+k)/n.
5+
Since we need strictly more than half of the total population strictly less than half of the average wealth
6+
therefore we would just make our avg equal to a[n/2]+1 as then more than hal would be unhappy.
7+
(sum+k)/(n*2)=a[n/2]+1
8+
On solving, k=a[n/2]*n*2+1-sum
9+
If k<0 then no need of any coin so k=0, therefore final ans is max(k, 0).
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
using namespace std;
14+
15+
int main() {
16+
long long t;
17+
cin>>t;
18+
while(t--) {
19+
long long n, sum=0;
20+
cin>>n;
21+
vector <long long> a(n);
22+
for(int i=0; i<n; i++) {
23+
cin>>a[i];
24+
sum+=a[i];
25+
}
26+
if(n<=2) {
27+
cout<<-1<<endl;
28+
continue;
29+
}
30+
sort(a.begin(), a.end());
31+
long long k= a[n/2]*n*2+1-sum;
32+
cout<<max(k, 0LL)<<endl;
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Submission Link: https://cses.fi/problemset/result/15792090/
2+
3+
/*
4+
we are going through the array ans checking if the number right left to it is smaller to it then answer for that is (i-1) else we will check position ans[i-1]
5+
if we encounter ans[i]=-1 we know there is no number smaller than that so the ans is -1
6+
*/
7+
8+
#include<bits/stdc++.h>
9+
using namespace std;
10+
void solve(){
11+
int n;
12+
cin>>n;
13+
vector<int> vec(n);
14+
for(int i=0;i<n;i++){
15+
cin>>vec[i];
16+
}
17+
vector<int> ans(n);
18+
ans[0]=-1;
19+
for(int i=1;i<n;i++){
20+
if(vec[i]>vec[i-1]){
21+
ans[i]=i-1;
22+
}else{
23+
int a=ans[i-1];
24+
bool ok=true;
25+
while(a>-1){
26+
if(vec[a]<vec[i]){
27+
ans[i]=a;
28+
ok=false;
29+
break;
30+
}
31+
a=ans[a];
32+
}
33+
if(ok) {
34+
ans[i]=-1;
35+
}
36+
}
37+
}
38+
for(int i=0;i<n;i++){
39+
cout<<ans[i]+1<<" ";
40+
}
41+
}
42+
int main(){
43+
solve();
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Submission Link: https://cses.fi/problemset/result/15799460/
2+
3+
/*
4+
-> I made a vector which stores pair(pair(arrival, departue), person no.).
5+
-> I sorted so that person coming first gets room first.
6+
-> I made a priority queue min-heap(element with lower value have higher priority) and stored element as
7+
pair(departure, room no.)
8+
-> Now I compared each incoming element with the top one only as it has been emptied first. If a[i].first.first then remove
9+
first element and pushed the incoming element and in ans vector put the room else I would add no. of room and push the element
10+
in pq and in ans vector put the room. Now I printed rm(no. of room) and vector ans(in which room each person stays).
11+
*/
12+
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
int main() {
18+
int n;
19+
cin>>n;
20+
vector<pair<pair<int,int>,int>>a(n);
21+
for(int i=0;i<n;i++) {
22+
cin>>a[i].first.first;
23+
cin>>a[i].first.second;
24+
a[i].second=i;
25+
}
26+
sort(a.begin(),a.end());
27+
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
28+
int rm=1;
29+
pq.push({a[0].first.second,rm});
30+
vector <int> ans(n);
31+
ans[a[0].second]=1;
32+
for(int i=1;i<n;i++) {
33+
int d=pq.top().first;
34+
int r=pq.top().second;
35+
if(a[i].first.first>d) {
36+
pq.pop();
37+
pq.push({a[i].first.second,r});
38+
ans[a[i].second]=r;
39+
}
40+
else {
41+
rm++;
42+
pq.push({a[i].first.second,rm});
43+
ans[a[i].second]=rm;
44+
}
45+
}
46+
cout<<rm<<endl;
47+
for(int i=0;i<n;i++) cout<<ans[i]<<" ";
48+
cout<<endl;
49+
}

0 commit comments

Comments
 (0)