码迷,mamicode.com
首页 > 其他好文 > 详细

Problem 1567 - D - Sloth's Angry ( 递归+贪心)

时间:2015-04-21 22:52:15      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Problem 1567 - D - Sloth‘s Angry
Time Limit: 1000MS Memory Limit: 65536KB
Total Submit: 326 Accepted: 113 Special Judge: No
Description
A forest is full of sloths, they are so eager for tree leaves and as a result, very angry.
We assume that the forest is a map of N * M grids, and each of the gird is an empty land or contains a big sloth. It’s guaranteed that the sequence of the sloth is a continuous segment from the leftmost column for every row. ( You may consider that the number of columns M is infinite ).
As a sloth lover, you want to feed all the sloths as quick as possible. Every second you may select a rectangle area of the grids which only contains sloths and feed all the sloths there.
What’s the minimum time required to meet all the sloths’ needs in the forest?
Input
First line of each case contains one numbers N.(1≤??n?≤?1?000).

The following line contains N numbers Ai, each describing the length of continuous sloths sequence from the left most column in every row. ( 1 <= Ai <= 10^9 )
Output
Output the answer on a single line for each case.
Sample Input
4
3 4 5 5
Sample Output
3
Hint

The distributing situation of the sloths in the sample is as follow:

SSS

SSSS

SSSSS

SSSSS

And you can choose three rectangles to cover it.
Source

这道题有点离散化的思想的味道,比如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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!