Đăng nhập để hỏi chi tiết


Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n,k;
cin>>n>>k;
vector<pair<ll,ll>>a={{0,0}};
for (int i=0;i<k;++i){
ll l,r;
cin >>l>>r;
if (l>r) swap(l,r);
a.push_back({l,r});
}
sort(a.begin(),a.end(),[](const pair<ll,ll>& x, const pair<ll,ll>& y) {
return x.second < y.second;
});
int res = 1;
ll x = a[1].second - 1;
//meothinhle
for (ll i = 2; i <= k; ++i){
if (a[i].first > x){
x = a[i].second - 1;
res++;
}
}
cout <<res+1;
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll nmax = 1000;
pair<ll, ll> a[nmax + 5];
ll n, k;
bool cmp (pair<ll, ll> x, pair<ll, ll> y)
{
return x . second < y . second;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (ll i = 1; i <= k; ++i)
{
cin >> a[i] . first >> a[i] . second;
if (a[i] . first > a[i] . second)
{
swap(a[i] . first, a[i] . second);
}
}
sort (a + 1, a + 1 + k, cmp);
ll ans = 1, p = a[1] . second - 1;
for (ll i = 2; i <= k; ++i)
{
if (a[i] . first > p)
{
ans++;
p = a[i] . second - 1;
}
}
cout << ans + 1;
return 0;
}Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin