HDU3030最长上升子序列个数+树状数组求和+二分优化

Increasing Speed Limits

Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 517    Accepted Submission(s): 259


Problem Description
You were driving along a highway when you got caught by the road police for speeding. It turns out that they\'ve been following you, and they were amazed by the fact that you were accelerating the whole time without using the brakes! And now you desperately need an excuse to explain that.

You've decided that it would be reasonable to say "all the speed limit signs I saw were in increasing order, that\'s why I've been accelerating". The police officer laughs in reply, and tells you all the signs that are placed along the segment of highway you drove, and says that's unlikely that you were so lucky just to see some part of these signs that were in increasing order.

Now you need to estimate that likelihood, or, in other words, find out how many different subsequences of the given sequence are strictly increasing. The empty subsequence does not count since that would imply you didn't look at any speed limits signs at all!

For example, (1, 2, 5) is an increasing subsequence of (1, 4, 2, 3, 5, 5), and we count it twice because there are two ways to select (1, 2, 5) from the list.
 

Input
The first line of input gives the number of cases, N. N test cases follow. The first line of each case contains n, m, X, Y and Z each separated by a space. n will be the length of the sequence of speed limits. m will be the length of the generating array A. The next m lines will contain the m elements of A, one integer per line (from A[0] to A[m-1]).

Using A, X, Y and Z, the following pseudocode will print the speed limit sequence in order. mod indicates the remainder operation.

for i = 0 to n-1
print A[i mod m]
A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

Note: The way that the input is generated has nothing to do with the intended solution and exists solely to keep the size of the input files low.

1 ≤ m ≤ n ≤ 500 000
 

Output
For each test case you should output one line containing "Case #T: S" (quotes for clarity) where T is the number of the test case and S is the number of non-empty increasing subsequences mod 1 000 000 007.
 

Sample Input
  
  
2 5 5 0 0 5 1 2 1 2 3 6 2 2 1000000000 6 1 2
 

Sample Output
  
  
Case #1: 15 Case #2: 13
 

Source

位置用map映射一下跑了9000ms
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 500005
#define MOD 1000000007
int num[N], A[N], c[N], seq[N], cnt;
//基础树状数组更新
inline int lowbit(int x)
{
    return x & -x;
}
inline void update(int pos, int val)
{
    for (int i = pos; i <= cnt; i += lowbit(i))
    {
        c[i] += val;
        if (c[i] >= MOD)
            c[i] %=MOD;
    }
}
inline int getsum(int pos)
{
    int s = 0;
    for (int i = pos; i > 0; i -= lowbit(i))
    {
        s += c[i];
        if (s >= MOD)
            s %= MOD;
    }
    return s;
}
int ca = 1;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T, n, m;
    long long X, Y, Z, ans;
    scanf("%d", &T);
    while (T--)
    {
        ans = 0;
        std::map<int, int> mp;
        scanf("%d%d%I64d%I64d%I64d", &n, &m, &X, &Y, &Z);
        memset(c, 0, sizeof(c));
        for (int i = 0; i < m; i++)
        {
            scanf("%d", &A[i]);
        }
        //根据题意构造seq,内容与num一样,不过错位一位
        for (int i = 0; i < n; i++)
        {
            seq[i]=num[i + 1] = A[i % m];
            A[i % m] = (X * A[i % m] + Y * (i + 1)) % Z;
        }
        //排序并离散化
        sort(num + 1, num + 1 + n);
        cnt = unique(num + 1, num + 1 + n) - (num + 1);
        //得到离散化后的数组大小,存在num里
        int pos;
        for(int i=1;i<=cnt;i++)
        {
            mp[num[i]]=i-1;
        }
        for (int i = 0; i < n; i++)
        {
            //得到seq在num数组的位置-1
            pos = mp[seq[i]];
            long long x = getsum(pos) + 1;//得到到位置pos共有多少递增序列
            ans += x;
            if (ans > MOD)
                ans %= MOD;
            update(pos + 1, x);//更新数组
        }
        printf("Case #%d: %I64d\n", ca++, ans);
    }
    return 0;
}

二分优化2000+ms
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 500005
#define MOD 1000000007
int num[N], A[N], c[N], seq[N], cnt;
//基础树状数组更新
inline int lowbit(int x)
{
    return x & -x;
}
inline void update(int pos, int val)
{
    for (int i = pos; i <= cnt; i += lowbit(i))
    {
        c[i] += val;
        if (c[i] >= MOD)
            c[i] %=MOD;
    }
}
inline int getsum(int pos)
{
    int s = 0;
    for (int i = pos; i > 0; i -= lowbit(i))
    {
        s += c[i];
        if (s >= MOD)
            s %= MOD;
    }
    return s;
}
int ca = 1;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T, n, m;
    long long X, Y, Z, ans;
    scanf("%d", &T);
    while (T--)
    {
        ans = 0;
        // std::map<int, int> mp;
        scanf("%d%d%I64d%I64d%I64d", &n, &m, &X, &Y, &Z);
        memset(c, 0, sizeof(c));
        for (int i = 0; i < m; i++)
        {
            scanf("%d", &A[i]);
        }
        //根据题意构造seq,内容与num一样,不过错位一位
        for (int i = 0; i < n; i++)
        {
            seq[i]=num[i + 1] = A[i % m];
            A[i % m] = (X * A[i % m] + Y * (i + 1)) % Z;
        }
        //排序并离散化
        sort(num + 1, num + 1 + n);
        cnt = unique(num + 1, num + 1 + n) - (num + 1);
        //得到离散化后的数组大小,存在num里
        int pos;
        for (int i = 0; i < n; i++)
        {
            //得到seq在num数组的位置-1
            pos = lower_bound(num + 1, num + cnt + 1, seq[i]) - (num + 1);
            long long x = getsum(pos) + 1;//得到到位置pos共有多少递增序列
            ans += x;
            if (ans > MOD)
                ans %= MOD;
            update(pos + 1, x);//更新数组
        }
        printf("Case #%d: %I64d\n", ca++, ans);
    }
    return 0;
}



 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值