codeforces1400D Zigzags

https://codeforces.com/contest/1400/problem/D

I feel that this question is a div3A difficult question. How can I put it in the D position? Everyone has 5 minutes. . .

Enumerate a[j],a[k], then their contribution to the answer is the number of a[k] in (1,j-1) multiplied by the number of a[j] in (k+1,n), Just use a prefix suffix and array maintenance

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;

const int maxl=3010;

int n,m,cas,k,cnt,tot;ll ans;
int a[maxl],b[maxl];
ll num[maxl],suf[maxl][maxl],pre[maxl][maxl];
char s[maxl];
bool in[maxl]; 

inline void prework()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		num[i]=0,scanf("%d",&a[i]);
	for(int j=1;j<=n;j++)
		suf[n+1][j]=0;
	for(int i=n;i>=1;i--)
	{
		for(int j=1;j<=n;j++)
			suf[i][j]=suf[i+1][j];
		suf[i][a[i]]++;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			pre[i][j]=pre[i-1][j];
		pre[i][a[i]]++;
	}
} 

inline void mainwork()
{
	ans=0;
	for(int i=2;i<=n;i++)
		for(int j=i+1;j<n;j++)
			ans+=pre[i-1][a[j]]*suf[j+1][a[i]];
}

inline void print()
{
	printf("%lld\n",ans);
}

int main()
{
	int t=1;
	scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/liufengwei1/article/details/108231807