#include <bits/stdc++.h>
typedef long long lint;
typedef long double llf;
using namespace std;
typedef pair<int, int> pi;
int n, m, a[1000005];
int ans[5005];
int tab[5005][5005];
void update(int sx, int ex, int sy, int ey, int v){
tab[sx][sy] += v;
tab[ex+1][sy] -= v;
tab[sx][ey+1] -= v;
tab[ex+1][ey+1] += v;
}
void get_table(int s, int e){
memset(tab, 0, sizeof(tab));
for(int i=s+1; i<=e; i+=2){
if(a[i-1] == a[i]){
update(1, a[i-1] - 1, 1, a[i-1] - 1, 2);
update(a[i-1] + 1, m, 1, a[i-1] - 1, 2);
update(1, a[i-1] - 1, a[i-1] + 1, m, 2);
update(a[i-1] + 1, m, a[i-1] + 1, m, 2);
}
else{
int x = min(a[i-1], a[i]);
int y = max(a[i-1], a[i]);
update(1, m, 1, m, 1);
update(1, x-1, 1, x-1, 1);
update(x+1, y-1, 1, x-1, 1);
update(y+1, m, 1, x-1, 1);
update(1, x-1, x+1, y-1, 1);
update(x+1, y-1, x+1, y-1, 1);
update(y+1, m, x+1, y-1, 1);
update(1, x-1, y+1, m, 1);
update(x+1, y-1, y+1, m, 1);
update(y+1, m, y+1, m, 1);
}
}
}
void apply(){
for(int i=1; i<=m; i++){
for(int j=1; j<=m; j++){
tab[i][j] += tab[i-1][j] + tab[i][j-1] - tab[i-1][j-1];
}
}
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
}
if(m > 5000) return 0;
memset(ans, 0x3f, sizeof(ans));
if(n % 2 == 0){
get_table(1, n);
apply();
for(int i=1; i<=m; i++){
for(int j=1; j<=i; j++){
ans[i] = min(ans[i], tab[i][j]);
ans[j] = min(ans[j], tab[i][j]);
}
}
get_table(2, n-1);
for(int i=1; i<=n; i+=n-1){
update(1, a[i] - 1, 1, a[i] - 1, 1);
update(a[i] + 1, m, 1, a[i] - 1, 1);
update(1, a[i] - 1, a[i] + 1, m, 1);
update(a[i] + 1, m, a[i] + 1, m, 1);
}
apply();
for(int i=1; i<=m; i++){
for(int j=1; j<=i; j++){
ans[i] = min(ans[i], tab[i][j]);
ans[j] = min(ans[j], tab[i][j]);
}
}
}
else{
get_table(1, n-1);
update(1, a[n] - 1, 1, a[n] - 1, 1);
update(a[n] + 1, m, 1, a[n] - 1, 1);
update(1, a[n] - 1, a[n] + 1, m, 1);
update(a[n] + 1, m, a[n] + 1, m, 1);
apply();
for(int i=1; i<=m; i++){
for(int j=1; j<=i; j++){
tab[i][j] += (a[n] != i && a[n] != j);
ans[i] = min(ans[i], tab[i][j]);
ans[j] = min(ans[j], tab[i][j]);
}
}
get_table(2, n);
update(1, a[1] - 1, 1, a[1] - 1, 1);
update(a[1] + 1, m, 1, a[1] - 1, 1);
update(1, a[1] - 1, a[1] + 1, m, 1);
update(a[1] + 1, m, a[1] + 1, m, 1);
apply();
for(int i=1; i<=m; i++){
for(int j=1; j<=i; j++){
ans[i] = min(ans[i], tab[i][j]);
ans[j] = min(ans[j], tab[i][j]);
}
}
}
for(int i=1; i<=m; i++) printf("%d\n", ans[i]);
}
Compilation message
rope.cpp: In function 'int main()':
rope.cpp:53:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
^
rope.cpp:55:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103796 KB |
Output is correct |
2 |
Correct |
23 ms |
103796 KB |
Output is correct |
3 |
Incorrect |
26 ms |
103796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103796 KB |
Output is correct |
2 |
Correct |
23 ms |
103796 KB |
Output is correct |
3 |
Incorrect |
26 ms |
103796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103796 KB |
Output is correct |
2 |
Correct |
23 ms |
103796 KB |
Output is correct |
3 |
Incorrect |
26 ms |
103796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103796 KB |
Output is correct |
2 |
Correct |
23 ms |
103796 KB |
Output is correct |
3 |
Incorrect |
26 ms |
103796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103796 KB |
Output is correct |
2 |
Correct |
23 ms |
103796 KB |
Output is correct |
3 |
Incorrect |
26 ms |
103796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |