[CSP-S Simulation Test]: Number of filled simple (analog greedy +)

Title Description

For a length $ n $, and the index from the sequence $ 1 $ numbering of $ a $, we define it is "legitimate" if and only if it satisfies the following conditions:
· $ A_1 = 1 $
· For $ i \ in [1, n), a_i \ leqslant a_ {i + 1} \ leqslant a_i + 1 $ and $ a_ {i + 1} $ is a positive integer
• for any number appears in the $ a $ in off $ V $ , remember the number of times it appears is $ S $, the $ 2 \ leqslant s \ leqslant 5 $
given a sequence of length $ n-$ a $ a $, some of which position is $ 0 $, you need any fill these positions number, such as a legitimate $ a $ sequence, $ and maximizing the value of $ A_N.


Input Format

A first line number $ n $, represents the length of the sequence.
The second line n-$ $ integers, the first integer represents $ I $ $ $ a_i, if $ a_i = 0 $, then the number indicates the position is not filled.


Output Format

If the number of refills legitimate program is not present, output $ $ -1; otherwise, outputting a first line of an integer, indicates the maximum $ $ A_N; second row $ $ n-positive integer, denotes the number of $ I $ complete fill after the number of $ I $ th element of the sequence. If the combination of several solutions of the method, a group of any output


Sample

Sample input 1:

7
0 1 0 0 0 3 0

Sample output 1:

3
1 1 2 2 3 3 3

Sample input 2:

4
0 0 0 3

Sample Output 2:

-1


Data range and tips

For $ 30 \% $ data, $ n \ leqslant 1,000 $;
for data additional $ 30 \% $ of the data to ensure that the randomly generated;
for $ 100 \% $ data, $ 2 \ leqslant n \ leqslant 2 \ times {10} ^ 5,0 \ leqslant a_i \ leqslant {10 } ^ 5 $.


answer

For each location maintains two tuples, namely $ up (x, l) $ indicates the current position can be filled and the maximum number of consecutive $ X $ $ l $; $ down (x, l) $ represents the current to fill only the minimum number and the number of consecutive $ X $ $ l $.

Seeking $ up $ is possible to rise, seeking $ down $ vice versa.

The first question is that the last maximum, as to the second question backwards swept aside can be obtained.

Time complexity: $ \ Theta (n) $.

Desired points: $ 100 $ points.

The actual sub-: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n;
int a[200001];
pair<int,int> up[200001],down[200001];
int sum[100001],ans[200001];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	if(a[1]>1){puts("-1");return 0;}
	a[1]=1;
	up[1]=down[1]=make_pair(1,1);
	for(int i=2;i<=n;i++)
	{
		up[i]=make_pair(up[i-1].first,up[i-1].second+1);
		down[i]=make_pair(down[i-1].first,down[i-1].second+1);
		if(up[i].second>2)
		{
			up[i].first++;
			up[i].second=1;
		}
		if(down[i].second>5)
		{
			down[i].first++;
			down[i].second=1;
		}
		if(a[i])
		{
			if(up[i].first==a[i])up[i].second=min(up[i].second,2);
			if(up[i].first>a[i])up[i]=make_pair(a[i],2);
			if(down[i].first<a[i])down[i]=make_pair(a[i],1);
			if(up[i].first<a[i]||down[i].first>a[i]){puts("-1");return 0;}
		}
	}
	if(up[n].second==1)up[n]=make_pair(up[n-1].first,up[n-1].second+1);
	printf("%d\n",up[n].first);
	ans[n]=up[n].first;
	sum[a[n]]=1;
	for(int i=n-1;i;i--)
	{
		if(a[i])ans[i]=a[i];
		else
		{
			int flag=min(ans[i+1],up[i].first);
			if(sum[flag]==5)flag--;
			ans[i]=flag;
		}
		sum[ans[i]]++;
	}
	for(int i=1;i<=n;i++)printf("%d ",ans[i]);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11495887.html