NC24828. [USACO 2009 Feb S]The Leprechaun
描述
By way of example, consider the 4 x 4 matrix on the left below that has all the elements from one exemplary "wrapped" diagonal marked: 8 6 6* 1 8 6* 6 1 -3 4 0 5* -3 4 0 5 4* 2 1 9 4 2 1 9* 1 -9* 9 -2 1 -9 9*-2 The marked diagonal of the right-hand matrix includes two nines (the highest available number) and a six for a total of 24. This is the best possible sum for this matrix and includes only three of the four possible elements on its diagonal.
输入描述
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains N space-separated integers that compose row i of the matrix
输出描述
* Line 1: A single integer that is the largest possible sum computable using the rules above
示例1
输入:
4 8 6 6 1 -3 4 0 5 4 2 1 9 1 -9 9 -2
输出:
24
C++14(g++5.4) 解法, 执行用时: 105ms, 内存消耗: 740K, 提交时间: 2019-09-15 21:35:02
#include<bits/stdc++.h> #define FOG(x,y,z) for(register int x=y,x##_=z;x<=x##_;++x) #define DOG(x,y,z) for(register int x=y,x##_=z;x>=x##_;--x) #define FOR(x,y,z) for(int x=y,x##_=z;x<=x##_;++x) #define DOR(x,y,z) for(int x=y,x##_=z;x>=x##_;--x) #define FOR_(x,y,z,s) for(int x=y,x##_=z;x<=x##_;x+=s) #define FOR__(x,y,z) for(int x=y,x##_=z;x<=x##_;x<<=1) #define EOR(x,y) for(int x##_=head[x],y=edge[x##_].e;x##_;y=edge[x##_=edge[x##_].to].e) #define EGOR(x,y,z) for(int x##_=head[x],y=edge[x##_].e,z=edge[x##_].c;x##_;y=edge[x##_=edge[x##_].to].e,z=edge[x##_].c) #define EOD(x,y) for(int &x##_=head[x],y=edge[x##_].e;x##_;y=edge[x##_=edge[x##_].to].e) #define SOR(x,y) for(int x=y;x;x=y&(x-1)) #define SOO(x,y) for(int x=y;;x=y&(x-1)) #define FOB(b,y) for(int b##_=y,b=bin[b##_&-b##_];b##_;b##_&=b##_-1,b=bin[b##_&-b##_]) #define While(x) for(;x;) #define clr(x,y) memset(x,y,sizeof(x)) #define lbd(A,s,e,x) (lower_bound(A+s,A+e+1,x)-A) #define ubd(A,s,e,x) (upper_bound(A+s,A+e+1,x)-A) #define uni(A,x) {sort(A+1,A+x+1);x=unique(A+1,A+x+1)-A-1;} #define uniz(A,x) {sort(A,A+x);x=unique(A,A+x)-A;} #define szf(x) sizeof(x) #define min3(x,y,z) min(min(x,y),z) #define max3(x,y,z) max(max(x,y),z) #define read2(x,y) read(x),read(y) #define read3(x,y,z) read(x),read(y),read(z) #define read4(x,y,z,w) read3(x,y,z),read(w) #define reads(str) sf("%s",str) #define readf(x) sf("%lf",&x) #define ts (*this) #define sf scanf #define pf printf #define ll long long #define ull unsigned long long #define uint unsigned int #define db double #define ct clock_t #define ck() clock() #define rd rand() #define rmx RAND_MAX #define RD T*(rd*2-rmx) using namespace std; template<class T>bool tomin(T &x,T y){return y<x?x=y,1:0;} template<class T>bool tomax(T &x,T y){return x<y?x=y,1:0;} template<class T>void read(T &x){ char c; x=0; int f=1; while(c=getchar(),c<'0'||c>'9')if(c=='-')f=-1; do x=(x<<3)+(x<<1)+(c^48); while(c=getchar(),c>='0'&&c<='9'); x*=f; } bool mem1; const db Pi=acos(-1); const int dx[]={-1,-1,0,1,1,1,0,-1}; const int dy[]={0,1,1,1,0,-1,-1,-1}; const int maxn=205; int n; int A[maxn][maxn]; int mark[maxn][maxn],tot; ll solve(int x,int y,int d){ ++tot; ll res=0,mx=A[x][y]; while(mark[x][y]!=tot){ mark[x][y]=tot; tomax(mx,res+=A[x][y]); x=(x+dx[d]+n)%n; y=(y+dy[d]+n)%n; }return mx; } bool mem2; int main(){ // cerr<<(&mem2-&mem1)/1024.0/1024<<endl; srand(time(NULL)); read(n); FOR(i,0,n-1)FOR(j,0,n-1)read(A[i][j]); ll ans=-1e18; FOR(i,0,n-1)FOR(j,0,n-1)FOR(d,0,7)tomax(ans,solve(i,j,d)); pf("%lld",ans); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 19ms, 内存消耗: 612K, 提交时间: 2019-09-14 11:49:49
#include <bits/stdc++.h> using namespace std; int n,a[201][201],maxx=-10000000,i,j,k,w,x,y,z; int main() { cin>>n; for(i=0;i<n;i++)for(j=0;j<n;j++)cin>>a[i][j]; for(i=0;i<n;i++)for(j=0;j<n;j++,w=0,x=0,y=0,z=0) for(k=0;k<n;k++) { w+=a[i][(j+k)%n]; x+=a[(i+k)%n][j]; y+=a[(i+k)%n][(j+k)%n]; z+=a[(i-k+n)%n][(j+k)%n]; maxx=max(max(max(w,x),max(y,z)),maxx); } cout<<maxx<<endl; }