Songwriter
时间限制: 1 Sec 内存限制: 256 MB
题目描述
Andi is a mathematician, a computer scientist, and a songwriter. After spending so much time writing songs, he finally writes a catchy melody that he thought as his best creation. However, the singer who will sing the song/melody has a unique vocal range, thus, an adjustment may be needed.
A melody is defined as a sequence of
N
N
N notes which are represented by integers. Let A be the original melody written by Andi. Andi needs to adjust
A
A
A into a new melody
B
B
B such that for every
i
i
i where
1
≤
i
<
N
1≤i<N
1≤i<N:
·If
A
i
<
A
i
+
1
A_i<A_{i+1}
Ai<Ai+1, then
B
i
<
B
i
+
1
B_i<B_{i+1}
Bi<Bi+1.
·If
A
i
=
A
i
+
1
A_i=A{i+1}
Ai=Ai+1, then
B
i
=
B
i
+
1
B_i=B_{i+1}
Bi=Bi+1.
·If
A
i
>
A
i
+
1
A_i>A_{i+1}
Ai>Ai+1, then
B
i
>
B
i
+
1
B_i>B_{i+1}
Bi>Bi+1.
·
∣
B
i
−
B
i
+
1
∣
≤
K
|B_i−B_{i+1}|≤K
∣Bi−Bi+1∣≤K, i.e. the difference between two successive notes is no larger than K.
Moreover, the singer also requires that all notes are within her vocal range, i.e.
L
≤
B
i
≤
R
L≤B_i≤R
L≤Bi≤R for all
1
≤
i
≤
N
1≤i≤N
1≤i≤N.
Help Andi to determine whether such
B
B
B exists, and find the lexicographically smallest
B
B
B if it exists.
A
A
A melody
X
X
X is lexicographically smaller than melody
Y
Y
Y if and only if there exists
j
(
1
≤
j
≤
N
)
j (1≤j≤N)
j(1≤j≤N) such that
X
i
=
Y
i
X_i=Y_i
Xi=Yi for all
i
<
j
i<j
i<j and
X
j
<
Y
j
X_j<Y_j
Xj<Yj.
For example, consider a melody A = 1 , 3 , 5 , 6 , 7 , 8 , 9 , 10 , 3 , 7 , 8 , 9 , 10 , 11 , 12 , 12 A={1,3,5,6,7,8,9,10,3,7,8,9,10,11,12,12} A=1,3,5,6,7,8,9,10,3,7,8,9,10,11,12,12 as shown in the following figure. The diagonal arrow up in the figure implies that A i < A i + 1 A_i<A_{i+1} Ai<Ai+1, the straight right arrow implies that A i = A i + 1 A_i=A_{i+1} Ai=Ai+1, and the diagonal arrow down implies that A i > A i + 1 A_i>A_{i+1} Ai>Ai+1.
Supposed we want to make a new melody with L = 1 , R = 8 L=1, R=8 L=1,R=8, and K = 6 K=6 K=6. The new melody B = 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 8 B={1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,8} B=1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,8 as shown in the figure satisfies all the requirements, and it is the lexicographically smallest possible.
输入
Input begins with a line containing four integers: N , L , R , K ( 1 ≤ N ≤ 100000 ; 1 ≤ L ≤ R ≤ 1 0 9 ; 1 ≤ K ≤ 1 0 9 ) N ,L, R, K (1≤N≤100000; 1≤L≤R≤10^9; 1≤K≤10^9) N,L,R,K(1≤N≤100000;1≤L≤R≤109;1≤K≤109) representing the number of notes in the melody, the vocal range ( L L L and R R R), and the maximum difference between two successive notes in the new melody, respectively. The next line contains N N N integers: A i ( 1 ≤ A i ≤ 1 0 9 ) A_i (1≤A_i≤10^9) Ai(1≤Ai≤109) representing the original melody.
输出
Output in a line
N
N
N integers (each separated by a single space) representing the lexicographically smallest melody satisfying all the requirements, or output -1
if there is no melody satisfying all the requirements. Note that it might be possible that the lexicographically smallest melody which satisfies all the requirements to be the same as the original melody.
样例输入
【样例1】
16 1 8 6
1 3 5 6 7 8 9 10 3 7 8 9 10 11 12 12
【样例2】
16 1 8 6
1 3 5 6 7 8 9 10 3 7 8 9 10 11 12 13
【样例3】
16 1 10 10
1 3 5 6 7 8 9 10 3 7 8 9 1 11 12 13
样例输出
【样例1】
1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 8
【样例2】
-1
【样例3】
1 2 3 4 5 6 7 8 1 2 3 4 1 2 3 4
/*** Amber ***/
//#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
#define ls (rt<<1)
#define rs (rt<<1|1)
typedef long long ll;
typedef pair<int,int> pii;
template <typename T>
inline void read(T &x) {
x = 0;
static int p;
p = 1;
static char c;
c = getchar();
while (!isdigit(c)) {
if (c == '-')p = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c - 48);
c = getchar();
}
x *= p;
}
template <typename T>
inline void print(T x) {
if (x<0) {
x = -x;
putchar('-');
}
static int cnt;
static int a[50];
cnt = 0;
do {
a[++cnt] = x % 10;
x /= 10;
} while (x);
for (int i = cnt; i >= 1; i--)putchar(a[i] + '0');
puts("");
}
const double Pi=acos(-1);
const double eps=1e-6;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e6+10;
int n,l,r,k;
int a[maxn],up[maxn],down[maxn];
inline void work() {
read(n);
read(l);
read(r);
read(k);
for (int i = 1; i <= n; i++) {
read(a[i]);
}
up[n] = r;
down[n] = l;
for (int i = n - 1; i >= 1; i--) {
if (a[i] == a[i + 1]) {
down[i] = down[i + 1];
up[i] = up[i + 1];
} else if (a[i] < a[i + 1]) {
up[i] = up[i + 1] - 1;
down[i] = max(l, down[i + 1] - k);
} else {
down[i] = down[i + 1] + 1;
up[i] = min(r, up[i + 1] + k);
}
if (up[i] < down[i]) {
puts("-1");
return;
}
}
int now = down[1];
printf("%d ", now);
for (int i = 2; i <= n; i++) {
if (a[i] == a[i - 1]) {
} else if (a[i - 1] < a[i]) {
now = max(down[i], now + 1);
} else {
now = max(down[i], now - k);
}
printf("%d ", now);
}
}
int main() {
//freopen("1.txt","r",stdin);
int T = 1;
//read(T);
while (T--) {
work();
}
return 0;
}