当前位置: 首页 > 工具软件 > silly > 使用案例 >

【CodeForces 1253B --- Silly Mistake】

戚奇略
2023-12-01

The Central Company has an office with a sophisticated security system. There are 106 employees, numbered from 1 to 106.

The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer −i.

The company has some strict rules about access to its office:

An employee can enter the office at most once per day.
He obviously can’t leave the office if he didn’t enter it earlier that day.
In the beginning and at the end of every day, the office is empty (employees can’t stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.

Some examples of valid or invalid days:

  • [1,7,−7,3,−1,−3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
  • [2,−2,3,−3] is also a valid day.
  • [2,5,−5,5,−5,−2] is not a valid day, because 5 entered the office twice during the same day.
  • [−4,4] is not a valid day, because 4 left the office without being in it.
  • [4] is not a valid day, because 4 entered the office and didn’t leave it before the end of the day.

There are n events a1,a2,…,an, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn’t change the order of the events.

You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it’s impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.

For example, if n=8 and a=[1,−1,1,2,−1,−2,3,−3] then he can partition it into two contiguous subarrays which are valid days: a=[1,−1 | 1,2,−1,−2,3,−3].

Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.

Input

The first line contains a single integer n (1≤n≤105).

The second line contains n integers a1,a2,…,an (−106≤ai≤106 and ai≠0).

Output

If there is no valid partition, print −1. Otherwise, print any valid partition in the following format:

On the first line print the number d of days (1≤d≤n).
On the second line, print d integers c1,c2,…,cd (1≤ci≤n and c1+c2+…+cd=n), where ci is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don’t have to minimize nor maximize the number of days.

Sample Input

6
1 7 -7 3 -1 -3

Sample Output

1
6

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using ll = long long;
const int inf = 0x3f3f3f3f;
const ll INT = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e6+5;
bool in[MAXN];
int last[MAXN];

int main()
{
    SIS;
    int n,x,cnt=0,y=0;
    bool flag=false;
    vector<int> v;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> x;
        if(x>0)
        {
            if(in[x]) flag=true;
            if(y<last[x]) flag=true;
            cnt++;
            in[x]=true;
        }
        else
        {
            if(!in[-x]) flag=true;
            cnt--;
            in[-x]=false;
            last[-x]=i;
        }
        if(cnt==0) v.emplace_back(i+1),y=i;
    } 
    if(cnt) flag=true;
    if(flag) cout << -1 << endl;
    else
    {
        int len=v.size();
        cout << len << endl << v[0];
        for(int i=1;i<len;i++) cout << " " << v[i]-v[i-1];
        cout << endl;
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答