Noip simulation 90 2021.11.5 (longest problem solution)

T1 palindrome This classic \ (dp \) just can'...
T1 palindrome
T2 quick sort
T3 chaos evil
Pigeons on the crooked neck tree outside T4 school

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; auto read=[](){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') while(ch>='0'&&ch<='9') return x*f; }; auto write=[](int x,char opt='\n'){ char ch[20];short len=0;if(x<0)x=~x+1,putchar('-'); dowhile(x); for(short i=len-1;i>=0;--i)putchar(opt); }; }using namespace AE86; int n,m,a[NN][NN],len; int dp[2][NN][NN]; char s[NN]; namespace WSN{ inline short main(){ wsn=freopen("palin.in","r",stdin); wsn=freopen("palin.out","w",stdout); n=read(); m=read(); len=n+m-2>>1; for(int i=1;i<=n;i++) if(a[1][1]!=a[n][m]) return puts("0"),0; dp[0][1][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()

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; auto read=[](){ int x=0;bool f=1;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar();} if(!f) return -1ll; while(ch>='0'&&ch<='9') return x; }; auto write=[](int x,char opt='\n'){ char ch[20];short len=0;if(x<0)x=~x+1,putchar('-'); dowhile(x); for(short i=len-1;i>=0;--i)putchar(opt); }; }using namespace AE86; int T,n,stk[NN],top; struct numbera[NN]; multiset<int> s; auto solve=[](){ n=read(); s.clear(); for(int i=1;i<=n;i++){ int x=read();if(x>0)s.insert(x); a[i]=x<0?number:number; } 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); T=read();while(T--)solve(); return 0; } } signed main()

T3 chaos evil

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

\(\color{\huge}\)

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

\(\ \) is a subset of \ (\ \)

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

\(\lfloor\frac\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 \) 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,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_-a_ \)

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_,a_ \) 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 \) even numbers, and the remaining \ (\ frac-\frac \) 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-\frac) \)

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

Then we found that \ (SM \ Leq m - \ frac < 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{\huge } \)

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; auto read=[](){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') while(ch>='0'&&ch<='9') return x*f; }; auto write=[](int x,char opt='\n'){ char ch[20];short len=0;if(x<0)x=~x+1,putchar('-'); dowhile(x); for(short i=len-1;i>=0;--i)putchar(opt); }; }using namespace AE86; int n,m,sm,c[NN]; vector<pair<int,int> > t; struct nodea[NN]; namespace WSN{ inline short main(){ wsn=freopen("chaoticevil.in","r",stdin); wsn=freopen("chaoticevil.out","w",stdout); n=read();m=read(); for(int i=1;i<=n;++i)a[i]=node; if(n&1) a[++n]=node; sort(a+1,a+n+1,[](node x,node y)->bool); 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[1].val)--n; for(int j=1;j<=n;++j)printf("%d ",c[j]); puts(""); return 0; } } return 0; } } signed main() //NP-Hard solved //Chaotic evil

Pigeons on the crooked neck tree outside T4 school

Changing, trying to change

5 November 2021, 22:34 | Views: 4754

Add new comment

For adding a comment, please log in
or create account

0 comments