Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://vinhdinhcoder.net/Problem/Details/5062
- #include <bits/stdc++.h>
- using namespace std;
- #define ms(s,n) memset(s,n,sizeof(s))
- #define all(a) a.begin(),a.end()
- #define present(t, x) (t.find(x) != t.end())
- #define sz(a) int((a).size())
- #define FOR(i, a, b) for (int i = (a); i < (b); ++i)
- #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
- #define pb push_back
- #define pf push_front
- #define fi first
- #define se second
- #define mp make_pair
- #define endl "\n"
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef pair<int,int> pi;
- typedef vector<int> vi;
- typedef vector<pi> vii;
- const int MOD = (int) 1e9+7;
- const int INF = (int) 1e9+2804;
- inline ll gcd(ll a,ll b){ll r;while(b){r=a%b;a=b;b=r;}return a;}
- inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
- bool cmp ( pair < ll , ll > a, pair <ll, ll > b ){
- return abs(a.first - a.second ) > abs ( b.first - b.second );
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- ll n;
- cin >> n;
- pair < ll, ll > a [n];
- ll dp[n];
- for (int i=0;i<n;i++){
- cin >> a[i].first >> a[i].second ;
- dp[i]=1;
- }
- sort ( a, a+n, cmp );
- // we use sort because we can pick any pair without caring about the initial order
- // bài toán này ta có thể chọn theo vị trí bất kì ( không phải đoạn con của mảng ban đầu ) do đó
- // ta có thể sắp xếp lại sao cho thuận tiện ( đưa về bài toán mảng con )
- // cout <<"____________"<<endl;
- // FOR(i,0,n) cout << a[i].first <<" "<<a[i].second <<endl;
- cout << endl;
- ll res=1;
- for (int i=1;i<n;i++){
- for (int j=0;j<i;j++){
- if ( a[i].first >= a[j].first && a[i].second <= a[j].second && dp[i] < dp[j]+1 ){
- dp[i]=dp[j]+1;
- }
- res = max( res, dp[i]);
- }
- }
- cout << res;
- return 0;
- }
Add Comment
Please, Sign In to add comment