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 ...
随机推荐
- 【Mac】Mac键盘实现Home, End, Page UP, Page DOWN
* Home键=Fn+左方向 * End键=Fn+右方向 * PageUP=Fn+上方向 * PageDOWN=Fn+下方向 * 向后删除=Fn+delete * Find ...
- 如何在Android开发中让你的代码更有效率
最近看了Google IO 2012年的一个视频,名字叫做Doing More With Less: Being a Good Android Citizen,主要是讲如何用少少的几句代码来改善And ...
- XCode修改工程名注意
以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...
- CSS、CSS2和CSS3选择器总结(全部选择器种类及其优先级)
选择器种类罗列: 1.基础的选择器 选择器 含义 示例 * 通用元素选择器,匹配任何元素 * { margin:0; padding:0; } E 标签选择器,匹配所有使用E标签的元素 p { fon ...
- Unity中Instantiate一个prefab时需要注意的问题
在调用Instantiate()方法使用prefab创建对象时,接收Instantiate()方法返回值的变量类型必须和声明prefab变量的类型一致,否则接收变量的值会为null. 比如说,我在 ...
- TP分析
http://blog.csdn.net/l627859442/article/details/7633457 http://blog.chinaunix.net/uid-27717694-id-37 ...
- Sqlserver作业-手把手带你体验
所谓Sql Server作业就是按照规定的时间执行指定的脚本,如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理-SQL Server代 ...
- linux下无法安装VMware的解决方法
在Reahat下安装VMware-Player-6.0.1-1379776.x86_64.bundle,结果却提示 Extracting VMware Installer...done.NOT_REA ...
- [JS代码]如何判断ipad或者iphone是否为横屏或者竖屏 - portrait或者landscape
//判断横屏或者竖屏 function orient() { //alert('gete'); if (window.orientation == 0 || window.orientation == ...
- 对stack概念的理解与应用
stack,中文翻译做“栈”,特点就是先进后出,后进先出. 像盖房子一样,新的数据总是被放在上层,若要取数据,就像拆房子,不要太暴力的方式,就要从顶层一层层往下拆. stack有几种操作,push—— ...