POJ 3378 Crazy Thairs 题解

POJ 3378 Crazy Thairs 题解

POJ 3378

题目

These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ≤ N ≤ 50000) numbers, which are no more than 109, Crazy Thair is a group of 5 numbers {i, j, k, l, m} satisfying:

  1. 1 ≤ i < j < k < l < m ≤ N
  2. Ai < Aj < Ak < Al < Am

For example, in the sequence {2, 1, 3, 4, 5, 7, 6},there are four Crazy Thair groups: {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 7} and {2, 3, 4, 5, 7}.

Could you help Sempr to count how many Crazy Thairs in the sequence?


输入

Input contains several test cases. Each test case begins with a line containing a number N, followed by a line containing N numbers.


输出

Output the amount of Crazy Thairs in each sequence.


样例

input
5
1 2 3 4 5
7
2 1 3 4 5 7 6
7
1 2 3 4 5 6 7

output
1
4
21


题意

在这里插入图片描述


解题思路

一道离散化+ D P DP DP+树状数组+高精(可用两个 l o n g long long l o n g long long 代替)的题
放洛谷绝对妥妥的紫题
离散化输入的数
存它们从小到大排后的下标

DP
应该都做过最长上升子序列这题吧,差不多的
f [ i ] [ j ] f[i][j] f[i][j]意为以第j个数结尾,长为i的子序列
转移方程: f [ i ] [ j ] = m a x ( f [ i ] [ j ] , f [ i − 1 ] [ j − 1 ] ) ; f[i][j]=max(f[i][j],f[i-1][j-1]); f[i][j]=max(f[i][j],f[i1][j1]);

树状数组
树状数组维护 D P DP DP
这是个二维的树状数组
一维维护子序列的长度
一维维护子序列中最后一个数在离散化后的下标


代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const long long mo=1000000000000000;
struct hhx{
	int x,s;
}a[500100];
int n,c[500100];
long long ans,ans2,tree[6][500100];
bool cmp(hhx t,hhx x)
{
	 return t.s<x.s;
}
void add(int x,int y,long long z)
{  
	 for (;y<=n;y+=y & (-y))
	     tree[x][y]+=z;  //累加方案数
}
long long tj(int x,int y)
{ 
	 long long sum=0;
	 for (;y>0;y-=y & (-y))
	     sum+=tree[x][y];
	 return sum;
}
int main()
{
	while (scanf("%d",&n)!=EOF)
	{
		  for (int i=1;i<=n;i++)
		  {
		  	  scanf("%d",&a[i].s); 
		  	  a[i].x=i;
		  }
		  sort(a+1,a+n+1,cmp);  //离散化
		  memset(tree,0,sizeof(tree));  //清0
		  c[a[1].x]=1;  //初值
		  int k=1;
		  for (int i=2;i<=n;i++)
		      if (a[i].x==a[i-1].x)  //重复的,下标一样
		         c[a[i].x]=k;
		         else {
		         	  k++;
		         	  c[a[i].x]=k;
		         } 
		   ans=ans2=0;
		   for (int i=1;i<=n;i++)
		   {
		 	   add(1,c[i],1);
		 	   for (int j=2;j<=4;j++)
		 	       add(j,c[i],tj(j-1,c[i]-1));  //add是存入树状数组,tj是统计长为j-1,最后一个数比a[i]小的数的方案数
		 	   ans+=tj(4,c[i]-1);  //求五元组,更新答案
		 	   ans2+=ans/mo;  
		 	   ans=ans%mo;
		   }
		   if (ans2)  //超了一个long long,先输出超的
		      printf("%lld",ans2);
		    printf("%lld\n",ans);
	}
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值