# CF1479B Painting the Array

## Meaning:

The only difference between this question and CF1480D2 is that this question asks for the maximum possible solution

to set one individual number group a , you take a i dye by b i colour , his in b yes from you finger set of one individual 01 number group . take a number group in cover dye become 0 colour of number word take Out come and according to stay a in Out present of along order Row column , group become number group a ( 0 ) . with reason , take a number group in cover dye become 1 colour of number word take Out come and according to stay a in Out present of along order Row column , group become number group a ( 1 ) . Given an array a, you will a_i is dyed as b_i color, where b is a 01 array specified by you. Take out the numbers dyed 0 in array A and arrange them in the order they appear in a to form array a^{(0)}. Similarly, take out the numbers dyed 1 in array A and arrange them in the order they appear in a to form array a^{(1)} Given an array a, you dye ai into bi, where b is a 01 array specified by you. Take out the numbers dyed 0 in array A and arrange them in the order they appear in a to form array a(0). Similarly, take out the numbers dyed 1 in array A and arrange them in the order they appear in a to form array a(1)

I Guys set righteousness s e g ( c ) yes one individual just whole number , his in c yes one individual number group , s e g ( c ) of value by stay I Guys take c in mutually adjacent of place yes mutually with element element close and after , c number group of large Small . example as , s e g ( [ 1 , 1 , 4 , 5 , 1 , 4 ] ) = ∣ [ 1 , 4 , 5 , 1 , 4 ] ∣ = 5. most large turn s e g ( a ( 0 ) ) + s e g ( a ( 1 ) ) We define seg(c) as a positive integer, where C is an array, and the value of seg(c) is the size of the C array after we combine all adjacent identical elements in C. for example, seg([1, 1, 4, 5, 1, 4]) = |[1, 4, 5, 1, 4]|=5. Maximize seg(a^{(0)})+seg(a^{(1)}) We define seg(c) as a positive integer, where C is an array, and the value of seg(c) is the size of C array after we combine all adjacent identical elements in C. for example, seg([1,1,4,5,1,4]) = ∣ [1,4,5,1,4] ∣ = 5. Maximize seg(a(0))+seg(a(1))

## Solution:

Greedy strategy, question D1. If we want to make the value larger, we should avoid the adjacent situation of the same number as much as possible. Now there are two arrays a 0 a^0 a0 and a 1 a^1 a1, then we can treat it as a stack. When allocating array a, we should make the same elements diverge as much as possible

1. If the first stack top = = the second stack top, then ai just put it aside,
2. If the first stack top= The second and the first stack top= a i a_{i} ai, then put ai on another stack
3. If the first stack top= The second, and the second stack top= a i a_{i} ai, then put ai on another stack
4. If the two stacks are not equal to ai and the top of the two stacks are not the same, it seems that you can put them anyway at this time? But it's not because the position we put will affect the later decision, because it will become the new top of the stack after we put it. At this point, we introduce nxt array, nxt[x] represents the next array a x a_{x} The location of the. We compare the nxt at the top of the two stacks. The more obvious it is that the same element appears again, the closer it is, and we need to separate it with different elements

The D2 question is the opposite statistical answer

## code:

### D1

```#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#else
startTime = clock ();
freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#else
endTime= clock();
printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn=2e5+9;
int a[maxn];
int nxt[maxn];
int id[maxn];
vector<PII>x,y;
int main()
{
//rd_test();
int n;
for(int i=1;i<=n;i++){
id[i]=n+1;
}
for(int i=n;i>=1;i--){
nxt[i]=id[a[i]];
id[a[i]]=i;
}
x.push_back({0,n+1});
y.push_back({0,n+1});
int ans=0;
for(int i=1;i<=n;i++){
if(a[i]==x.back().first&&a[i]==y.back().first){
x.push_back({a[i],nxt[i]});
}
else if(a[i]==x.back().first){
y.push_back({a[i],nxt[i]});
ans++;
}
else if(a[i]==y.back().first){
x.push_back({a[i],nxt[i]});
ans++;
}
else
{
ans++;
if(x.back().second>y.back().second){
y.push_back({a[i],nxt[i]});
}
else
x.push_back({a[i],nxt[i]});
}
}
cout<<ans<<endl;
//Time_test();
}

```

### D2

```#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#else
startTime = clock ();
freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#else
endTime= clock();
printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn=2e5+9;
int a[maxn];
int nxt[maxn];
int id[maxn];
vector<PII>x,y;
int main()
{
//rd_test();
int n;
for(int i=1;i<=n;i++){
id[i]=n+1;
}
for(int i=n;i>=1;i--){
nxt[i]=id[a[i]];
id[a[i]]=i;
}
x.push_back({0,n+1});
y.push_back({0,n+1});
int ans=0;
for(int i=1;i<=n;i++){
if(a[i]==x.back().first&&a[i]==y.back().first){
x.push_back({a[i],nxt[i]});
}
else if(a[i]==x.back().first){
x.push_back({a[i],nxt[i]});
}
else if(a[i]==y.back().first){
y.push_back({a[i],nxt[i]});
}
else
{
ans++;
if(x.back().second>y.back().second){
x.push_back({a[i],nxt[i]});
}
else
y.push_back({a[i],nxt[i]});
}
}
cout<<ans<<endl;
//Time_test();
}

```

Tags: greedy algorithm

Posted on Sun, 03 Oct 2021 19:56:53 -0400 by cahamilton