Offer harvesting _4

Source: Internet
Author: User

1. Water problem

2.BFS wide search (using priority queue Priority_queue)

4. Test instructions: give array A. Requires that the array be rearranged so that any adjacent two elements in the array are different. If there are multiple scenarios, select the scheme with the smallest dictionary order.

If the above requirements are not met, output "-1".

Idea: Use greedy tactics. Each time the remainder of the element reaches the lower limit of the number of elements that can be divided into the current num[i], the num[i] must be arranged at this time;

Otherwise, the smallest element is arranged. And differs from the previous element.

Use map to count the number of occurrences, use Set<pair, pair> to record <second in the map, first> <=> <cnt, num>.

#include <iostream>#include<algorithm>#include<cstdio>#include<string.h>#include<Set>#include<map>using namespacestd;Const intMAXN =100001;intN, A[maxn];map<int,int>CNT;Set<pair<int,int>>S;intMain () {CIN>>N;  for(inti =0; I < n; i + +) {scanf ("%d",&A[i]); Cnt[a[i]]++; } Map<int,int>:: Iterator it;  for(it = Cnt.begin (); It! = Cnt.end (); it + +) {S.insert (Make_pair (It->second, it->First )); }    if((--s.end ())->first *2-1>N) {cout<<"-1"<<Endl; return 0; }    intpre_x =-1;  for(inti =1; I <= N; i + +){        intx; if((--s.end ())->first *2-1= = (n + i-1) ) {x= (--s.end ())second; }Else{It=Cnt.begin ();  while(It->first = = Pre_x && It! =Cnt.end ()) {It++; } x= it->First ;        } s.erase (Make_pair (cnt[x], x)); CNT[X]--; if(Cnt[x] >0) {S.insert (Make_pair (cnt[x], x)); }Else{cnt.erase (x); } printf ("%d%c", x, i = = n?'\ n':' '); Pre_x=x; }    return 0;}

Offer harvesting _4

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.