Codeforces Round #355 (Div. 2)E. Vanya and Balloons
来源:互联网 发布:如何把淘宝微信群做大 编辑:程序博客网 时间:2024/05/02 02:23
链接:http://codeforces.com/contest/677/problem/E
题意:给定一个n*n的仅包含'0','1','2','3'的矩阵。求一个乘积最大的十字,要求十字的四边一样长。
分析:因为是十字所以可以向8个方向扩展,很明显我们会以中心点来处理出这个十字在哪。那么我们想要知道的是对于中心点(x,y)它的8个方向的2的个数有多少,3的个数有多少,那么我们显然是可以从它之前的点继承过来,我们可以从(1,1)~(n,n)遍历的时候处理出(x,y)的左上方4个方向,从(n,n)~(1,1)遍历的时候出来出右下方4个方向。O(n^2)。
代码:
#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<cstdio>#include<vector>#include<string>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=100010;const int MAX=1000000100;const int mod=100000000;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=1000000007;const int INF=1000000010;const double pi=acos(-1.0);typedef double db;typedef unsigned long long ull;struct node { int l,e,s; node() {} node(int l,int e,int s):l(l),e(e),s(s) {}}f[1010][1010][8];char s[1010];int g[8][2]={0,-1,-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1};int a[1010][1010];node get(int x,node k) { node ret=node(0,0,0); if (x==0) return ret; ret=k;ret.l++; if (x==2) ret.e++; else if (x==3) ret.s++; return ret;}int pd(int a,int b,int c,int d) { return (a-c)*log(2)+(b-d)*log(3)>=0;}int main(){ int i,j,k,h,n,mx=0,x=0,y=0,xx,yy; ll ans; scanf("%d", &n); for (i=1;i<=n;i++) { scanf("%s", s); for (j=1;j<=n;j++) { a[i][j]=s[j-1]-'0';mx=max(mx,a[i][j]); } } memset(f,0,sizeof(f)); for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (a[i][j]) { for (k=0;k<4;k++) f[i][j][k]=get(a[i+g[k][0]][j+g[k][1]],f[i+g[k][0]][j+g[k][1]][k]); } for (i=n;i>=1;i--) for (j=n;j>=1;j--) if (a[i][j]) { for (k=4;k<8;k++) f[i][j][k]=get(a[i+g[k][0]][j+g[k][1]],f[i+g[k][0]][j+g[k][1]][k]); } for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (a[i][j]) { xx=yy=0; if (a[i][j]==2) xx++; else if (a[i][j]==3) yy++; k=min(f[i][j][0].l,min(f[i][j][2].l,min(f[i][j][4].l,f[i][j][6].l))); for (h=0;h<=6;h+=2) { xx+=f[i][j][h].e-f[i+k*g[h][0]][j+k*g[h][1]][h].e; yy+=f[i][j][h].s-f[i+k*g[h][0]][j+k*g[h][1]][h].s; } if (pd(xx,yy,x,y)) { x=xx;y=yy; } xx=yy=0; if (a[i][j]==2) xx++; else if (a[i][j]==3) yy++; k=min(f[i][j][1].l,min(f[i][j][3].l,min(f[i][j][5].l,f[i][j][7].l))); for (h=1;h<=7;h+=2) { xx+=f[i][j][h].e-f[i+k*g[h][0]][j+k*g[h][1]][h].e; yy+=f[i][j][h].s-f[i+k*g[h][0]][j+k*g[h][1]][h].s; } if (pd(xx,yy,x,y)) { x=xx;y=yy; } } if (x==0&&y==0) printf("%d\n", mx); else { ans=1; for (i=1;i<=x;i++) ans=ans*2%MOD; for (i=1;i<=y;i++) ans=ans*3%MOD; printf("%I64d\n", (ans+MOD)%MOD); } return 0;}
0 0
- Codeforces Round #355 (Div. 2)E. Vanya and Balloons
- Codeforces Round #280 (Div. 2) E. Vanya and Field
- E. Vanya and Brackets(Codeforces Round #308 (Div. 2))
- Codeforces Round #308 (Div. 2) E. Vanya and Brackets
- Codeforces Round #355 (Div. 2) A. Vanya and Fence(水题)
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure
- Codeforces Round #355 (Div. 2)- Vanya and Fence(模拟)
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure
- Codeforces Round #355 (Div. 2)C - Vanya and Label
- Codeforces Round #355 (Div. 2)C. Vanya and Label
- Codeforces Round #355 (Div. 2)D. Vanya and Treasure
- Codeforces Round #355 (Div. 2)C. Vanya and Label
- Codeforces Round #355 (Div. 2) C. Vanya and Label
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- cf Round #280 (Div. 2) E. Vanya and Field 数学
- Codeforces 677E Vanya and Balloons(暴力+转换) category:
- Codeforces #280 Div.2 E.Vanya and Field 数学,数论
- AOP编程之前置通知
- HDU 4910 / BC 3D Problem about GCD
- #1039 : 字符消除
- HDU 1879 继续畅通工程
- 开发中遇到的问题
- Codeforces Round #355 (Div. 2)E. Vanya and Balloons
- android6.0 wifi连接
- matlab中关于pushbotton的左键双击响应问题
- LeetCode刷题之路 - number 26 and number 9
- iOS中常用的几何方法
- 字符集的前世今生
- 1013 Problem M
- 通过 脚本执行 kettle 的作业 命令
- 不同部门员工吃饭时聊些什么,程序员那一段笑死我了…