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