2016弱校联盟十一专场10.2 Longest Increasing Subsequence
2016-10-05 21:59
483 查看
这个dp题很有学问,我也是照着标称写的
还需要学习
补: if(order[i] < order[i-1]) pre[j] += now[j]; 这句的解释
首先order表示的是每个数字排序之后的数组
order[0] 就是最小的那个数字是原来数组哪一个
但是会有等于的情况 所以这里定义的是如果
i < j时
* order[i] > order[j] 意味 A{order[i]] >= A[order[j]]
* order[j] < order[j] 意味 A[order[i]] < A[order[j]]
还需要学习
补: if(order[i] < order[i-1]) pre[j] += now[j]; 这句的解释
首先order表示的是每个数字排序之后的数组
order[0] 就是最小的那个数字是原来数组哪一个
但是会有等于的情况 所以这里定义的是如果
i < j时
* order[i] > order[j] 意味 A{order[i]] >= A[order[j]]
* order[j] < order[j] 意味 A[order[i]] < A[order[j]]
#include<vector> #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int MAXN = 1e5+5; typedef long long ll; int main(){ int n; while(~scanf("%d",&n)) { vector<int> low(n), high(n); int m = 0; for(int i = 0; i < n; ++i) { scanf("%d %d",&low[i],&high[i]); m = max(m, high[i]+1); } vector<ll> result(n+1); vector<int> order(n); for(int i = 0; i < n; ++i) order[i] = i; do{ vector<ll> now(m); for(int i = low[order[0]]; i <= high[order[0]]; ++i) now[i] ++; for(int i = 1; i < n; ++i) { vector<ll> pre(m); ll sum = 0; int lo = low[order[i]]; int hi = high[order[i]]; for(int j = 0; j < m; ++j) { if(lo <= j && j <= hi) { pre[j] = sum; if(order[i] < order[i-1]) pre[j] += now[j]; } sum += now[j]; } now.swap(pre); } vector<int> dp(n,1); for(int i = 0; i < n; ++i) for(int j = 0; j < i; ++j) if(order[j] < order[i]) { dp[i] = max(dp[i], dp[j]+1); } int maxx = *max_element(dp.begin(), dp.end()); for(int i = 0; i < m; ++i) result[maxx] += now[i]; }while(next_permutation(order.begin(), order.end())); for(int i = 1; i <= n; ++i) { if(i!=1) printf(" "); printf("%lld",result[i]); } printf("\n"); } return 0; }
相关文章推荐
- 2016弱校联盟十一专场10.2 Longest Increasing Subsequence
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing
- 2016弱校联盟十一专场10.2——Around the World
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
- 2016弱校联盟十一专场10.2部分题解
- 2016弱校联盟十一专场10.2——Floyd-Warshall
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing bnu 52325
- [弱校联萌2016]2016弱校联盟十一专场10.2
- 2016弱校联盟十一专场10.5 I Increasing or Decreasing(数位dp)
- Longest Increasing Continuous Subsequence
- 动态规划--最长上升子序列(Longest increasing subsequence)
- leetcode 674 Longest Continuous Increasing Subsequence
- Longest Increasing Subsequence问题及解法
- [弱校联萌2016]2016弱校联盟十一专场10.3
- [置顶] BP算法经典例题 longest increasing subsequence
- 2016弱校联盟十一专场10.5 F Fibonacci of Fibonacci(矩阵快速幂+找循环节)
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
- LeetCode No.300 Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence
- Longest Increasing Subsequence