C - The Battle of Chibi

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

Description

Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army. But all generals and soldiers of Cao Cao were loyal, it's impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao's opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.

Input

The first line of the input gives the number of test cases, T(1≤100). T test cases follow.

Each test case begins with two numbers N(1≤N≤103) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤109) indicates the value in Cao Cao's opinion of the ith information in happening order.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by 1000000007(109+7).

Sample Input

2
3 2
1 2 3
3 2
3 2 1

Sample Output

Case #1: 3
Case #2: 0

HINT

题意

给你n个数,求长度为m的上升子序列有多少个

题解:

n^3方法:dp[i][j]表示结尾为i,长度为j的有多少个,转移是O(n)的、

那么我们用树状数组优化转移过程,优化到n^2logn就可以AC了

代码:

#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 = ;
const int mod = ;
inline long long read()
{
long long x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[maxn], b[maxn], c[maxn][maxn], n, m, dp[maxn][maxn]; inline void Init()
{
for (int i = ; i <= n; i++)
{
a[i] = b[i] = ;
for (int j = ; j <= n; j++)
dp[i][j] = c[i][j] = ;
}
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 = ;
while(pos)
{
temp = (temp + c[l][pos]);
while(temp >= mod)temp -= mod;
pos = pos - Lowbit(pos);
}
return temp;
} int main()
{
int T, cas = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
Init();
for (int i = ; i <= n; i++)
{
a[i]=read();
//scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + , b + + n);
for (int i = ; i <= n; i++)
a[i] = lower_bound(b + , b + + n, a[i]) - b;
// cout << -1 << endl;
for (int i = ; i <= n; i++)
{
// cout << a[i] << endl;
dp[i][] = ;
for (int j = ; j <= i; j++)
dp[i][j] = Sum(j - , a[i] - );
for (int j = ; j <= i; j++)
Update(j, a[i], dp[i][j]);
}
long long ans = ;
for (int i = ; 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 ;
}

2015南阳CCPC C - The Battle of Chibi DP的更多相关文章

  1. 2015南阳CCPC C - The Battle of Chibi DP树状数组优化

    C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...

  2. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  3. 2015南阳CCPC D - Pick The Sticks 背包DP.

    D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...

  4. 2015 南阳ccpc The Battle of Chibi (uestc 1217)

    题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...

  5. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

  6. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

  7. 2015南阳CCPC L - Huatuo's Medicine 水题

    L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous ...

  8. 2015南阳CCPC H - Sudoku 暴力

    H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best cou ...

  9. 2015南阳CCPC G - Ancient Go 暴力

    G - Ancient Go Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yu Zhou likes to play Go wi ...

随机推荐

  1. Android:真机调试,不显示logcat的解决方案

    有时做开发的时候,用真机测试,总是看不到logcat信息 .原因是系统默认关闭了log,需要将其打开. 解决办法如下:   在拨号界面输入*#*#2846579#*#* ,然后系统会自动弹出一个菜单, ...

  2. C# 委托总结

    总结 委托的本质: 委托是一种特殊的数据类型,它表示某种特定类型的函数,并且可以表示多个函数,将这些函数串联起来.使用委托就好像函数调用一样. 委托实质上是一个类,编译器会根据关键字delegate自 ...

  3. mybatis返回HashMap结果类型与映射

    <!-- 返回HashMap结果 类型--> <!-- 如果想返回JavaBean,只需将resultType设置为JavaBean的别名或全限定名 --> <!-- T ...

  4. UVA 11600-Masud Rana(状压,概率dp)

    题意: 有n个节点的图,开始有一些边存在,现在每天任意选择两点连一条边(可能已经连过),求使整个图联通的期望天数. 分析: 由于开始图可以看做几个连通分量,想到了以前做的一个题,一个点代表一个集合(这 ...

  5. 【LeetCode 230】Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  6. 《Python 学习手册4th》 第十一章 赋值、表达式和打印

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  7. 022 UFT虚拟对象

    虚拟对象: 程序中那些行为标准类型对象的对象,但不能被QTP识别,则可把这些对象类型称为虚拟对象.并且映射到某类标准对象,例如button,check box等,QTP在测试过程中就会对这些虚拟对象模 ...

  8. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_labels(self, locator)

    def get_selected_list_labels(self, locator): """Returns the visible labels of selecte ...

  9. DataTable转List<Model>通用类【实体转换辅助类】

    /// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...

  10. new trip

    离开YY已经快一周了,特别感谢以前的老大姚冬和朱云峰,从他俩身上学到了很多.这个决定也经过了很长的纠结,不想再做个犹豫不决的人,所以最后还是坚定了最初的信念,也算是对半年前自己的一个完好交代,以防将来 ...