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
2
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.
C.
#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)