问题 C: K-th K
时间限制: 2 Sec 内存限制: 256 MB提交: 262 解决: 35
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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.
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
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
题意:给出n个数,分别代表第i个i的位置,问是否存在有i个i的数组a,满足这个位置关系。
题解:在第i个i前面必有(i-1)个i,后面必有(n-i)个i,先填位置最靠前的数字,所以按照每个数字的位置排序,若前面填不了这么多数字,或后面填不了,那么就输出No,全部数字填完则输出Yes。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
const int maxn=505;
int x[maxn],f[maxn],a[maxn*maxn+maxn];
int cmp(int p,int q)
{
return x[p]<x[q];
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x[i]);
f[i]=i;
}
sort(f+1,f+n+1,cmp);
int now=1;
for(int i=1;i<=n;i++)
{
int w=f[i];a[x[w]]=w;
for(int j=1;j<w;j++)
{
while(a[now]) now++;
a[now]=w;
}
if(now>x[w]){puts("No");return 0;}
}
for(int i=1;i<=n;i++)
{
int w=f[i];
for(int j=1;j<=n-w;j++)
{
while(a[now]) now++;
if(now<x[w]){puts("No");return 0;}
a[now]=w;
}
}
puts("Yes");
return 0;
}