2017.08.14总结

来源:互联网 发布:手机淘宝撤销投诉 编辑:程序博客网 时间:2024/06/15 19:04

第十二题:

题意:找到最大的全是1的子矩阵

思路:把1想象组成一个长方形的一部分。预处理出每个数向下延伸的矩形的高度,然后就是比较以每一行为底能得到的最大矩形面积。

#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int n,m,h[100005],top,ans;

struct Node

{

    int h,sta;

}s[100005];

int main()

{

    int hh,t;

    while(scanf("%d%d",&n,&m)==2)

    {

        memset(h,0,sizeof(h));

        ans=0;

        for(int i=1;i<=n;++i)

        {

            for(int j=1;j<=m;++j)

            {

                scanf("%d",&hh);

                h[j]=hh==0?0:h[j]+1;

            }

 

            t=m;

            h[++t]=-1;

            s[0].h=-1;

            s[0].sta=top=0;

            for(int k=1;k<=t;++k)

            {

                if(h[k]>=s[top].h)

                {

                    s[++top].h=h[k];

                    s[top].sta=k;

                }

                else

                {

                    while(h[k]<s[top].h)

                    {

                        ans=max(ans,(k-s[top].sta)*s[top].h);

                        --top;

                    }

                    s[++top].h=h[k];

                }

            }

        }

        printf("%d\n",ans);

    }

    return 0;

}

第十三题:

题意:从左往右,每个牛都有一个高度,他能看到右边低于他高度的牛,问总共能看到多少

思路:求所有牛总共能看到多少牛,可以转化为:这n头牛共能被多少头牛看见。 当我们新加入一个高度值时,如果栈中存在元素小于新加入的高度值,那么这些小的牛肯定看不见这个高度的牛(那就看不见这头牛后边的所有牛), 所以就可以把这些元素弹出。

#include<iostream>

#include<cstdio>

#include<stack>

#include<algorithm>

using namespace std;

int main()

{

    int n;

    while(~scanf("%d",&n))

    {

        stack<int>S;

        int t;

        scanf("%d",&t);

        S.push(t);

        long long ans=0;

        for(int i=1;i<n;i++)

        {

            scanf("%d",&t);

            while(!S.empty()&&t>=S.top())S.pop();

            ans+=S.size();

            S.push(t);

        }

        cout<<ans<<endl;

    }

    return 0;

}

第九题:

题意:给出一组数据,和一个一定长度的窗,这个窗可以移动,求在这个窗口内,对应数据的最大最小值,数据范围:0~1000000。

#include <cstdio>

#include <cstdlib>

using namespace std;

const int MAXN = 1000010;

int a[MAXN], qup[MAXN], qdw[MAXN], ans[MAXN];

int hup, tup, hdw, tdw;

int n, len;

int main()

{

    scanf("%d%d", &n, &len);

    for (int i = 1; i <= n; i++)

    scanf("%d", &a[i]);;

    hup = 1; tup = 1; qup[1] = 1;

    hdw = 1; tdw = 1; qdw[1] = 1;

    if (len == 1) printf("%d ", a[1]);

    ans[1] = a[1];

    for (int i = 2; i <= n; i++)

    {

        while (a[i] <= a[qup[tup]] && tup >= hup) tup--;

        qup[++tup] = i;

        while (a[i] >= a[qdw[tdw]] && tdw >= hdw) tdw--;

        qdw[++tdw] = i;

        if (qup[hup] <= i - len) hup++;

        if (qdw[hdw] <= i - len) hdw++;

        if (i >= len) printf("%d ", a[qup[hup]]);

        ans[i] = a[qdw[hdw]];

    }

    printf("\n");

    for (int i = len; i <= n; i++)

    printf("%d ", ans[i]);

    printf("\n");

    return 0;

}

 

 

 

 

 



 

原创粉丝点击