【洛谷1637】三元上升子序列
来源:互联网 发布:阿里云服务器流量租用 编辑:程序博客网 时间:2024/06/04 01:19
题面
题目描述
Erwin最近对一种叫”thair”的东西巨感兴趣。。。
在含有n个整数的序列a1,a2……an中,
三个数被称作”thair”当且仅当i< j< k且ai< aj< ak
求一个序列中”thair”的个数。
输入输出格式
输入格式:
开始一个正整数n,
以后n个数a1~an。
输出格式:
“thair”的个数
Input
4
2 1 3 4
Output
2
Input
5
1 2 2 3 4
Output
7
/*
7个”thair”分别是
1 2 3
1 2 4
1 2 3
1 2 4
1 3 4
2 3 4
2 3 4
*/
说明
约定 30%的数据n<=100
60%的数据n<=2000
100%的数据n<=30000
大数据随机生成
0<=a[i]<=maxlongint
题解
原来做过一道极其类似的题目
对于任意一个选定的数aj而言
要求的答案即是
1~j-1中比aj小的数的个数与
j+1~n中比aj大的数的个数
的乘积的和
求出数的个数可以直接使用树状数组
最后直接统计结果就行了
但是,考虑到N的范围很小
但是ai< maxlongint
因此,如果不使用离散化的话,可能会炸空间
好了。说了这里
直接上代码了:
//ai在maxlongint内,不离散化数组开不下。。。 #include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<algorithm>using namespace std;#define MAX 40000#define INF 30000int n;inline int read(){ register int x=0,t=1; register char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-'){t=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} return x*t;}struct Node{ int a;//数值 int i;//位置 }Q[MAX];bool operator <(Node a,Node b){ return a.a<b.a;}int c[MAX*4];int a[MAX],t1[MAX],t2[MAX];inline int lowbit(int x){ return x&(-x);}inline void update(int x){ while(x<=n) { c[x]+=1; x+=lowbit(x); }}inline int get(int x){ int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}int main(){ n=read(); for(int i=1;i<=n;++i) Q[i]=(Node){read(),i}; //离散化 sort(&Q[1],&Q[n+1]); for(int i=1;i<=n;++i) { if(Q[i].a!=Q[i-1].a||i==1) a[Q[i].i]=i; else a[Q[i].i]=a[Q[i-1].i]; } //树状数组 //第一遍,求出前面所有数中,比它小的个数 for(int i=1;i<=n;++i) { t1[i]=get(a[i]-1); update(a[i]); } memset(c,0,sizeof(c)); //第二遍,求出后面所有数中,比它大的个数 for(int i=n;i>=1;--i) { t2[i]=get(n-a[i]); update(n-a[i]+1); } //最后求和 long long ans=0; for(int i=1;i<=n;++i) ans+=t1[i]*t2[i]; cout<<ans<<endl; return 0; }
阅读全文
0 0
- 【洛谷1637】三元上升子序列
- 【洛谷】1637 三元上升子序列 树状数组
- 三元上升子序列 洛谷p1637
- LuoguP1637 三元上升子序列
- 洛谷 P1637 三元上升子序列(树状数组)
- 洛谷P1637 三元上升子序列(树状数组)
- 洛谷P1637 三元上升子序列Thair(树状数组)
- 三元上升子序列(二重顺序对与乘法原理的应用)
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- NavigationView 的使用
- JNI
- 流量分析常见指标
- Spark RDD
- C#treeView控件单击事件选中节点滞后问题解决方法
- 【洛谷1637】三元上升子序列
- BZOJ 3631: [JLOI2014]松鼠的新家 树上差分
- to_date使用方法
- centos下安装Python包管理工具pip的方法总结
- Unity5.x 音效 AudioMixer
- Vue过渡(动画)- transition组件
- Mybatis学习笔记(一)-Mybatis入门篇
- CentOS6与CentOS7启动流程
- 《嫌疑人X的献身》