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;
}
- 2017.08.14总结
- (2017.08.14-2017.08.18)周总结-琐事总结
- 总结14
- 2017.08.04总结
- 2017.08.05总结
- 2017.08.15总结
- 2017.08.16总结
- 2017.08.17总结
- 2017.08.18总结
- 2017.08.19总结
- 2017.08.21总结
- 2017.08.23总结
- 2012.7.14总结
- 2012.7.14总结
- 总结13,展望14
- 每日总结-05-14
- 第14周总结
- 年度总结(14)
- 算法的时间复杂度和空间复杂度-总结<转载>
- 练习1-1 在你自己的程序中运行“hello,world”程序。再有意去掉部分内容,看看会得到什么错误信息。
- position属性值(面试必考)
- 努力的回报1
- 手游服务端框架之使用事件驱动模型解决业务高耦合
- 2017.08.14总结
- 【黑科技】用树状数组解决区间修改查询问题
- CoordinatorLayout的layout_scrollFlags
- 前几天整理的Python小程序
- JS修改昵称
- 【CUGBACM15级BC第20场 A】hdu 5123 who is the best?
- ssh框架搭建出错
- Makefile :=和=区别
- 知识储备:02数组与字符串:判断元素出现与否及次数