[Educational Codeforces Round 94] D. Zigzags dp preprocessing + enumeration

Title link: D. Zigzags

Title

Give you a sequence a of length n, let you find how many quadruples meet the conditions.
Quadruple (i,j,k,l), where 1 ≤ i <j <k <l ≤ n {1≤i<j<k<l≤n}1i<j<k<ln,并且 a [ i ] = a [ k ] , a [ j ] = a [ l ] {a[i]=a[k],a[j]=a[l]} a[i]=a[k],a[j]=a[l]

answer

This question is difficult to start at the beginning, I don't know how to enumerate.

  1. In fact, we can try if the size of enumeration i and j, a[k] and a[l] is determined, but the position cannot be determined, there is a relative position relationship between k and L, and adding a layer of loop will obviously time out, so Pass. The same is true for enumerations k and l.
  2. If i and k are enumerated, then the positions of a[j] and a[l] can be determined, but the size cannot be determined, so it still does not work. The same is true for enumerations j and l.
  3. If enumerating j and k, then the size of a[i] and a[l] can be determined, and the position can be obtained, which is feasible. So we can enumerate j and k. But note that i and l cannot be enumerated, because j and k have a relative positional relationship, which is not feasible.

Now what we need to think about is how to find the answer when enumerating j and k.
In fact, what we need now is the number of equal to a[k] in the interval from 1 to (i-1) and the number of equal to a[j] in the interval from (k+1) to n.
This question gives another condition a [i] ≤ n {a[i]≤n}a[i]n , then it is easy to think of dp to preprocess.

Let dp[i][j]: how many numbers are equal to the number j in the interval from 1 to i.
State transition equation:
if j==a[i] dp[i][j]=dp[i-1][j]+1
otherwise dp[i][j]=dp[i-1][j];

After preprocessing, use the idea of ​​prefix sum to find the number equal to a[k] in the interval i~j: dp[j][a[k]]-dp[i-1][a[k]]

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=3e3+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int a[maxn];
int dp[maxn][maxn];
int main()
{
    
    
	int t;
	scanf("%d",&t);
	while(t--)
	{
    
    
		int n; scanf("%d",&n);
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
		{
    
    
			for(int j=1;j<=n;j++) 
			{
    
    
				if(j==a[i])dp[i][j]=dp[i-1][j]+1;
				else dp[i][j]=dp[i-1][j];
			}
		}	
		ll ans=0;
		for(int j=2;j<n-1;j++)
			for(int k=j+1;k<n;k++)
				ans+=dp[j-1][a[k]]*(dp[n][a[j]]-dp[k][a[j]]);
		printf("%lld\n",ans);
	}
}

Guess you like

Origin blog.csdn.net/weixin_44235989/article/details/108246762