Uva-1617-laptop

Source: Internet
Author: User


or the greedy method. To sort the original data, the rules are sorted by the right endpoint first, the right endpoint is the same, and then the left endpoint is sorted. Then the leftmost side begins to traverse the line segment, taking the right end of the first segment, and judging if it is equal to the right end of the second segment, if it is equal, it can be shortened to two adjacent; If you do not want to wait, then determine whether the first right endpoint is less than the second left endpoint, if less than, there must be a gap Then the second right end and the third segment continue the same comparison; if it is greater than, then the second can be placed next to the first right, the equivalent of the first line segment amplification, here is the idea of greed.

PS: Just began to write CMP function accidentally wrote a quote, direct CE a.

AC Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring > #include <string> #include <sstream> #include <vector> #include <set> #include <map># Include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert > #include <cmath> #include <functional>using namespace Std;typedef pair<int, int> pair;int n;// Sort by the right endpoint from small to large and the right endpoint in the same case by the left endpoint, BOOL CMP (pair A, pair B) {if (A.second! = B.second) {return A.second < B.second;} return A.first < B.first;}  int main () {Ios::sync_with_stdio (false); int t;cin >> t;while (t--) {cin >> n;vector<pair> a;int x, y;for (int i = 0; i < n; i++) {cin >> x >> y; A.push_back (Pair (x, y));} Sort (A.begin (), A.end (), CMP);//greedy solution int ret = 1, right = a[0].second;for (int i = 1; i < n; i++) {if (right! = a[i].se COND) {if (right < A[i].first) {ret++;right = A[i].second;} else {right++;Here is greedy handling}}}cout << ret-1 << Endl;} return 0;}




Uva-1617-laptop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.