这个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]]
#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;
}