对于这题笔者无解,只有手抄一份正解过来了:
基本思想就是 :
- 二分答案,对于第x天,计算它最少的花费f(x),<=s就是可行的,这是一个单调的函数,所以可以二分。
- 对于f(x)的计算,我用了nlog(n)的算法,遍历m个商品,用c[i]乘以前x天里,第t[i]种货币的最便宜的价格,存放到一个数组里,之后排序,计算前k个的和就是f(x)
- 前x天里,第t[i]种货币的最便宜的价格是通过预处理得到的,很容易的计算一下前缀最小值就行了。
贴代码吧:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pii;
#define fi first
#define se second
#define mp make_pair
const int maxn = ;
int a[maxn],b[maxn],t[maxn],c[maxn];
int n,m,k,s;
int am[maxn],bm[maxn];
int ida[maxn],idb[maxn];
const int inf = 0x3f3f3f3f; pii pli[maxn];
int id[maxn]; ll f(int x){
ll ret = ;
for (int i=;i<=m;i++){
if (t[i] == ){
pli[i].fi = (ll)c[i] * (ll)am[x]; }
else{
pli[i].fi = (ll)c[i] * (ll)bm[x];
}
pli[i].se = i;
}
sort(pli+,pli+m+);
for (int i=;i<=k;i++){
ret += pli[i].fi;
} return ret;
} int main(){
cin>>n>>m>>k>>s;
am[] = bm[] = inf;
for (int i=;i<=n;i++){
scanf("%d",&a[i]);
if (a[i] < am[i-]){
am[i] = a[i];
ida[i] = i;
}
else{
am[i] = am[i-];
ida[i] = ida[i-];
}
}
for (int i=;i<=n;i++){
scanf("%d",&b[i]);
if (b[i] < bm[i-]){
bm[i] = b[i];
idb[i] = i;
}
else{
bm[i] = bm[i-];
idb[i] = idb[i-];
}
}
for (int i=;i<=m;i++){
scanf("%d%d",&t[i],&c[i]);
} ll low,high,mid;
low = ,high = n;
ll d = -;
while(low <= high){
mid = (low + high) / (ll);
if (f(mid) <= s){
high = mid - ;
d = mid;
for (int i=;i<=k;i++){
id[i] = pli[i].se;
}
}
else{
low = mid + ;
}
}
cout << d <<"\n";
if (d == (ll)-)
return ;
int x = (int)d;
for (int i=;i<=k;i++){
printf("%d %d\n",id[i],t[id[i]]==?ida[x]:idb[x]);
} return ;
}