K-th K
2024-10-08 19:39:17
题目描述
You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.
a is N2 in length, containing N copies of each of the integers 1, 2, …, N.
For each 1≤i≤N, the i-th occurrence of the integer i from the left in a is the xi-th element of a from the left.
Constraints
1≤N≤500
1≤xi≤N2
All xi are distinct.
输入
The input is given from Standard Input in the following format:
N
x1 x2 … xN
输出
If there does not exist an integer sequence a that satisfies all the conditions, print 'No'. If there does exist such an sequence a, print 'Yes'.
样例输入
3
1 5 9
样例输出
Yes
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int a[];
struct node
{
int num;
int x;
}s[];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int i,j,n;
cin>>n;
for(i=;i<=n;i++){
cin>>s[i].x;
s[i].num=i;
}
sort(s+,s+n+,cmp);
int now=;
for(i=;i<=n;i++){
a[s[i].x]=s[i].num;
for(j=;j<s[i].num;j++){
while(a[now]) now++;
a[now]=s[i].num;
}
if(now>s[i].x) {cout<<"No"<<endl;return ;}
}
for(i=;i<=n;i++){
for(j=;j<=n-s[i].num;j++){
while(a[now]) now++;
if(now<s[i].x) {cout<<"No"<<endl;return ;}
a[now]=s[i].num;
}
}
cout<<"Yes"<<endl;
return ;
}
For each 1≤i≤N, the i-th occurrence of the integer i from the left in a is the xi-th element of a from the left.
就这句话。。。仔细读
第i个i在下标为xi的地方。嗯
这是贪心,先放上这些数字应在的位置
再从小到大把i弄上去
排除不可能的解
最新文章
- 【Excel 4.0 函数】REGISTER
- swiper的使用
- Pro ASP.NET MVC 5 Framework.学习笔记.6.4.MVC的必备工具
- [安卓][转]Android eclipse中程序调试
- GBin1插件推荐之马可波罗(Marco Polo),jQuery的自动补齐插件 - Autocomplete Plugin
- 简单又强大的联发科手机PhilZ Touch Recovery安装器,详细教程 - 本文出自高州吧
- Eclipse中快捷键的使用
- (大数据工程师学习路径)第一步 Linux 基础入门----用户及文件权限管理
- 当Azure里的虚拟机网卡被禁用
- calling c++ from golang with swig--windows dll (三)
- java项目和java-web项目中文件和文件夹的含义
- Visual Studio2012 添加服务引用时,生成基于任务操作不可用原因
- Vue-admin工作整理(八): BUS | | 组件通信
- Julia 下载 安装 juno 开发环境搭建
- NLP总览
- Hadoop集群搭建笔记
- as和is,但is也有as所没有的功能[C#] --转载 甘木
- 清北冬令营入学测试[ABCDEF]
- libcurl库的编译
- mask layer的遮罩层