题目大意:

给定n和n个数,每个数a[i]代表除了i外序列中颜色不同的数的个数,问能否构造出来这个数列。

比较简单,首先先求出来a数列的最大值Max,

如果有数小于Max-1,那么显然是不存在的

接下来就是有m个数等于Max-1,n-m个数等于Max

那么可以知道m个数中每个数肯定是有且只有一种颜色

所以m<Max,剩下的必须至少有2个,所以条件就是m<Max && m + 2*(Max-m) <= n

特殊情况:所有数都等于Max,这时候有2种情况,一种是每个数都是不同的,一种是2*Max <= n,判断一下就好

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
int n;
int a[];
map<int, int>M;
int main()
{
cin>>n;
for(int i = ; i < n; i++) cin>>a[i];
int Max = , t = ;
for(int i = ; i < n; i++){
if(!M[a[i]]) { M[a[i]] = ; t++; Max = max(Max, a[i]); }
}
for(int i = ; i < n; i++) if(a[i] < Max-) { cout<<"No"<<endl; return ; }
if(t > ) { cout<<"No"<<endl; }
else {
int m = ;
for(int i = ; i < n; i++) if(a[i] == Max-) m++;
if(m == ) {
if(*(Max-m) <= n) cout<<"Yes"<<endl;
else if(Max == n-) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return ;
}
if(m < Max && (m + *(Max-m) <= n)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}

最新文章

  1. WebApi接口 - 响应输出xml和json
  2. MongoDB学习笔记~根据子集合里某个属性排序
  3. [Scala] 快学Scala A2L2
  4. guava学习--File1
  5. strlen和mb_strlen的区别
  6. c缺陷与陷阱笔记-第四章 连接
  7. 如果iis的配置文件 applicationHost.config坏掉了, 会在 C:\inetpub\history\ 中存储历史备份。复制过去还原就可以了-摘自网络
  8. 解析嵌套json字符串,一个json字符串中嵌套另一个json字符串
  9. Google桌面搜索引擎
  10. BZOJ1976: [BeiJing2010组队]能量魔方 Cube
  11. Nginx 配置指令的执行顺序(五)
  12. Java JSON处理库Jackson
  13. Java动态绑定的内部实现机制
  14. 小白的Python之路 PEP8 代码风格
  15. 仿qq最新侧滑菜单
  16. springboot启动流程
  17. [Swift]LeetCode188. 买卖股票的最佳时机 IV | Best Time to Buy and Sell Stock IV
  18. Vertica系列:Vertica和Hadoop的互操作性
  19. mysql案例~mysql主从复制延迟概总
  20. 修改Devexpress DateEdit控件默认的日期格式和日历风格

热门文章

  1. linux系统基础之---RPM管理(基于centos7.4)
  2. js文件处理File
  3. Windows下安装最新版的MongoDB
  4. 解决每次运行Xcode,都需要输入密码的问题
  5. 什么是token及怎样生成token
  6. maven-聚合与继承
  7. 640. Solve the Equation
  8. 【转】Git远程操作详解
  9. OpenCV代码提取:flip函数的实现
  10. PowerMock简单使用