题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长度为j的上升子序列个数,但是dp数组是在树状数组的update函数中进行更新. update(i, val, j)函数表示在i的位置加上val,更新dp[i][j]. sum(i, j)就是求出末尾数字小于等于i 且长度为j的子序列有多少个. //#pragma comment(linker, "/…
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k](1<=k<j),由于要求前缀和,可以用树状数组优化 #include<bits/stdc++.h> #define lowbit(x) x&(-x) typedef long long ll; const int N=1e5+3; ll dp[12][N]; using n…
设$f(i, j)$表示以$i$结尾的,长为$j$的上升子序列的数量 转移时用树状数组维护即可 复杂度为$O(kn \log n)$ 注:特判0 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> namespace remoon { #define ri register int #define ll long long #define tpr temp…
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1137    Accepted Submission(s): 477 Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D…
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in other words, a string s* of length n, consisting of characters "(" and ")". Sereja needs to answer m queries, each of them is describe…
树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #i…
树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ^ xr[l] 相当于l + 1到r出现奇数次的数的异或,sum(r) ^ sum(l)表示l + 1到r每个数只考虑一次的异或,则两者异或为出现偶数次的数的异或. #include<cstdio> #include<iostream> #include<cstdlib>…
考试最后半个小时才做这道题.十分钟写了个暴力还写挂了..最后默默输出n.菜鸡一只. 这道题比较好看出来是动规.首先我们要明确一点.因为能拔高长度任意的一段区域,所以如果从i开始拔高,那么一直拔高到n比一直拔高到j更优.因为j~n变高了对于答案是有利的. 我们定义f[i][j]表示到第i个点前面拔高j次的最大剩余数.在i点的高度为hei[i]+j(因为前面拔高j次,最终都会拔高到n).所以我们要找在高度小于hei[i]+j,次数小于j里面最大剩余数+1去更新.而找这个有限制的二维前缀最大值,可以用…
题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$-1$的数量为$cnt$,可以等效成求长度为$cnt$的排列的逆序对期望.设$dp[i]$为长度为$i$的全排列的逆序对期望,有$dp[i]=dp[i-1]+$$\frac{i-1}{2}$,可以理解成在原$dp[i-1]$的基础上,数值$i$对每个长度为$i-1$的排列产生$\sum_{t=1}^{…
题中有两种操作,第一种把某个位置的字母修改,第二种操作查询与[L, R]内与给出字符串循环起来以后对应位置的字母相同的个数.给出的字符串最大长度是10. 用一个四维树状数组表示 cnt[ATCG的编号][可能的给出字符串的长度][原字符中这个位置在循环中的位置][从开始到这个位置] = 出现的次数. 然后对于一开始的字符串,暴力更新每一种情况. 对于1操作,对于每一种可能的循环节长度,把这个位置的贡献清除,然后换成要填入的字母 对于2操作,(l+i)表示在原字符串的位置,(l+i-1)%len表…
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Petya has an array aa consisting of nn integers. He has learned partial sums recently, and now he can calculate the sum o…
http://www.lightoj.com/volume_showproblem.php?problem=1085 题意:求一个序列的递增子序列个数. 思路:找规律可以发现,某个数作为末尾数的种类数为所有比它小的数的情况+1.使用树状数组查找即可. C++11 的auto在Lightoj上不能用/.\ /** @Date : 2016-12-01-21.58 * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : https://github.…
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每个点都有权值,然后从(x, y)引x轴的垂线,然后构成一个三角形,三个顶点分别是(0, 0),(x, 0)和(x, y).求三角形内点的权值和,包括边界,n的范围是1000,m的范围是100000,说起来也比较坑..学弟n*m的复杂度竟然水过去了,目测比赛数据比较水..不过我挂到我们OJ上给了一组随…
题目传送 做法 对于每个人,inc为x,pref为y:对于每道菜,p和s为x,b为y 于是根据题意有\[p[i]<=x<=s[i]\]\[p[i]+b[i]<=x+y\]\[p[i]-b[i]<=x-y\] 把所有出现的点都离散化一下,然后开始扫x轴 对于x+y和x-y这两个函数,分别开一个树状数组去维护合法点的个数 #include <cstdio> #include <cctype> #include <iostream> #include…
转自:https://blog.csdn.net/my_sunshine26/article/details/79831362 题目大意: i从1开始 基本思路: 完全没思路,所以上来就二分,果不其然卡死在了第7个样例,然后认为是贪心,苦思冥想未果,然后就看了答案. 说是主席树模板题,但是可以用巧方法过了,大部分用的都是树状数组好像,然后想了想也没想出来用树状数组能好到那里去, 然后参考了下答案思路,嗯,这绝对不是我能想到的,难受,就很难受. 正确思路: 1.先记录满足i<=a[j]的最大下标i…
For the given sequence with n different elements find the number of increasing subsequences with k + 1elements. It is guaranteed that the answer is not greater than 8·1018. Input First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10)…
C. Subsequences     For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018. Input   First line contain two integer values n and k (1…
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem/C Description For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed tha…
相当于将线段划分成两个集合使集合内线段不相交,并且可以发现线段相交等价于逆序对.也即要将原序列划分成两个单增序列.由dilworth定理,如果存在长度>=3的单减子序列,无解,可以先判掉. 这个时候有两种显然的暴力. 将点集划分成两部分使内部无边显然就是二分图,于是第一种暴力是在逆序对之间连边,答案即为2连通块个数,因为每个连通块都可以交换黑白点.问题在于暴力连边是n2的,而显然实际有用的边其实只有O(n)条.考虑这样一种连边方式:每个点向后缀最小值.前缀第一个比他大的点连边.瞎归纳归纳就可以证…
题意:给你一颗树,有q次询问,每次询问给你若干个点,这些点可以最多分出m组,每组要满足两个条件:1:每组至少一个点,2:组内的点不能是组内其它点的祖先,问这样的分组能有多少个? 思路:https://blog.csdn.net/BUAA_Alchemist/article/details/86765501 代码: #include <bits/stdc++.h> #define LL long long #define lowbit(x) (x & (-x)) using namesp…
大意: 给定n*m矩阵, k条链, 链上每个点有权值, 每次操作可以关闭或打开一条链或询问一个子矩阵内未关闭的权值和. 关键询问操作比较少, 可以枚举每条链, 暴力算出该条链对每个询问的贡献. 最后再按询问顺序切换链的状态, 只记录打开的链的贡献即可. #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #inclu…
You are given an array $a_1,a_2,…,a_n$. All $a_i$ are pairwise distinct. Let's define function $f(l,r)$ as follows: let's define array $b_1,b_2,…,b_{r-l+1}$, where $b_i=a_{l-1+i}$; sort array $b$ in increasing order; result of the function $f(l,r)$ i…
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is…
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用dp 复杂度 大概是 O(n ^ 2 * k) T了 但是 思路还是一样的 只是用树状数组 优化了一下 第三层循环 dp[i][j] 表示 第 i 个数 长度为 j 时 那么 dp[i][j] 的状态转移就是 ∑(arr[i] > arr[k] ? : dp[k][j - 1] ) AC代码 #in…
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a piece of squared paper and wrote all distinct non-empty non-decreasing subsequences of sequence a. Then for each sequence written on the squared paper, S…
题意 给定$n$个数,求最长上升子序列的方案数 根据数据范围要求是$O(n\log n)$ 朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i=v_i+v_j,(f_i=f_j+1)$,利用lis的$O(n\log n)$树状数组做法同时维护长度和方案数 从通酱博客里还看到更详尽的解释:stackoverflow 时间复杂度$O(n\log n)$ 代码 #include <bits/stdc++.h> using namespace…
题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Anton's favourite geometric figures are regular polyhedrons. Note that there…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences                                  Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                             …
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化处理一下,标记一下前面是否出现过这个值,然后不断更新last数组(该数组保存的是每个数最后一次出现的位置).最后用树状数组维护. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; ; struct node{…
题目链接:http://codeforces.com/problemset/problem/314/C 题意:给定一个数列a.(1)写出a的不同的所有非下降子列:(2)定义某个子列的f值为数列中各个数的乘积.(3)求所有非下降子列的f值之和. 思路:我们用s[i]表示以数字a[i]结尾的所有非下降子列的f之和.那么a必然是接在之前小于等于a的某个数之后,设这个位置为j,那么s[i]=(s[j]+1)*a[i].也就是,a[i]可以接在其后或者自成一个子列.那么最后的答案就是所有的s值之和.这里有…