CF 620C Pearls in a Row (greedy)

Source: Internet
Author: User

D. There is a string of numbers, to divide these numbers into successive segments, each of which must contain at least 2 identical numbers, how can the number of segments be divided by the maximum?

For example, 1 2 1 3 1 2 1

So the answer is

1 3
6 U

That is, the most points in 2 paragraphs, the first paragraph is 4~7, the second paragraph is a.

This is divided into 2 segments: 1 2 1,3 1 2 1

S. Very good a greedy question. At that time did not think very much, later looked after tourist code to know.

It can be proved that the properties of greedy selection and optimal substructure are satisfied.

The greedy strategy is to iterate backwards, selecting the minimum length of qualifying segments at a time.


#include <iostream>#include<stdio.h>#include<Set>using namespacestd;#defineMAXN 312345intA[MAXN];int_START[MAXN];int_END[MAXN];intMain () {intN; Set<int>existed; intCNT; intstart;  while(~SCANF ("%d",&N))        {existed.clear ();  for(intI=0; i<n;++i) {scanf ("%d",&A[i]); } CNT=0; Start=0;  for(intI=0; i<n;++i) {            if(Existed.find (a[i])! =Existed.end ()) {_start[cnt]=start; _END[CNT]=i; ++CNT;                Existed.clear (); Start=i+1; }            Else{Existed.insert (a[i]); }        }        if(cnt==0) {printf ("-1\n"); }        Else{_end[cnt-1]=n-1; printf ("%d\n", CNT);  for(intI=0; i<cnt;++i) {printf ("%d%d\n", _start[i]+1, _end[i]+1); }        }    }    return 0;}
View Code

CF 620C Pearls in a Row (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: 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.