杭电1003_Max Sum
这是原题的链接http://acm.hdu.edu.cn/showproblem.php?pid=1003
起初我是利用暴力的方法,求出所有序列的和的情况,每取一个序列就和以知道的最大和作对比,取大者。结果超时,代码如下,时间复杂度达到o(n^3)
#include <bits/stdc++.h>
using namespace std;
int main(){
int k;
cin >> k;
int q=k;
int f=;
while(k--){
int m;
cin >> m;
int a[m];
for(int i=;i<m;i++){
cin >> a[i];
}
int tp1=;
int tp2=;
int i,j=;
int sum=;
for(i=;i<m;i++)
{
for( j=;j<=i;j++){
int s=j;
int sum1=;
while(s<=i){
sum1+=a[s];
s++;
}
if(sum1>sum){
sum=sum1;
tp1=j+;
tp2=i+;
}
}
}
cout << "Case " <<++f<<":"<<endl;
cout << sum << " " << tp1 << " " << tp2;
if(k!=){
cout << endl;
}
}
return ;
}
必须换一种方法,考虑用动态规划,具体思路如下:
对于第i个数,它只有两种状态,一个是接在前一个队伍的前面,另一个是自己作为队头元素。dp[i]中存放的应该在i位置时i位置所属队伍的值。
所以统计dp中最大的值就可以知道所有队伍中的最值。据此,只需要保证dp[i]的值最大就可以。
so 状态转移方程为:dp[i]=max(dp[i-1]+a[i], a[i]);时间复杂度o(n^2),成功accept
代码
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std;
int array1[];
int dp[]; int main(){
int k;
cin >> k;
int f=;
while(k--)
{
int a;
cin >> a;
int start=;
int end=;
memset(array1,,sizeof(array1));
memset(dp,,sizeof(dp));
int sum=-;
int i;
for( i=;i<=a;i++)
{
cin >> array1[i];
dp[i]=max(dp[i-]+array1[i],array1[i]);
if(dp[i]>sum){//用来更新sum
sum=dp[i];
end=i;
}
}
int sum1=;
int j;
for( j=end;j>=;j--){//确定起始点
sum1+=array1[j];
if(sum1==sum){
start=j; } }
cout << "Case " <<++f<<":"<<endl;
cout << sum << " " << start << " " << end <<endl;
if(k!=){
cout << endl;
} }
return ;
}
杭电1003_Max Sum的更多相关文章
- 杭电1003-Max Sum
Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...
- 杭电1024Max Sum Plus Plus
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目: Problem Description Now I think you have got a ...
- 杭电ACM2058--The sum problem
http://acm.hdu.edu.cn/showproblem.php?pid=2058 以为简单的穷举就完了,结果是一直Time Limit Exceeded.. 这是代码: #include ...
- 杭电1024----Max Sum Plus Plus
/* 这题还没有理解透彻.某个dalao也不写注释.只能自己理解了... 先求为i个元素(1<=i<=M)为一个区间的最大和,保证元素个数大于等于i个,递推到M个即可 借鉴原址:http: ...
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- Help Johnny-(类似杭电acm3568题)
Help Johnny(类似杭电3568题) Description Poor Johnny is so busy this term. His tutor threw lots of hard pr ...
随机推荐
- centos 7 PostgreSQL一些简单问题以及解决办法
问题:org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are corre ...
- Android中m、mm、mmm、mma、mmma的区别
m:编译整个安卓系统 makes from the top of the tree mm:编译当前目录下的模块,当前目录下需要有Android.mk这个makefile文件,否则就往上找最近的Andr ...
- Could not find JSON in http://updates.jenkins-ci.org/update-center.json?id=default&version=2.7.4
14-Sep-2016 21:43:58.241 INFO [Download metadata thread] hudson.model.AsyncPeriodicWork$1.run Finish ...
- Android USB 开发详解
Android USB 开发详解 先附上 Android USB 官方文档 Android通过两种模式支持各种 USB 外设和 Android USB 附件(实现Android附件协议的硬件):USB ...
- laravel基础课程---10、数据库基本操作(如何使用数据库)
laravel基础课程---10.数据库基本操作(如何使用数据库) 一.总结 一句话总结: 1.链接数据库:.env环境配置里面 2.执行数据库操作:DB::table('users')->up ...
- tensorflow knn 预测房价 注意有 Min-Max Scaling
示例数据: 0.00632 18.00 2.310 0 0.5380 6.5750 65.20 4.0900 1 296.0 15.30 396.90 4.98 24.00 0.02731 0.00 ...
- tensorflow实现svm多分类 iris 3分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)
# Multi-class (Nonlinear) SVM Example # # This function wll illustrate how to # implement the gaussi ...
- stack_2.由两个栈组成队列
思路: 用两个栈($stack_a, $stack_b),当push的时候,压入$stack_a, 让pop的时候,先把$stack_a元素依次全部倒入$stack_b中,再对$stack_b进行po ...
- unity3d: how to display the obj behind the wall
透墙显示,遮挡显示,使用ztest Tags { "Queue"="Overlay+1" "RenderType"="Transp ...
- PS 滤镜— —扇形warp
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...