Problem Description
长度为 n 的序列,把它划分成两段非空的子序列,定义权值为:两段子序列的最大值的差的绝对值。求可能的最大的权值。
数据范围:
2 <= n <= 10^6 , 0 < 序列内的数 <= 10^6 。
Input
第一行输入一个 T,表示有 T 组数据。
接下来有 T 组数据,每组数据的第一行输入一个数 n ,第二行输入 n 个数。
Output
每组数据输出可能的最大的权值。
Sample Input
1
3
1 2 3
Sample Output
2

编程题目,哪位大神教我做,谢谢!
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- threenewbee 2017-11-18 10:11关注
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 1e6+10; int a[N], pre[N], sum[N]; int main() { int T; cin>> T; while(T--) { int n; cin >> n; memset(sum, 0, sizeof(sum)); memset(pre, 0, sizeof(pre)); for(int i = 1; i <= n; i ++) cin >> a[i]; for(int i = 1; i <= n; i ++) { sum[i] = max(sum[i-1], a[i]); } for(int i = n; i > 0; i --) { pre[i] = max(pre[i+1], a[i]); } int MAX = -1; for(int i = 2; i <= n; i ++){ MAX = max(MAX, abs(pre[i]-sum[i-1])); } printf("%d\n",MAX); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报