牛客多校6 A
Array
题目链接
题目大意
给出长度为n的数组\(a\),满足\(\sum_{i=1}^n\frac{1}{a[i]}\le\frac{1}{2}\),构造数列\(c\),满足对于\(c\)的循环数列\(b\),\(b[i]=c[i\%m]\),满足\(b\)中每\(a[i]\)个数中存在\(i\)
题解
非正解,我们发现\(\sum_{i=1}^n\frac{1}{a[i]}\le\frac{1}{2}\)的条件,\(a[i]\)不会很小,于是可以考虑贪心着填,当遇到已经填到的位置就往前填,为防止首位冲突,我们再最后也尽可能填
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 | #include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int a[N];
int b[N];
struct node_ {
int a, pos;
}node[N];
bool cmp(node_ x, node_ y) {
return x.a < y.a;
}
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) {
scanf("%d", &node[i].a);
node[i].pos = i;
}
sort(node + 1, node + n + 1, cmp);
int s = 1;
for(int i = 1; i <= n; i ++ ) {
while(b[s]) s ++ ;
for(int j = s; ;j += node[i].a) {
while(b[j] || j > 1000000) j -- ;
b[j] = node[i].pos;
if(1000000 - j + s <= node[i].a) break;
}
s ++ ;
}
printf("1000000\n");
for(int i = 1; i <= 1000000; i ++ ) {
if(!b[i]) printf("1 ");
else printf("%d ", b[i]);
}
return 0;
}
|