POJ 1716 Integer intervals# Greedy

Source: Internet
Author: User

(~ ̄▽ ̄) ~*

//ask for a set, the intersection of this set with any interval, at least two digits//greedy Process: According to the right value of n intervals from small to large to arrange the interval,//The collection first takes the right two digits of the first interval,//to the second interval, judging whether the number in the set is in the interval//if not, take the right from the second interval to the left (Cnt=0 takes the last two digits, Cnt=1 takes the last number)#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespacestd;structinterval{intL,r;} a[10005];BOOLcmp (interval a,interval b) {returna.r<B.R;}intMain () {intn,res=0; scanf ("%d",&N);  for(intI=0; i<n;i++) scanf ("%d%d",&a[i].l,&A[I].R); Sort (A,a+n,cmp); Vector<int>Set; Set.push_back (a[0].r-1); Set.push_back (a[0].R); Res=2;  for(intI=1; i<n;i++)    {        intCnt=0;  for(intj=0; J<set.size (); j + +)        {            if(set[j]>=a[i].l&&set[j]<=A[I].R) CNT++; }        if(cnt==0) {set.push_back (A[I].R-1);            Set.push_back (A[I].R); Res+=2; }        Else if(cnt==1) {set.push_back (A[I].R); Res++; }} printf ("%d\n", RES); return 0;}

POJ 1716 Integer intervals# greedy

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.