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:
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
Input contains several test cases. Each test case begins with a line containing a number N, followed by a line containing N numbers.
Output
Output the amount of Crazy Thairs in each sequence.
Sample Input
5
1 2 3 4 5
7
2 1 3 4 5 7 6
7
1 2 3 4 5 6 7
Sample Output
1
4
21
题意:
寻找上升五元组的个数
思路:
本质上是树状数组优化dp。
状态就是dp[i][j],到了第j个数字的i元上升组。
从比j小的i - 1元上升组转移过来,那么这个求和的过程可以用树状数组维护。
比较坑的就是得用大整数,而且复杂度卡的很死,得用10000进制加法,于是找了个10000进制加法的代码改了一下。
#pragma GCC optimize(2)
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 10;
const int BASE = 10000;
struct bign{
int d[maxn], len;
void clean() { while(len > 1 && !d[len-1]) len--; }
// bign() { memset(d, 0, sizeof(d)); len = 1; }
// bign(int num) { *this = num; }
// bign(char* num) { *this = num; }
bign():len(0) {}
bign(int n):len(0) {
for( ; n > 0; n /= BASE) d[len++] = n%BASE;
}
bign operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bign operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
// bign operator + (const bign& b){
// bign c = *this; int i;
// for (i = 0; i < b.len; i++){
// c.d[i] += b.d[i];
// if (c.d[i] >= BASE) c.d[i]%=BASE, c.d[i+1]++;
// }
// while (c.d[i] >= BASE) c.d[i++]%=BASE, c.d[i]++;
// c.len = max(len, b.len);
// if (c.d[i] && c.len <= i) c.len = i+1;
// return c;
// }
bign operator + (const bign& b) { //不使用 = 运算
bign c;
int i, carry = 0;
for(i = 0; i < this->len || i < b.len || carry > 0; ++i) {
if(i < this->len) carry += this->d[i];
if(i < b.len) carry += b.d[i];
c.d[i] = carry%BASE;
carry /= BASE;
}
c.len = i;
return c;
}
bign operator - (const bign& b){
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bign operator * (const bign& b)const{
int i, j; bign c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bign operator / (const bign& b){
int i, j;
bign c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator % (const bign& b){
int i, j;
bign a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bign operator += (const bign& b){
*this = *this + b;
return *this;
}
bool operator <(const bign& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bign& b) const{return b < *this;}
bool operator<=(const bign& b) const{return !(b < *this);}
bool operator>=(const bign& b) const{return !(*this < b);}
bool operator!=(const bign& b) const{return b < *this || *this < b;}
bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
void Print() {
if(len == 0) {cout << "0" << endl; return ;}
cout << d[len - 1];
for(int i = len - 2; i >= 0; --i) {
for(int j = BASE/10; j > 0; j /= 10) {
cout << d[i] / j % 10;
}
}
cout << endl;
}
};
istream& operator >> (istream& in, bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bign& x)
{
out << x.str();
return out;
}
const int maxm = 5e4 + 7;
int a[maxm];
bign c[6][maxm];
int n;
struct Node
{
int x,id;
}nodes[maxm];
bool cmp(Node a,Node b)
{
return a.x < b.x;
}
void add(int num,int x,bign v)
{
while(x <= n)
{
c[num][x] += v;
x += x & (-x);
}
}
bign query(int num,int x)
{
bign res = 0;
while(x)
{
res += c[num][x];
x -= x & (-x);
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> n && n)
{
for(int i = 1;i <= n;i++)
{
cin >> nodes[i].x;
nodes[i].id = i;
}
sort(nodes + 1,nodes + 1 + n,cmp);
int cnt = 0;
a[nodes[1].id] = ++cnt;
for(int i = 2;i <= n;i++)
{
if(nodes[i].x == nodes[i - 1].x)a[nodes[i].id] = cnt;
else a[nodes[i].id] = ++cnt;
}
for(int i = 1;i <= 5;i++)
{
for(int j = 1;j <= n;j++)
{
c[i][j] = 0;
}
}
for(int i = 1;i <= n;i++)
{
int x = a[i];
add(1,x,1);
for(int j = 2;j <= 5;j++)
{
add(j,x,query(j - 1,x - 1));
}
}
bign ans = query(5,n);
ans.Print();
}
return 0;
}