在这里
c题:给你三个数组,你可以任意排列,使得数组1和数组2的对应的数成为一个区间,1对应的数要小于2,这个区间长度乘以另一个数组对应的值,问最小的多少
做法:首先我们发现这是一个贪心问题,而且是不要求顺序的。我们可以找最小的区间长度,由小到大排序,依次乘c区间的大------>小,就解决了,要用适当的方法排序,为每一个b数组找一个最近的a数组对应的值
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define LL long long
using namespace std;
typedef pair<int,int>PII;
const int N = 2e5;
int a[N],b[N],c[N];
void solve()
{
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
for(int i=1;i<=n;i++)cin>>c[i];
sort(a+1,a+1+n);
sort(b+1,b+1+n);
sort(c+1,c+1+n,greater<int>());
priority_queue<LL>q;
priority_queue<LL,vector<LL>,greater<LL>>len;
int i=1;
for(int j=1;j<=n;j++)
{
while(a[i]<b[j]&&i<=n)
{
q.push(a[i]);
i++;
}
len.push(b[j]-q.top());
q.pop();
}
LL ans=0;
for(int i=1;i<=n;i++)
{
ans+=len.top()*c[i];
len.pop();
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int T;cin>>T;
while(T--)solve();
return 0;
}