标签:
C - The Battle of Chibi
Time Limit: 1 Sec
Memory Limit: 256 MB
无
2 3 2 1 2 3 3 2 3 2 1
Sample Output
Case #1: 3
Case #2: 0
题意
给你n个数,求长度为m的上升子序列有多少个
题解:
n^3就直接暴力就好了,但是会TLE
于是用树状数组优化到n^2logn就好了
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <stack> #include <bitset> #define INF 1000000005 #define eps 1e-12 #define PI acos(-1.0) #define LL long long using namespace std; const int maxn = 1005; const int mod = 1000000007; inline long long read() { long long x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int a[maxn], b[maxn], c[maxn][maxn], n, m, dp[maxn][maxn]; inline void Init() { for (int i = 1; i <= n; i++) { a[i] = b[i] = 0; for (int j = 1; j <= n; j++) dp[i][j] = c[i][j] = 0; } return; } inline int Lowbit(int x) { return x & (- x); } inline void Update(int l, int pos, int key) { while(pos <= n) { c[l][pos] = (c[l][pos] + key); while(c[l][pos]>=mod) c[l][pos] -= mod; pos = pos + Lowbit(pos); } return; } inline int Sum(int l, int pos) { int temp = 0; while(pos) { temp = (temp + c[l][pos]); while(temp >= mod)temp -= mod; pos = pos - Lowbit(pos); } return temp; } int main() { int T, cas = 0; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); Init(); for (int i = 1; i <= n; i++) { a[i]=read(); //scanf("%d", &a[i]); b[i] = a[i]; } sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b; // cout << -1 << endl; for (int i = 1; i <= n; i++) { // cout << a[i] << endl; dp[i][1] = 1; for (int j = 2; j <= i; j++) dp[i][j] = Sum(j - 1, a[i] - 1); for (int j = 1; j <= i; j++) Update(j, a[i], dp[i][j]); } long long ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + dp[i][m]); while(ans>=mod) ans-=mod;// % mod; //cout << dp[i][m] << endl; } printf("Case #%d: %lld\n", ++cas, ans); } return 0; }
2015南阳CCPC C - The Battle of Chibi DP
标签:
原文地址:http://www.cnblogs.com/qscqesze/p/4899331.html