题目传送门:https://agc008.contest.atcoder.jp/tasks/agc008_d

题目大意:

给你一个长度为\(N\)的序列\(A\),请你构造一个长度为\(N^2\)的序列\(a\),满足\(1\sim N\)都出现了\(N\)次,且对于任意\(i\),满足\(i\)在\(a\)中第\(i\)次出现的位置为\(A_i\)


首先我们知道对于一个数\(i\),在\([1,A_i)\)中出现了\(i-1\)次,在\((A_i,N^2]\)中出现了\(N-i\)次(显然)

然后考虑位置\(i\)应该填什么数,肯定是给需求最大的对吧?也就是按\(A_i\)排序后最靠前的,且出现次数还没达到限制的数(限制即为\(i\)在\(A_i\)前只能出现\(i-1\)次)。然后我们正反都做一遍,最后判断数列是否合法即可

/*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\n");
return 0;
}
printf("Yes\n");
for (int i=1;i<=n*n;i++) printf("%d",A[i]),putchar(i==n*n?'\n':' ');
return 0;
}

AtCoder Grand Contest 008 D - K-th K的更多相关文章

  1. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

  2. Atcoder Grand Contest 008 E - Next or Nextnext(乱搞+找性质)

    Atcoder 题面传送门 & 洛谷题面传送门 震惊,我竟然能独立切掉 AGC E 难度的思维题! hb:nb tea 一道 感觉此题就是找性质,找性质,再找性质( 首先看到排列有关的问题,我 ...

  3. AtCoder Grand Contest 008题解

    传送门 \(A\) 分类讨论就行了 然而我竟然有一种讨论不动的感觉 int x,y; inline int min(R int x,R int y){return x<y?x:y;} inlin ...

  4. AtCoder Grand Contest 008 A

    Problem Statement Snuke has a calculator. It has a display and two buttons. Initially, the display s ...

  5. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  6. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  7. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  8. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

  9. AtCoder Grand Contest 006

    AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...

随机推荐

  1. pom.xml和testng.xml

    转自:http://www.cnblogs.com/penghong2014/p/4380199.html <project xmlns="http://maven.apache.or ...

  2. 有遍历struct中字段信息的函数或方法

    例:struct a{int a;char b[10];double c;}; 在程序中只知道一个结构 a 的指针, 有没有函数能通过结构的名字 和 指向结构的指针 随次得到 结构中的变量类型 和 变 ...

  3. 写入文本文件时“\n”不是回车换行而是个方块“■”的解决方法

    用“\n”写入文本文件时,打开文本文件显示的为什么不是回车换行而是个黑方块“■”,但用file()读取时还是认为是一行一行的? 首先在WINDOWS里回车换行是"\r\n"; 而L ...

  4. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  5. 简单的shell脚本编写

    http://www.cnblogs.com/wuyuegb2312/p/3399566.html

  6. OO的片段,继承与组合,继承的优点与目的,虚机制在构造函数中不工作

    摘自C++编程思想: ------------------------------ 继承与组合:接口的重用 ------------------------------- 继承和组合都允许由已存在的类 ...

  7. js中数组遍历的几种方法及其区别

    参考网站: http://www.cnblogs.com/lvmh/p/6104397.html 第一种最常用的:for循环 for(j = 0; j < arr.length; j++) { ...

  8. POJ1426 Find The Multiple —— BFS

    题目链接:http://poj.org/problem?id=1426 Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  9. Java经典算法大全

    1.河内之塔.. 2.Algorithm Gossip: 费式数列. 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官(一) 6. ...

  10. 基于阿里云上实现全站https的正确姿势(一)

    对应的网址:https://yq.aliyun.com/articles/65199 摘要: 目前主流大厂的网站和服务都已经实现了全站https, 例如: baidu, taobao, jd等. 关于 ...