题意:给你一个a数组和一个d数组,这两个数组都是非递减的序列,让你通过构造b数组(数据保证一定能构成b数组),同时求出b数组的每个元素的最大的可能和最小的可能(数组的每个元素都单独分析)。
思路:
对于元素最小的数组,只要看a数组中能不能小于当前的d数组的最小值,如果能就是这两个的差,不能就d数组的最小值往后挪。
对于元素最大的数组 ,最大能减的值初始化为j为n,从n到1逆向遍历,如果存在ai>di-1,那么ai-1的元素是不能和i之后的元素的组合的,因为如果组合了ai的元素就一定没有元素能够组合,就j=i-1就好了。
// 10 20 30 36
// 19 33 33 55
比如是这种情况,首先b4=55-36 因为36>33,那么30只能访问到33这个下标(如果组合55的下标那么36就没有数可以组合)。
代码:
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N],ss[N],a[N],d[N];
int main()
{
int t;
sc_int(t);
while(t--)
{
sc_int(n);
for(int i =1;i<=n;i++)
cin>>a[i];
for(int i =1;i<=n;i++)
cin>>d[i];
int i=1,j=n;
for(int k =1;k<=n;k++)
{
while(a[k]>d[i])i++;
s[k]=d[i]-a[k];
}
i=1,j=n;
// cout<<i<<" "<<j<<endl;
for(int k=n;k>=1;k--)
{
ss[k]=d[j]-a[k];
if(a[k]>d[k-1])
j=k-1;
}
for(int i =1;i<=n;i++)
cout<<s[i]<<" ";
cout<<endl;
for(int i =1;i<=n;i++)
cout<<ss[i]<<" ";
cout<<endl;
// 10 20 30 36
// 19 33 33 55
}
return 0;
}