AtCoder Grand Contest 008 D - K-th K
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder Grand Contest 008 D - K-th K相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/def' title='def'>def
void tco name unsigned return stat str read棰樼洰浼犻€侀棬锛?a href="https://agc008.contest.atcoder.jp/tasks/agc008_d" class="uri">https://agc008.contest.atcoder.jp/tasks/agc008_d
棰樼洰澶ф剰锛?/strong>
缁欎綘涓€涓暱搴︿负(N)鐨勫簭鍒?span class="math inline">(A)
锛岃浣犳瀯閫犱竴涓暱搴︿负(N^2)鐨勫簭鍒?span class="math inline">(a)锛屾弧瓒?span class="math inline">(1sim N)閮藉嚭鐜颁簡(N)娆★紝涓斿浜庝换鎰?span class="math inline">(i)锛屾弧瓒?span class="math inline">(i)鍦?span class="math inline">(a)涓(i)娆″嚭鐜扮殑浣嶇疆涓?span class="math inline">(A_i)棣栧厛鎴戜滑鐭ラ亾瀵逛簬涓€涓暟(i)锛屽湪([1,A_i))涓嚭鐜颁簡(i-1)娆★紝鍦?span class="math inline">((A_i,N^2])
涓嚭鐜颁簡(N-i)娆★紙鏄剧劧锛?/p>鐒跺悗鑰冭檻浣嶇疆(i)搴旇濉粈涔堟暟锛岃偗瀹氭槸缁欓渶姹傛渶澶х殑瀵瑰惂锛熶篃灏辨槸鎸?span class="math inline">(A_i)
鎺掑簭鍚庢渶闈犲墠鐨勶紝涓斿嚭鐜版鏁拌繕娌¤揪鍒伴檺鍒剁殑鏁?闄愬埗鍗充负(i)鍦?span class="math inline">(A_i)鍓嶅彧鑳藉嚭鐜?span class="math inline">(i-1)娆?銆傜劧鍚庢垜浠鍙嶉兘鍋氫竴閬嶏紝鏈€鍚庡垽鏂暟鍒楁槸鍚﹀悎娉曞嵆鍙?/p>/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=5e2;
int cnt[N+10],A[N*N+10],C[N+10],B[N+10];
int pos[N+10][N+10];
bool cmp(int x,int y){return B[x]<B[y];}
bool check(int n){
for (int i=1;i<=n*n;i++) pos[A[i]][++pos[A[i]][0]]=i;
for (int i=1;i<=n;i++) if (pos[i][i]!=B[i]) return 0;
return 1;
}
int main(){
int n=read();
for (int i=1;i<=n;i++) C[i]=A[B[i]=read()]=i;
sort(C+1,C+1+n,cmp);
for (int i=1;i<=n;i++) cnt[i]=i-1;
for (int i=1;i<=n*n;i++){
if (A[i]) continue;
for (int j=1;j<=n;j++){
if (cnt[C[j]]){
cnt[A[i]=C[j]]--;
break;
}
}
}
for (int i=1;i<=n;i++) cnt[i]=n-i;
for (int i=n*n;i>=1;i--){
if (A[i]) continue;
for (int j=n;j>=1;j--){
if (cnt[C[j]]){
cnt[A[i]=C[j]]--;
break;
}
}
}
if (!check(n)){
printf("No
");
return 0;
}
printf("Yes
");
for (int i=1;i<=n*n;i++) printf("%d",A[i]),putchar(i==n*n?'
':' ');
return 0;
}
以上是关于AtCoder Grand Contest 008 D - K-th K的主要内容,如果未能解决你的问题,请参考以下文章
AtCoder Grand Contest 008 D - K-th K
markdown AtCoder Grand Contest 016