The meaning of problems: a company with employees in and out every day, $ a [i]> represents $ a [i] 0 $ $ when the employee into the company, $ a [i] <represents $ -a [i] 0 $ when $ the staff of the company, there are some strict rules and out of office
- Office staff can only enter once a day
- If he did that day into the office, he obviously can not leave
- Start and end of the office are empty (employees can not stay the night) a day, the office may at any time of the day are empty
You are given a sequence $ a $ ($ a [i] \ neq 0 $), ask if you can put an array $ a $ is divided into several adjacent sub-arrays, each sub-array out of the situation so that employees comply with the requirements and the length of each sub-array output, or the output can not represent $ $ -1
Thinking: MK $ $ array with a flag state of a person, $ mk [i] = 0 $ $ I $ denotes the number of employees within has not been operated, $ mk [i] = 1 $ $ I $ denotes the number of employees the days have been into the office, $ mk [i] = - 1 $ $ i $ represents the number of days that employees have entered the office and out, $ num $ record number of office at this time, if when $ num = $ 0 you can enter a new day, while the need to use an array of $ p $ records for this day has to go out of employee numbers, when entering a new day, you need these employees $ mk [i] $ becomes $ 0 $, appeared in the middle not lawful directly out of the loop, the output of $ -1 $
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; const int N = 100010; const int M = 1000010; int n, a[N], mk[M]; int num, cnt, p[N]; int res[N], pos; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int flag = 1; res[++pos] = 0; for (int i = 1; i <= n; i++) { if (a[i] > 0) { if (0 == mk[a[i]]) { mk[a[i]] = 1, num++; } else { flag = 0; break; } } else { if (1 == mk[-a[i]]) { mk[-a[i]] = -1, num--; p[++cnt] = -a[i]; if (0 == num) { for (int j = 1; j <= cnt; j++) mk[p[j]] = 0; cnt = 0, res[++pos] = i; } } else { flag = 0; break; } } } if (0 != num) flag = 0; if (!flag) printf("-1\n"); else { printf("%d\n", pos - 1); for (int i = 2; i <= pos; i++) { printf("%d", res[i] - res[i - 1]); printf(i == pos ? "\n" : " "); } } return 0; }