[51nod1291]Farmer

  用单调栈的话不严格的O(n^3)可以轻松艹过去,统计的时候要差分。

  可以发现,对于一个单调栈里的元素,从它进栈到出栈都会重复类似的计算。。再差分一波后就可以只在出栈的时候计算一下了。

  具体的话看代码吧。。

O(n^3):

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #define ll long long
 7 #define ui unsigned int
 8 #define ull unsigned long long
 9 using namespace std;
10 const int maxn=606;
11 char s[maxn];
12 int h[maxn],st[maxn],l[maxn];
13 int an[maxn][maxn];
14 int i,j,k,n,m;
15
16 int ra,fh;char rx;
17 inline int read(){
18     rx=getchar(),ra=0,fh=1;
19     while((rx<‘0‘||rx>‘9‘)&&rx!=‘-‘)rx=getchar();
20     if(rx==‘-‘)fh=-1,rx=getchar();
21     while(rx>=‘0‘&&rx<=‘9‘)ra*=10,ra+=rx-48,rx=getchar();return ra*fh;
22 }
23 inline void add(int y1,int x2,int y2){
24 //    printf("add:1,%d  %d,%d\n",y1,x2,y2);
25     an[x2][y2]++,an[x2][y1-1]--;
26 }
27 inline void addall(int len,int st2,int h){
28     for(register int i=1;i<=len;i++)an[h][i+st2]++,an[h][i-1]--;
29 //    add(i,h,i+st2);
30 //    an[h][1+st2]++,an[h][len+st2+1]--,
31 //    an[h][0]--,an[h][len]++;
32 }
33
34 char ss[10];int len;
35 inline void outx(int x){
36     if(!x){putchar(‘0‘);return;}
37     while(x)ss[len++]=x%10,x/=10;
38     while(len)putchar(ss[--len]+48);
39 }
40 int main(){
41     n=read(),m=read();//register int k;
42     for(i=1;i<=n;i++){
43         scanf("%s",s+1);
44         int top=0;
45         for(j=1;j<=m+1;j++){
46             h[j]=s[j]==‘1‘?h[j]+1:0;
47             while(top&&h[st[top]]>=h[j])addall(j-st[top],st[top]-l[top],h[st[top]]),top--;
48             st[++top]=j,l[top]=st[top-1]+1;
49             //for(k=1;k<=top;k++)add(j-st[k]+1,h[st[k]],j-l[k]+1);
50         }
51     }
52 //    for(i=1;i<=n;i++)for(j=1;j<=m;j++)an[i][j]+=an[i][j-1];
53 //    for(i=1;i<=n;printf("%d\n",an[i][m]),i++)for(j=1;j<m;j++)printf("%d ",an[i][j]);
54     for(i=n;i;i--)for(j=m;j;j--)an[i][j]+=an[i+1][j]+an[i][j+1]-an[i+1][j+1];
55     for(i=1;i<=n;outx(an[i][m]),putchar(‘\n‘),i++)for(j=1;j<m;j++)outx(an[i][j]),putchar(‘ ‘);
56 }

O(n^2):

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #define ll long long
 7 #define ui unsigned int
 8 #define ull unsigned long long
 9 using namespace std;
10 const int maxn=606;
11 char s[maxn];
12 int h[maxn],st[maxn],l[maxn];
13 int an[maxn][maxn];
14 int i,j,k,n,m;
15
16 int ra,fh;char rx;
17 inline int read(){
18     rx=getchar(),ra=0,fh=1;
19     while((rx<‘0‘||rx>‘9‘)&&rx!=‘-‘)rx=getchar();
20     if(rx==‘-‘)fh=-1,rx=getchar();
21     while(rx>=‘0‘&&rx<=‘9‘)ra*=10,ra+=rx-48,rx=getchar();return ra*fh;
22 }
23
24 inline void addall(int len,int st2,int h){
25     an[h][1+st2]++,an[h][len+st2+1]--,
26     an[h][0]--,an[h][len]++;
27 }
28
29 char ss[10];int len;
30 inline void outx(int x){
31     if(!x){putchar(‘0‘);return;}
32     while(x)ss[len++]=x%10,x/=10;
33     while(len)putchar(ss[--len]+48);
34 }
35 int main(){
36     n=read(),m=read();register int i,j;
37     for(i=1;i<=n;i++){
38         scanf("%s",s+1);
39         int top=0;
40         for(j=1;j<=m+1;j++){
41             h[j]=s[j]==‘1‘?h[j]+1:0;
42             while(top&&h[st[top]]>=h[j])addall(j-st[top],st[top]-l[top],h[st[top]]),top--;
43             st[++top]=j,l[top]=st[top-1]+1;
44         }
45     }
46     for(i=1;i<=n;i++)for(j=1;j<=m;j++)an[i][j]+=an[i][j-1];
47     for(i=1;i<=n;i++)an[i][m+1]=0;for(i=1;i<=m;i++)an[n+1][i]=0;
48     for(i=n;i;i--)for(j=m;j;j--)an[i][j]+=an[i+1][j]+an[i][j+1]-an[i+1][j+1];
49     for(i=1;i<=n;outx(an[i][m]),putchar(‘\n‘),i++)for(j=1;j<m;j++)outx(an[i][j]),putchar(‘ ‘);
50 }

时间: 2024-12-27 18:05:02

[51nod1291]Farmer的相关文章

fzu 1150 Farmer Bill&#39;s Problem

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n,m,k,ans; while(~scanf("%d%d",&n,&k)) { ans=0; m=0; ans=n; m=n; while(n>=k) { m=n/k; ans+=n/k; n=n%k; n+=m; } printf(&qu

Sicily 13859. Farmer John has no Large Brown Cow

13859. Farmer John has no Large Brown Cow Constraints Time Limit: 1 secs, Memory Limit: 256 MB Description Farmer John likes to collect as many different types of cows as possible. In fact, he has collected almost every conceivable type of cow, excep

【CoreForces Round #102B】 Help Farmer

C. Help Farmer 简化题意:已知abc=N,给出N,求解出 \[ (a+2)*(b+2)*(c+1) 的最大值和最小值 \] 题解:暴力枚举a,b,c,求出最小值. 默认 \[ 最大值 =3*3*(N+1) \] #include <cstdio> #include <queue> using namespace std; typedef long long ll; ll min(ll a,ll b){ return a<b?a:b; } ll max(ll a,

HDU 4380 Farmer Greedy 计算几何+bitset

枚举直线,对于直线的某个点在直线的左端还是右端,可以状压出一个数,用bitset记录. 然后三角形就是3个bitset&一下 #include <cstdio> #include <cstring> #include <bitset> #include <algorithm> using namespace std; typedef long long ll; const int N = 101; const int M = 1005; bitset

洛谷 P3133 [USACO16JAN]无线电联系Radio Contact

P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! They both fan out and search the farm along different paths, but stay in contact via radio so they can keep in to

[POJ3253] Fence Repair

Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a s

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

Agri-Net POJ - 1258

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is going to share hi

洛谷 P1339 [USACO09OCT]热浪Heat Wave

题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for good eating but are not so adept at creating creamy delicious dairy products. Farmer John is leading the charge to deliver plenty of ice cold nutritiou