PAT 甲级 1007. Maximum Subsequence Sum
2017-06-27 21:46
387 查看
1003. Emergency (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked
on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in
and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected
by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
题意:最大连续和,输出最大连续和,和连续和的起止位置上的数;
思路:最大连续和的基础上加一些小优化,需要注意:
1、4 0 0 0 0 是有输出的 0 0 0
2. 4 -1 -2 -3 -4 根据题意是输出0 -1 -4#include<bits/stdc++.h>
using namespace std;
int num[10240];
int n;
int main()
{
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%d",&num[i]);
}
int ans = -1,l = 0,r = 0,tmp = 0,x = 0,y = n-1;
for(int i = 0;i < n;i++)
{
tmp += num[i];
if(tmp > ans)
{
ans = tmp;
x = l;
y = i;
}
if(tmp < 0)
{
tmp = 0;
l = i + 1;
}
}
if(ans == -1)
{
printf("0 %d %d\n",num[x],num[y]);
}
else
{
printf("%d %d %d\n",ans,num[x],num[y]);
}
return 0;
}
相关文章推荐
- PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】
- [PAT甲级]1007. Maximum Subsequence Sum(求连续最大子序列和)
- PAT 甲级 1007. Maximum Subsequence Sum
- PAT - 甲级 - 1007. Maximum Subsequence Sum (25)
- 【PAT甲级】【C++】1007. Maximum Subsequence Sum (25)
- PAT甲级-1007. Maximum Subsequence Sum (25)最大子序列和
- 【PAT甲级】1007. Maximum Subsequence Sum (25)
- PAT 甲级 1007. Maximum Subsequence Sum (DP 动态规划)
- PAT(甲级)1007. Maximum Subsequence Sum (25)
- PAT(甲级) 1007. Maximum Subsequence Sum (25)
- PAT甲级 1007. Maximum Subsequence Sum (25)
- PAT 甲级 1007. Maximum Subsequence Sum (25)
- PAT 甲级 1007. Maximum Subsequence Sum
- [PAT-甲级]1007.Maximum Subsequence Sum
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) dp
- PAT (Advanced Level) Practise - 1007. Maximum Subsequence Sum (25)
- PAT_1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum (25)(dp动态规划)
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 最大连续子数组,一次遍历,stack辅助
- 1007. Maximum Subsequence Sum (25)-PAT