# Noip simulation 90 2021.11.5 (longest problem solution)

## T1 palindrome

This classic \ (dp \) just can't do it, just can't.....

Except for the \ (dp \) array, everything else is similar to the problem solution, but it's useless....

Let \ (dp[len][x1][x2] \) mean that half the length of the palindrome string is \ (len \), the abscissa of the end point of the string starting from \ ((1,1) \) is \ (x1 \), and the abscissa of the end point of the string starting from \ ((n,m) \) is \ (x2 \)

Then transfer according to the situation

Finally, the answers are counted at a diagonal line, that is, the palindrome center

palin
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NN=501,mod=993244853;
namespace AE86{
FILE *wsn;
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
};
auto write=[](int x,char opt='\n'){
char ch;short len=0;if(x<0)x=~x+1,putchar('-');
do{ch[len++]=x%10+(1<<5)+(1<<4);x/=10;}while(x);
for(short i=len-1;i>=0;--i){putchar(ch[i]);}putchar(opt);
};
}using namespace AE86;
int n,m,a[NN][NN],len;
int dp[NN][NN];
char s[NN];
namespace WSN{
inline short main(){
wsn=freopen("palin.in","r",stdin);
wsn=freopen("palin.out","w",stdout);
for(int i=1;i<=n;i++){scanf("%s",s+1);
for(int j=1;j<=m;j++)a[i][j]=s[j]-'a';
}
if(a!=a[n][m]) return puts("0"),0;
dp[n]=1;
for(int i=0;i<len;i++){
int x1=min(n,i+1),x2=max(1ll,n-i);
memset(dp[i+1&1],0,sizeof(dp[i+1&1]));
for(int j=1;j<=x1;j++){
int y1=i-j+2;
for(int k=n;k>=x2;k--){
int y2=n+m-k-i;
if(j<n&&y2>1) if(a[j+1][y1]==a[k][y2-1]) (dp[i+1&1][j+1][k]+=dp[i&1][j][k])%=mod;
if(j<n&&k>1)  if(a[j+1][y1]==a[k-1][y2]) (dp[i+1&1][j+1][k-1]+=dp[i&1][j][k])%=mod;
if(y1<m&&y2>1)if(a[j][y1+1]==a[k][y2-1]) (dp[i+1&1][j][k]+=dp[i&1][j][k])%=mod;
if(y1<m&&k>1) if(a[j][y1+1]==a[k-1][y2]) (dp[i+1&1][j][k-1]+=dp[i&1][j][k])%=mod;
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
(ans+=dp[len&1][i][i])%=mod;
if((n+m&1)&&i<n) (ans+=dp[len&1][i][i+1])%=mod;
} write(ans);
return 0;
}
}
signed main(){return WSN::main();}


## T2 quick sort

It's really like a big conclusion question, but I think it was almost analyzed in the examination room, but there was still no....

It is found that the relative position of \ (nan \) will not change when \ (nan \) is \ (left \) in his sorting process

So scan it directly. When you reach a number that is not \ (nan \), you will output all the numbers less than this number

Yes \ (nan \) direct output \ (nan \)

qsort
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int NN=5e5+5;
namespace AE86{
FILE *wsn;
int x=0;bool f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='a'){f=0,ch=getchar();break;}ch=getchar();}
if(!f) return -1ll;
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x;
};
auto write=[](int x,char opt='\n'){
char ch;short len=0;if(x<0)x=~x+1,putchar('-');
do{ch[len++]=x%10+(1<<5)+(1<<4);x/=10;}while(x);
for(short i=len-1;i>=0;--i){putchar(ch[i]);}putchar(opt);
};
}using namespace AE86;
int T,n,stk[NN],top;
struct number{bool isnan;int value;}a[NN];
multiset<int> s;
auto solve=[](){
for(int i=1;i<=n;i++){
a[i]=x<0?number{1,x}:number{0,x};
}
for(int i=1;i<=n;i++){
if(a[i].isnan) printf("nan ");
else{
if(s.find(a[i].value)==s.end())continue;
multiset<int>::iterator it=s.begin();
while(*it<a[i].value){
stk[++top]=*it;
++it;
}
for(int j=1;j<=top;j++)
s.erase(s.find(stk[j])),printf("%lld ",stk[j]);
top=0; printf("%lld ",*it); s.erase(it);
}
}
puts("");
};
namespace WSN{
inline short main(){
wsn=freopen("qsort.in","r",stdin);
wsn=freopen("qsort.out","w",stdout);
return 0;
}
}
signed main(){return WSN::main();}


## T3 chaos evil

Entrusted by \ (OMA \), I decided to write the solution of this problem into a paper.....

$$\color{red}{\huge{Warning}}$$

Considering the limitations of this problem, what will the original array look like

$$\ {a_1,a_2,\dots,a_n \}$$ is a subset of \ (\ {1,2,\dots,m \} \)

This ensures that all \ (a_i \) are different from each other, and the value range is determined

$$\lfloor\frac{2m}{3}\rfloor <n\leq m$$

This ensures that there must be a series of equal difference sequences in the \ (a \) array with a tolerance of \ (1 \)

This is not difficult to prove, because there are at most \ (\ frac{m}{2} \) even or odd numbers in \ ([1,m] \),

It is obviously impossible to ensure that the difference between two numbers is greater than \ (1 \), because \ (n\in (\frac{2m}{3},m] \)

Therefore, after sorting, there must be a sequence of equal difference numbers with a tolerance of \ (1 \), that is, odd and even numbers

Ensure that \ (\ sum a_i \) is even

This restriction ensures that there must be even and odd numbers in the \ (a \) array

Consider that if there is no limit, the array \ (a \) has several cases

It is not difficult to find that the array \ (a \) is either even or even odd, otherwise it cannot meet \ (\ sum a_i\in even \)

The previous restrictions ensure that the \ (a \) array cannot all be even, so there can only be even and odd numbers in the array

In this way, we have analyzed several properties of array \ (a \) construction:

1. All numbers are different from each other, and the value range is between \ ([1,m] \)
2. There must be a series of equidistant numbers with a tolerance of one
3. There must be even and odd numbers in the array

Then we consider how to construct.

If the length of the array is odd, we add an element \ (0 \) to it, which ensures that the length of the array is even, so that we can group later

We sort the array \ (a \) in ascending order, then the adjacent pairs are grouped into a group, and record \ (d_i=a_{2i}-a_{2i-1} \)

Then we record \ (sm=\sum d_i \), which is the deviation value of our "pre constructed" scheme (that is, how much more than the correct construction scheme)

Then we "select" in descending order of \ (d_i \)

A group of elements with a difference of \ (d_i \) are found. If the positional relationship is exchanged, i.e. \ (a_{2i},a_{2i-1} \) is exchanged

It will have \ (2d_i \) impact on \ (SM \), that is \ (sm-2d_i \), so our "selection" process is the process of eliminating the difference between the original construction schemes

In this way, we have realized the construction of this problem, and there is no illegal scheme (if you don't want to see the proof, you can start happily to the point now)

We began to prove the correctness of such a structure on a large scale....

First, obviously, adding an element \ (0 \) will not affect the answer

Then we consider why there must be no illegal scheme

It is found that the total contribution generated by each exchange of element positions must be an even number, so the problem we want to prove now becomes \ (\ sum d_i\in even \),

start

The premise of our construction is that if the array length is even and there are even odd numbers, there must be even numbers and even numbers

Then we can discuss it in two cases,

1. Even number is greater than odd number

At this time, part of the sequence after we have arranged the order must be staggered with odd and even numbers. At this time, pairing will produce even pairs of odd and even matches

Their differences are odd, and there are even differences. The \ (\ sum d \) of this part is even, and the rest is better. Even even pairing differences are even

This situation is proved

1. Even number is less than odd number

The staggered parts of odd and even numbers in the front are the same as those in the front. The rest must be even odd numbers. Their matching differences are even numbers, which is also proved

end

After proving that \ (\ sum d \) is an even number, it is found that the problem of proof is that there must be a legal solution to eliminate the difference from large to small

It is not difficult to find that if all differences are selected, the final \ (sm < 0 \) (because \ (sm \) is added by \ (d \), subtracting \ (2d \) will make it less than zero)

We will think of a situation where there may be no solution, that is, when \ (sm=2 \), the minimum difference is \ (2 \) or greater, so the subtraction will be negative, \ (2-2 * 2 < 0 \)

Now we need to find a sequence that makes the difference as much as possible \ (\ geq 2 \). If it also has a solution, then all the sequences have solutions and all the problems will be solved

Then we consider selecting even numbers in \ ([1.m] \), so that there will be \ (\ frac{m}{2} \) even numbers, and the remaining \ (\ frac{2m}{3}-\frac{m}{2} \) can only be odd numbers

You might as well specify them as \ (1,3,5,... \), because the others are the same as these

We found that there is an arithmetic sequence with a tolerance of one with a length of \ (2\times(\frac{2m}{3}-\frac{m}{2}) \)

In this way, there will be \ (\ frac{2m}{3}-\frac{m}{2}=\frac{m}{6} \) pairs with a difference of \ (1 \) after pairwise grouping, and the remaining \ (\ frac{n}{2}-\frac{m}{6} \) groups have an even number to ensure their difference \ (\ geq 2 \)

Then we found that \ (SM \ Leq m - \ frac{n}{2} < n \) (only we don't know why here, let's use what the problem solution says)

Therefore, you can eliminate \ (sm \) without using a binary with a difference of one. If you find \ (sm-2 * d < 0 \), you can find the previous one with a difference of \ (1 \), then the above structures are reasonable

$$\ color{red}{\huge {certificate is finished, hand is disabled!!!}}$$

But I will admire the people who can see here all the time from above. I don't have this patience anyway (salute you ~ ~)

chaoticevil
#include<bits/stdc++.h>
using namespace std;
const int NN=1e6+5;
namespace AE86{
FILE *wsn;
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
};
auto write=[](int x,char opt='\n'){
char ch;short len=0;if(x<0)x=~x+1,putchar('-');
do{ch[len++]=x%10+(1<<5)+(1<<4);x/=10;}while(x);
for(short i=len-1;i>=0;--i){putchar(ch[i]);}putchar(opt);
};
}using namespace AE86;
int n,m,sm,c[NN];
vector<pair<int,int> > t;
struct node{int val,id;}a[NN];
namespace WSN{
inline short main(){
wsn=freopen("chaoticevil.in","r",stdin);
wsn=freopen("chaoticevil.out","w",stdout);
if(n&1) a[++n]=node{0,n};
sort(a+1,a+n+1,[](node x,node y)->bool{return x.val<y.val;});
for(int i=1,d;i<=n;i+=2){
d=a[i+1].val-a[i].val;
t.push_back(make_pair(d,i)); sm+=d;
c[a[i+1].id]=1; c[a[i].id]=-1;
}
sort(t.begin(),t.end());
for(int i=t.size()-1,v,id;~i;--i){
v=t[i].first,id=t[i].second;
if(sm>=v*2)
sm-=v*2,swap(c[a[id].id],c[a[id+1].id]);
else if(sm==0){
puts("NP-Hard solved");if(!a.val)--n;
for(int j=1;j<=n;++j)printf("%d ",c[j]);
puts(""); return 0;
}
}
return 0;
}
}
signed main(){return WSN::main();}
//NP-Hard solved
//Chaotic evil


## Pigeons on the crooked neck tree outside T4 school

Changing, trying to change

Posted on Fri, 05 Nov 2021 22:34:25 -0400 by aouriques