2015南阳CCPC C - The Battle of Chibi DP
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
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
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 2015 南阳ccpc The Battle of Chibi (uestc 1217)
题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大
Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...
- 2015南阳CCPC L - Huatuo's Medicine 水题
L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous ...
- 2015南阳CCPC H - Sudoku 暴力
H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best cou ...
- 2015南阳CCPC G - Ancient Go 暴力
G - Ancient Go Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yu Zhou likes to play Go wi ...
随机推荐
- Android-监听sdcard状态
public class MyService extends Service { private static final String TAG = "MyService"; Fi ...
- WPF应用加载图片URI指定需要注意的地方
应用程序(.exe)加载图片: 可以省略"pack://application:,,," 打头,因为系统运行时需要的图片文件在Exe程序集(组合体)中:譬如: <Image ...
- NSThread 多线程相关
1.下面的代码,有2点需要注意,1>就是 就是thread:所传得参数,这里传得的是nsarray 当然也可以传其他的类型.2> [self performSelectorOnMainTh ...
- python 本地文档查看
本地安装Python文档本地查看,在命令行中运行: python -m pydoc -p 1234 在浏览器中访问如下链接,就可以访问到本地文档: http://localhost:1234/ 本地文 ...
- [算法] 选择排序 Selection sort
选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然 ...
- POJ No.3680 Intervals
2016-06-01 22:01:39 题目链接: POJ No.3680 Intervals 题目大意: 给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大 解法: 费用流 建模 ...
- Codeforces 372
A (被装的袋鼠不可以装的袋鼠)贪心,排序,从n/2分成两部分. B 好一道前缀和的题目. C 标准算法不难想,m^2的算法见http://codeforces.com/blog/entry/9907 ...
- Java NIO 缓冲区学习笔记
Buffer其实就是是一个容器对象,它包含一些要写入或者刚读出的数据.在NIO中加入Buffer对象,体现了新库与原I/O的一个重要区别.在面向流的I/O中,您将数据直接写入或者将数据直接读到Stre ...
- 第三百零三天 how can I 坚持
今天年会,运气还不错,竟然中了个小奖,一个榨汁机,已经很满足了. 今天加上了她,感觉挺合适,就怕一句话聊不来就带搭不理的了.她很好,懂得知足,不攀比. 弟弟今天把房子首付交了,把贷款办完就算安心了,目 ...
- angular的注入实现
angular需要对用户的传入函数进行静态分析,抽取当中的依赖,才能工作.因此用户的函数,包括控制器函数,工厂函数,服务函数与$watch回调都只是一个模板,用于取toString,真正运行的是编译后 ...