做网站什么码建设银行论坛网站
做网站什么码,建设银行论坛网站,网站换域名做301,图片制作器手机版在线题意#xff1a;
----最大M子段和问题 给定由 n个整数#xff08;可能为负整数#xff09;组成的序列以及一个正整数 m#xff0c;要求确定序列的 m个不相交子段#xff0c;使这m个子段的总和达到最大#xff0c;求出最大和。
题目#xff1a;
Now I think you have …题意
----最大M子段和问题 给定由 n个整数可能为负整数组成的序列以及一个正整数 m要求确定序列的 m个不相交子段使这m个子段的总和达到最大求出最大和。
题目
Now I think you have got an AC in Ignatius.L’s “Max Sum” problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.
Given a consecutive number sequence S 1, S 2, S 3, S 4 … S x, … S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) S i … S j (1 ≤ i ≤ j ≤ n).
Now given an integer m (m 0), your task is to find m pairs of i and j which make sum(i 1, j 1) sum(i 2, j 2) sum(i 3, j 3) … sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x is not allowed).
But Im lazy, I don’t want to write a special-judge module, so you don’t have to output m pairs of i and j, just output the maximal summation of sum(i x, j x)(1 ≤ x ≤ m) instead. _
Input
Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 … S n. Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 3 2 6 -1 4 -2 3 -2 3
Sample Output
6 8
思路
很明显我们不能知道某一点是否该加入段中加入到那一段中不能用贪心写那么只能用动态规划看某点j是否需要加入i段中或是新开一段作为新段的开头。
AC代码
#includestdio.h
#includestring.h
#includealgorithm
using namespace std;
typedef long long ll;
const int M1e610;
const int inf0x3f3f3f3f;
int n,m,dp[M],f[M],s[M],ans;
int main()
{while(~scanf(%d%d,n,m)){for(int i1; im; i)scanf(%d,f[i]);memset(dp,0,sizeof(dp));memset(s,0,sizeof(s));for(int i1; in; i){ans-inf;for(int ji; jm; j){dp[j]max(dp[j-1]f[j]/*f[j]属于第i段*/,s[j-1]f[j]/*f[j]不属于第i段为新的段加上前i-1段看是否较大*/);/*看该点是否为新段的开始*/s[j-1]ans;/*上一段i-1)到j-1点的最大值*/ansmax(dp[j],ans);/*在第i段第j点的最大值为当前最大值*/}}printf(%d\n,ans);}return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/87819.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!