第十四个目标(dp + 树状数组 + 线段树)
Accept: 17 Submit: 35 Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
目暮警官、妃英里、阿笠博士等人接连遭到不明身份之人的暗算,柯南追踪伤害阿笠博士的凶手,根据几起案件现场留下的线索发现凶手按照扑克牌的顺序行凶。在经过一系列的推理后,柯南发现受害者的名字均包含扑克牌的数值,且扑克牌的大小是严格递增的,此外遇害者与毛利小五郎有关。
为了避免下一个遇害者的出现,柯南将可能遭到暗算的人中的数字按关联程度排列了出来,即顺序不可改变。柯南需要知道共有多少种可能结果,满足受害人名字出现的数字严格递增,但是他柯南要找出关键的证据所在,所以这个任务就交给你了。
(如果你看不懂上面在说什么,这题是求一个数列中严格递增子序列的个数。比如数列(1,3,2)的严格递增子序列有(1)、(3)、(2)、(1,3)、(1,2),共5个。长得一样的但是位置不同的算不同的子序列,比如数列(3,3)的答案是2。)
Input
多组数据(<=10),处理到EOF。
第一行输入正整数N(N≤100 000),表示共有N个人。
第二行共有N个整数Ai(1≤Ai≤10^9),表示第i个人名字中的数字。
Output
每组数据输出一个整数,表示所有可能的结果。由于结果可能较大,对1 000 000 007取模后输出。
Sample Input
Sample Output
Source
福州大学第十三届程序设计竞赛
#include<iostream>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
using namespace std;
const int MAXN = ;
int dp[MAXN];
int a[MAXN], b[MAXN];
int tree[MAXN];
typedef long long LL;
const int MOD = 1e9 + ;
int lowbit(int x){return x & (-x);}
void update(int i, int x){
while(i < MAXN){
tree[i] += x;
tree[i] %= MOD;
i += lowbit(i);
}
}
int sum(int i){
int ans = ;
while(i > ){
ans += tree[i];
ans %= MOD;
i -= lowbit(i);
}
return ans;
}
int main()
{
int n;
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(a, a + n);
memset(dp, , sizeof(dp));
memset(tree, , sizeof(tree));
LL ans = ;
int k = unique(a, a + n) - a;
for (int i = ; i < n; i++) {
int p = lower_bound(a, a + k, b[i]) - a + ;
dp[i] = sum(p - ) + ;
update(p, dp[i]);
}
for(int i = ; i < n; i++){
ans += dp[i];
ans %= MOD;
}
printf("%lld\n", ans % MOD);
}
return ;
}
比赛的时候刚开始想着用优先队列优化的,没什么用,还是超时;
代码:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int MAXN = ;
int dp[MAXN];
int a[MAXN];
int tree[MAXN];
typedef long long LL;
const int MOD = 1e9 + ;
struct Node{
int v,p;
friend bool operator < (Node a, Node b){
return a.v > b.v;
}
};
int main()
{
int n;
while (~scanf("%d", &n)) {
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
}
memset(dp, , sizeof(dp));
LL ans = ;
priority_queue<Node>Q, Q1;
Node x;
for (int i = ; i <= n; i++) {
dp[i] = ;
x.v = a[i];x.p = i;
Q.push(x);
while(Q.top().v < a[i]){
dp[i] = (dp[i] + dp[Q.top().p])%MOD;
Q1.push(Q.top());
Q.pop();
}
while(!Q1.empty()){
Q.push(Q1.top());
Q1.pop();
}
}
for(int i = ; i <= n; i++){
ans += dp[i];
ans %= MOD;
}
printf("%lld\n", ans % MOD);
}
return ;
}
线段树又写了下,发现还是用二分好,用了结构体各种错。。。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
const int MOD = 1e9 + ;
const int MAXN = ;
typedef long long LL;
LL tree[MAXN << ];
LL dp[MAXN];
void pushup(int root){
tree[root] = (tree[ll] + tree[rr]) % MOD;
}
void update(int root, int l, int r, int a, int b){
int mid = (l + r) >> ;
if(a == l && a == r){
tree[root] += b;
return;
}
if(mid >= a)
update(lson, a, b);
else
update(rson, a, b);
pushup(root);
}
LL ans;
void query(int root, int l, int r, int a, int b){
int mid = (l + r) >> ;
if(l >= a && r <= b){
ans += tree[root];
ans %= MOD;
return;
}
LL ans = ;
if(mid >= a)
query(lson, a, b);
if(mid < b)
query(rson, a, b);
return ;
}
int a[MAXN], b[MAXN];
int main(){
int N;
while(~scanf("%d", &N)){
for (int i = ; i < N; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(a, a + N);
memset(tree, , sizeof(tree));
memset(dp, , sizeof(dp));
int k = unique(a, a + N) - a;
for(int i = ; i < N; i++){
int p = lower_bound(a, a + k, b[i]) - a + ;
ans = ;
query(, , N + , , p - );
dp[i] = ans + ;
dp[i] %= MOD;
update(, , N + , p, dp[i]);
}
printf("%lld\n", tree[] % MOD);
}
return ;
}
第十四个目标(dp + 树状数组 + 线段树)的更多相关文章
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 51nod 1680区间求和 (dp+树状数组/线段树)
不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- 数据结构--树状数组&&线段树--基本操作
随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...
- BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...
随机推荐
- HDOJ/HDU 2544 最短路---dijkstra算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...
- python (3):wxPython打包app,报错
1,打包app报错 如图: 使用py2app,mac下打包成app.异常.程序直接退出. 没有详细的错误信息,client程序直接崩溃了. 2.原因 代码没有几行: #!/usr/bin/python ...
- Unity发送短信
闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码! 1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧! 2,界面极其 ...
- Tomcat 原理篇
TOMCAT 原理篇一.Tomcat 组成(Tomcat 由以下组件组成) 1.server a) Server是一个Catalina Servlet容器: b) Server 可以包含一个或多个se ...
- jQuery制作焦点图(轮播图)
焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- iOS程序的加载过程
1.执行main函数2.执行UIApplicationMain函数1> 创建一个UIApplication对象(UIApplication是整个程序的象征)一个应用只有一个application ...
- UTF8转GB2312(UTF8解码)
小弟C++上手没多久,代码不严谨之处敬请见谅.英语也不是很好,有的是直接使用的拼音. string MyUTF_8toGB2312(string str) { ,,str.c_str(),-,NULL ...
- 《第一行代码》学习笔记3-活动Activity(1)
1.活动-一种可以包含用户界面的组件,用于和用户进行交互. <Button android:id="@+id/button_1" android:layout_width=& ...
- Merge Sorted Array 合并数组并排序
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- iOS 8以上的设置的跳转
iOS8以上的系统应用可以与设置进行深层的交互,用户可以根据APP的需要进行对应的权限的设置. 现在大多数的APP依旧仅仅是弹出一个包含操作指令的警示窗口,如“进入设置>隐私>位置> ...