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

来源:互联网 发布:vscode markdown 插件 编辑:程序博客网 时间:2024/06/05 20:47

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
25 5 0 0 5121236 2 2 1000000000 612
 

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

Source
2009 Multi-University Training Contest 6 - Host by WHU

位置用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-6typedef 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 1000000007int 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-6typedef 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 1000000007int 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 0
原创粉丝点击