标签:
Problem 1567 - D - Sloth‘s Angry这道题有点离散化的思想的味道,比如3 4需要2次,100 ,101当然也是两次,所以不必纠结每次操作后怎么改变原来的值,就保持原来的值不变即可,
比如 3 4 2 5 想把每个都减去2 那么为1 2 0 3 但是事实是和 3 4 0 5 是一样的,更进一步还是3 4 【2】 5 ,连2 都不用变,直接无视掉 ,那么递归算法就很好写了
solve(L,R) 表示区间 [ L ,R ]全部被矩形覆盖需要的次数
#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32)if(c==EOF)return 0;
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return 1;
}
template<class T> inline T read_(T&x,T&y)
{
return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------ZCC IO template------
const int maxn=1e6+10;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 100000000
LL a[maxn];
int n;
LL solve(int l,int r)
{
LL len=LL(r-l+1),sum=1,pos=l;
LL minv=a[l];
for(int i=l;i<=r;i++)
minv=min(minv,a[i]);///贪心在于每次找到右端的最小值,相邻连续的一起减去
for(int i=l;i<=r;i++)
{
if(a[i]==minv)
{
sum+=solve(pos,i-1);//把i的位置“变为 0 ”,计算这个之前的次数
pos=i+1;//更新pos的位置
}
}
if(pos<=r)//最后剩下的区间
sum+=solve(pos,r);
return min(len,sum);//最小值
}
int main()
{
//freopen("in.txt","r",stdin);
while(read(n))
{
for(int i=1;i<=n;i++)
read(a[i]);
writeln(solve(1,n));
}
return 0;
}
/*
4
3 4 5 5
4
3 4 2 5
6
3 4 1 3 4 1
*/
Problem 1567 - D - Sloth's Angry ( 递归+贪心)
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/45173777