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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <stdio.h> #include <stdlib.h>
#define max(x, y) x < y ? y : x #define min(x, y) x < y ? x : y #define LL long long #define IN freopen("in.txt", "r", stdin) #define OUT freopen("out.txt", "w", stdout) #define scan(x) scanf("%d", &x) #define sqr(x) (x) * (x) #define f(i, a, b) for (int i = a; i <= b; i++) #define pn(x) printf("%d", x) #define pr1(x) printf("Case %d: ", x) #define pn1(x) printf("Case %d:\n", x) #define pr2(x) printf("Case #%d: ", x) #define pn2(x) printf("Case #%d:\n", x) #define lowbit(x) (x & (-x)) int left[100086], right[100086], leftc[100086], rightc[100086],res=0,cntl=2,cntr=1,last,now,x,fz=1;
int main() { scan(x); scan(last); left[1] = 1; leftc[1] = last - 1; res += last - 1; f(i, 2, x) { scan(now); if (now > last) { res += now - last; left[cntl] = i; leftc[cntl++] = now - last; } if(now<last){ right[cntr] = i; rightc[cntr++] = last - now; } last = now; } pn(res); f(i,1,cntl-1){ while(leftc[i]--) { printf("\n%d ", left[i]); if(rightc[fz]>0){ printf("%d", right[fz]-1); rightc[fz] -= 1; if(rightc[fz]==0) fz += 1; } else printf("%d", x); } } return 0; }
|