Summer Reading
Time Limit: 20 Sec Memory Limit: 512 MB
Description
Output
7
0 1 0 0 0 3 0
Sample Output
3
1 1 2 2 3 3 3
HINT
Solution
Code
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include<bits/stdc++.h> using namespace std; typedef long long s64;
const int ONE = 1000005; const int MOD = 1e9 + 7;
int n; int a[ONE]; int vis[ONE], ans[ONE];
struct power { int val, times; friend bool operator <(power a, power b) { if(a.val != b.val) return a.val < b.val; return a.times < b.times; } }low[ONE], up[ONE];
int get() { int res;char c; while( (c=getchar())<48 || c>57 ); res=c-48; while( (c=getchar())>=48 && c<=57 ) res=res*10+c-48; return res; }
void Deal_first() { low[1] = up[1] = (power){1, 1}; for(int i = 2; i <= n; i++) { low[i] = low[i - 1], up[i] = up[i - 1];
if(++low[i].times > 5) low[i] = (power){low[i].val + 1, 1}; if(++up[i].times > 2) up[i] = (power){up[i].val + 1, 1};
if(a[i] != 0) { if(low[i].val > a[i] || up[i].val < a[i]) { printf("-1"); exit(0); } low[i] = max(low[i], (power){a[i], 1}); up[i] = min(up[i], (power){a[i], 5}); }
if(low[i].val > up[i].val) {printf("-1"); exit(0);} } }
int main() { n = get(); for(int i = 1; i <= n; i++) a[i] = get(); if(a[1] > 1) {printf("-1"); exit(0);} Deal_first();
int val = up[n].val - (up[n].times == 1); if(low[n].val > val) {printf("-1"); exit(0);} for(int i = n; i >= 1; i--) { val = min(val, up[i].val); val -= vis[val] == 5; ans[i] = val, vis[val]++; }
if(ans[1] <= 0) {printf("-1"); exit(0);}
printf("%d\n", ans[n]); for(int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
|