洛谷 P1868 饥饿的奶牛
https://www.luogu.com.cn/problem/P1868
思路:
dp[N]表示 在前i块草皮中最多能吃到几块
sort 把所有区间按照右端的大小排序
for 遍历[0,区间中出现过的最大长度]
{
dp[i] = dp[i-1] (继承上一个草皮的dp)
if存在区间[x,y] 使得y=i 则dp[y] = max ( dp[y] , dp[x-1]+len([x,y]) )
}
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
typedef long long LL;
typedef std::pair<int ,int> PP;
const int N = 3e6+5;
int n,v,vv;
int dp[N];
bool cmp(PP x, PP y)
{
return x.second < y.second ;
}
int main()
{
std::vector<PP> vc;
std::cin >> n;
for(int i=0; i<n; i++){
std::cin>> v >> vv;
vc.push_back(std::make_pair(v,vv));
}
std::sort(vc.begin(),vc.end(),cmp);
int idx=1;
for(std::vector<PP>::iterator iter = vc.begin(); iter != vc.end(); iter++){
//这里要加等号,否则最后两个样例会WA
while(idx <= iter->second){
dp[idx] = std::max(dp[idx],dp[idx-1]);
idx += 1;
}
dp[iter->second] = std::max(dp[iter->first-1]+iter->second-iter->first+1,dp[iter->second]);
}
std::cout << dp[vc[vc.size()-1].second] << std::endl;
return 0;
}
洛谷 P1091 合唱队形
https://www.luogu.com.cn/problem/solution/P1091
此题要求提出最少的人,获得一个最长的先单调递增后点掉递减的队伍,对于队伍递增递减部分的长度没有要求。
思路:
up[]数组记录 在正向遍历下 第i个位置的最长升序列长度(第一个长度计0)
dn[]数组记录 在反向遍历下 第i个位置的最长升序列长度(第一个长度计0)
然后遍历一遍 [0,n-1],找出最大的 先增后降序列长度 (up[i]+dn[i]+1)
n-最大长度 即为答案
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
typedef long long LL;
typedef std::pair<int ,int> PP;
const int N = 105;
int qu[N],up[N],dn[N];
int n;
int main()
{
scanf("%d",&n);