[POJ3378]Crazy Thairs

Problem

给你一个数列,让你求由五个元素组成的顺序对的个数。

Solution

DP:用DP[i][j]表示把第j个作为五元组中第i个的方案数
则DP[i][j]=sum{DP[k][j-1]} (k < i && a[k] < a[i])
所以只要用树状数组维护最小值即可
但需要离散化和高精度。

Notice

代码较长,比较容易写错

Code


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define lowbit(x) (x & -x)
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 50000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int now, T[N + 5];
struct BigInteger
{
    int len;
    int val[10];
    /*      转换      */
    BigInteger(int x = 0)
    {
        I_TO_B(x);
    }
    void I_TO_B(int x)
    {
        len = 0;
        memset(val, 0, sizeof(val));
        while(x)
        {
            val[len++] = x % 100000000;
            x /= 100000000;
        }
    }
    void print()
    {
        per(i, len - 1, 0) printf("%d", val[i]);
        putchar('
');
    }
    friend BigInteger operator +(BigInteger x, BigInteger y)
    {
        int len = x.len > y.len ? x.len : y.len;
        BigInteger ans;
        rep(i, 0, len - 1)
        {
            ans.val[i] += x.val[i] + y.val[i];
            ans.val[i + 1] += ans.val[i] / 100000000;
            ans.val[i] %= 100000000;
        }
        if (ans.val[len] != 0) len++;
        ans.len = len;
        return ans;
    }
    friend BigInteger operator +(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x + y;
    }
    friend void operator +=(BigInteger &x, BigInteger y)
    {
        x = x + y;
    }
    friend void operator +=(BigInteger &x, int t)
    {
        x = x + t;
    }
    friend void operator ++(BigInteger &x)
    {
    	x += 1;
	}
};
struct Node
{
	int val, id;
}x[N + 5];
int cmp(Node X, Node Y)
{
	return X.val < Y.val;
}
struct node
{
	BigInteger val[6][N + 5];
	void build(int l, int r)
	{
		rep(i, 1, 5)
			rep(j, l, r) val[i][j] = 0;
	}
	BigInteger query(int x, int y)
	{
		BigInteger ans = 0;
		while(y)
		{
			ans += val[x][y];
			y -= lowbit(y);
		}
		return ans;
	}
	void modify(int x, int y, BigInteger v)
	{
		while (y <= now)
		{
			val[x][y] += v;
			y += lowbit(y);
		}
	}
}BIT;
int sqz()
{
	int n;
	while(~scanf("%d", &n))
	{
		BIT.build(1, N);
		rep(i, 1, n) x[i].val = read(), x[i].id = i;
		sort(x + 1, x + n + 1, cmp);
		now = 0;
		rep(i, 1, n)
		{
			if (x[i].val != x[i - 1].val) now++;
			T[x[i].id] = now;
		}
		rep(i, 1, n)
		{
			BIT.modify(1, T[i], 1);
			rep(j, 2, 5)
				BIT.modify(j, T[i], BIT.query(j - 1, T[i] - 1));
		}
		BIT.query(5, now).print();
	}
}

原文地址:https://www.cnblogs.com/WizardCowboy/p/7608483.html