Link to Problem:-https://cses.fi/problemset/task/1164
Code
can Someone please help me with my code..it is not working for some test cases..i have tried many test cases but could'nt find out my mistake
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Link to Problem:-https://cses.fi/problemset/task/1164
using namespace std;
bool compareBy(const pair<ll,pair<ll,int> >&a,const pair<ll ,pair<ll,int> >&b){ if(a.second.first!=b.second.first) return a.second.first<b.second.first;
return a.first<b.first;
} void solve(){ ll int n; cin>>n; vector<pair<ll,pair<ll,int> > >a;
for(int i=0;i<n;i++){ ll x,y; cin>>x>>y; a.push_back({x,{y,i}}); }
sort(a.begin(),a.end(),compareBy);
int res[n]={},valAssigned=1;
res[a[0].second.second]=valAssigned;
priority_queue<pair<int,int> ,vector<pair<int,int> >,greater<pair<int,int> > >pq;
pq.push({a[0].second.first,1}); //storing the finish time of room with the room number
for(int i=1;i<n;i++){
if(a[i].first>pq.top().first){
auto temp=pq.top().second;
pq.pop();
res[a[i].second.second]=temp;
pq.push({a[i].second.first,temp});
}
else{
res[a[i].second.second]=++valAssigned;
pq.push({a[i].second.first,valAssigned});
}
}
cout<<valAssigned<<"\n";
for(int i=0;i<n;i++)
cout<<res[i]<<" ";
cout<<endl;
}
int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; // cin>>t; while(t--){ solve(); } }
can Someone please help me with my code..it is not working for some test cases..i have tried many test cases but could'nt find out my mistake
Name |
---|
Can you help me with this? Its showing TLE but time complexity is nlogn.
include<bits/stdc++.h>
using namespace std; const long long M=1e9+7; const int N=5e4+10;
int main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); int t=1; // cin>>t; while(t--){ long long n; cin>>n; vector<pair<pair<int, int>, int>> v(n); for(int i=0; i<n; i++){ cin>>v[i].first.first>>v[i].first.second; v[i].second=i; } sort(v.begin(), v.end()); vector ans(n); set<pair<int, int>> a; a.insert({v[0].first.second, 0}); ans[v[0].second]=0; for(int i=1; i<n; i++){ int z=v[i].first.first; pair<int, int> p={z, -1}; auto it=lower_bound(a.begin(), a.end(), p); if(it==a.begin()){ a.insert({v[i].first.second, a.size()}); ans[v[i].second]=a.size()-1; } else{ it--; pair<int, int> p=*(it); ans[v[i].second]=p.second; a.erase(it); a.insert({v[i].first.second,p.second}); } } cout<<a.size()<<endl; for(int i=0; i<n; i++){ cout<<ans[i]+1<<" "; } cout<<endl; }
}