T7315 yyy matrix folding (long), t7315yyy

Source: Internet
Author: User

T7315 yyy matrix folding (long), t7315yyy
Background

The audience is basically violent.

Description

Input/Output Format

Input Format:

 

 

Output Format:

 

 

Input and Output sample input sample #1:
2 21 -23 -4
Output sample #1:
4
Input example #2:
2 51 -2 -3 4 -56 -7 -8 9 -10
Output sample #2:
20
Description

 

DFS row, pressure DP Column

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<stack> 8 #include<cstdlib> 9 using namespace std;10 const int maxn=0x7fffff;11 inline void read(int &n)12 {13     char c='+';int x=0;bool flag=0;14     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}15     while(c>='0'&&c<='9')16     x=(x<<1)+(x<<3)+c-48,c=getchar();17     flag==1?n=-x:n=x;18 }19 int n,m; 20 int a[99][501];21 int ans=-maxn;22 int cur[501];23 int pos2(int x)24 {25     return (1<<x);26 }27 int dp(int zt)28 {29     memset(cur,0,sizeof(cur));30     int ou,ji;31     for(int i=1;i<=n;i++)32         if(zt&pos2(i))33             for(int j=1;j<=m;j++)34                 cur[j]+=a[i][j];35     bool flag=1;36     for(int i=1;i<=m;i++)37         if(cur[i]>0)38         {39             flag=0;40             break;41         }42     if(flag)43         return -maxn;44     ou=0;ji=0;45     for(int i=1;i<=m;i++)46     {47         if(i&1)48         {49             int x=cur[i]+ou;50             ji=max(ji,x);51         }52         else53         {54             int x=cur[i]+ji;55             ou=max(ou,x);56         }57     }58     return max(ou,ji);59 }60 void dfs(int pos,int last,int zt)61 {62     if(pos==n+1)63     {64         ans=max(ans,dp(zt));65         return ;66     }67 68         if(last==-1||(pos-last)&1)69             dfs(pos+1,pos,zt|pos2(pos));70     dfs(pos+1,last,zt);71 }72 int main()73 {74     read(n);read(m);75     for(int i=1;i<=n;i++)76         for(int j=1;j<=m;j++)77         {78             read(a[i][j]);79             ans=max(ans,a[i][j]);80         }81     if(ans<0)    82     cout<<ans;83     else84     {85         dfs(1,-1,0);86         printf("%d",ans);87     }88     return 0;89 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.