1.水题

2.BFS宽搜(使用优先队列priority_queue)

4.题意:给数组a。要求重排列数组,使得数组中的任意相邻的两个元素不同。如果存在多个方案,那么选择字典序最小的方案。

    如果不能满足如上要求,输出“-1”。

思路:使用贪心策略。每次如果剩下的元素刚好达到可以分割当前Num[i]的数目下限个元素的时候,那么此时就必须安排num[i];

        否则,安排最小的元素。且和上一个元素不同。

使用map来统计数值出现的个数, 使用set<pair, pair>来记录map中的<second, first> <=> <cnt, num>。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <set>
#include <map> using namespace std; const int Maxn = ;
int n, a[Maxn];
map<int, int>cnt;
set<pair<int, int>>S; int main()
{
cin>>n;
for(int i = ; 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 * - > n){
cout<<"-1"<<endl;
return ;
}
int pre_x = -;
for(int i = ; i <= n; i ++){
int x;
if((--S.end())->first * - == (n + i - )){
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] > ){
S.insert(make_pair(cnt[x], x));
}else{
cnt.erase(x);
}
printf("%d%c",x, i == n?'\n':' ');
pre_x = x;
}
return ;
}

最新文章

  1. solr DIH 知识梳理
  2. Web Project犯错误!
  3. Map 对象
  4. SZU:J38 Number Base Conversion
  5. 使用java连接MySql,中文乱码解决的方法
  6. 面试题-Java基础-异常部分
  7. mac 终端简单指令
  8. 通过 dhcp-agent 访问 Metadata - 每天5分钟玩转 OpenStack(168)
  9. 为什么要学Python
  10. 20162330 第10周 MySort实验
  11. kubernetes入门(07)kubernetes的核心概念(4)
  12. linux网络配置+远程工具使用
  13. [国家集训队]Tree II
  14. Python2.7-xdrlib
  15. Win7无法添加用户的问题
  16. Jaccard similarity(杰卡德相似度)和Abundance correlation(丰度相关性)
  17. PHP curl_setopt函数用法介绍补充篇
  18. SpringBoot接口返回去掉空字段
  19. Docker(1)在CentOS上的安装与卸载
  20. pip和conda安装源更改

热门文章

  1. 恶补---bell数
  2. 关于 CMSIS 标准 及 STM32F10x的固件库
  3. Android ToggleButton:状态切换的Button
  4. Codeforces Round #232 (Div. 2) C
  5. hdu 2167 状态压缩dp
  6. 文化之旅 2012年NOIP全国联赛普及组
  7. 生产(production)
  8. 【Google Chrome】Google Chrome快捷键大全
  9. 学一学书里的django是怎么写views.py的
  10. 转载 字符串hash