HDU 2830 Dynamic Planning (DP) Matrix Swapping II

Source: Internet
Author: User

Analysis: Scan each row, use this row as the base, use dp to store the heights reached by column 1, and then sort the heights (of course, you cannot sort the heights directly by dp, because it is useful to scan the next line), it is easy to find the largest area. finally, we can find the overall maximum value!


[Cpp]
# Include <iostream>
# Include <string>
# Include <cstring>
# Include <algorithm>
 
Using namespace std;
Const int maxn = 1005;
 
Int work (int g [], int m ){
Int dp [maxn];
For (int I = 0; I <m; ++ I) dp [I] = g [I];
Sort (dp, dp + m );
Int ret = 0;
For (int I = m-1; I> = 0; -- I)
Ret = max (ret, dp [I] * (m-I ));
Return ret;
}
 
Int main (){
Int n, m;
While (cin> n> m ){
String map [maxn];
For (int I = 0; I <n; ++ I)
Cin> map [I];
Int dp [maxn]; // storage height
Int ans = 0;
Memset (dp, 0, sizeof (dp ));
For (int I = 0; I <n; ++ I ){
For (int j = 0; j <m; ++ j)
If (map [I] [j] = '1') dp [j] ++;
Else dp [j] = 0;
Ans = max (ans, work (dp, m ));
}
Cout <ans <endl;
}
Return 0;
}

# Include <iostream>
# Include <string>
# Include <cstring>
# Include <algorithm>

Using namespace std;
Const int maxn = 1005;

Int work (int g [], int m ){
Int dp [maxn];
For (int I = 0; I <m; ++ I) dp [I] = g [I];
Sort (dp, dp + m );
Int ret = 0;
For (int I = m-1; I> = 0; -- I)
Ret = max (ret, dp [I] * (m-I ));
Return ret;
}

Int main (){
Int n, m;
While (cin> n> m ){
String map [maxn];
For (int I = 0; I <n; ++ I)
Cin> map [I];
Int dp [maxn]; // storage height
Int ans = 0;
Memset (dp, 0, sizeof (dp ));
For (int I = 0; I <n; ++ I ){
For (int j = 0; j <m; ++ j)
If (map [I] [j] = '1') dp [j] ++;
Else dp [j] = 0;
Ans = max (ans, work (dp, m ));
}
Cout <ans <endl;
}
Return 0;
}


 

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.