题目链接:
http://codeforces.com/problemset/problem/609/D
D. Gadgets for dollars and pounds
Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.
Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.
Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.
Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.
Input
First line contains four integers n, m, k, s (1 ≤ n ≤ 2·105, 1 ≤ k ≤ m ≤ 2·105, 1 ≤ s ≤ 109) — number of days, total number and required number of gadgets, number of burles Nura has.
Second line contains n integers ai (1 ≤ ai ≤ 106) — the cost of one dollar in burles on i-th day.
Third line contains n integers bi (1 ≤ bi ≤ 106) — the cost of one pound in burles on i-th day.
Each of the next m lines contains two integers ti, ci (1 ≤ ti ≤ 2, 1 ≤ ci ≤ 106) — type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.
Output
If Nura can't buy k gadgets print the only line with the number -1.
Otherwise the first line should contain integer d — the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di — the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.
In case there are multiple possible solutions, print any of them.
input
5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2
output
3 1 1 2 3
input
4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2
output
-1
input
4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432
output
-1
题目大意:
给了n天时间,每天的美元和英镑兑换这种货币的汇率,然后给了m种商品和s个这种货币,问买k种商品最少需要多少天;而且商品只能按美元或者英镑来买;
输入:
第一行 ,n,m,k,s;
第2行:n个整数,表示多少卢布换一美元
第3行:n个整数,表示多少卢布换一英镑
接下来是m行,每行2个整数,表示每样东西用什么货币结账(1是美元,2是英镑),以及要多少那种外币
输入,最早那一天能够买k种东西
思路:
可以用保存前i天的最低美元和英镑汇率,没有规定哪天买,每天也没有购买数量限制,所以二分出一个答案mid的后,就在前mid天中汇率最低的时候一次性购入最便宜的就行了,判断一下总花费是否小于有的卢布
打印答案的时候以ans为结果,再模拟一遍就好
This is the code
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x7f7f7f7f //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const int N=2e5+4;
struct node
{
int num;//表示花费
int pos;//表示第几种东西
bool operator < (const node &y)const //从小到大排序
{
return num<y.num;
}
};
node fa[N];//储存需要买的东西的花费和种类
node fb[N];//储存需要买的东西的花费和种类
int ma[N],mb[N];//表示的当前i天的最低汇率是第几天的汇率
LL a[N],b[N];//表示当前天数的汇率
LL suma[N],sumb[N];//表示买钱i中东西需要画的钱
int cnta=1;//花费不同钱买的东西的数量
int cntb=1;
int s;//可以最多的花费
int k;//需要买k中东西
int check(int x)
{
LL sum=0;
for(int i=0; i<cnta&&i<=k; i++)//以买第一类东西为基础
{
if(k<i+cntb)//保证买的东西数量不超
{
sum=suma[i]*a[ma[x]]+sumb[k-i]*b[mb[x]];//按汇率最少的时候算需要多少这种货币;
if(sum <= s)//表示当前天数的汇率满足条件,可以寻找更加考前的汇率
return 0;
}
}
return 1;//表示当前天数的汇率满足条件,需要找靠后的天数的汇率
}
int bi(int l,int r)//二分寻找最少的天数
{
int mid;
while(l <= r)
{
mid = (l+r)>>1;
if(check(mid))//check的条件看上面
l = mid+1;
else
r = mid-1;
}
return l;//l-1表示的是最后一个不满足的天数,那么l天就是第一个满足的天数,也是最小的;
}
int main()
{
int n,m;
scanf("%d%d%d%d",&n,&m,&k,&s);
a[0]=1e6+1;//保证第一天的汇率就是第一天最低的
b[0]=1e6+1;
for(int i = 1; i <= n; i++)
{
scanf("%lld",&a[i]);
if(a[ma[i-1]] > a[i])
ma[i] = i;//ma[i]记录的是前i天美元汇率最小的那天; mb[i]也是;
else
ma[i] = ma[i-1];
}
for(int i = 1; i <= n; i++)
{
scanf("%lld",&b[i]);
if(b[mb[i-1]] > b[i])//同上
mb[i] = i;
else
mb[i] = mb[i-1];
}
for(int i = 1; i <= m; i++)
{
int type;//需要花费的钱的类型
int co;//花费
scanf("%d%d",&type,&co);
if(type==1)//分类
{
fa[cnta].num = co;//花费
fa[cnta].pos = i;//种类
cnta++;
}
else
{
fb[cntb].num = co;
fb[cntb].pos = i;
cntb++;
}
}
sort(fa+1,fa+cnta);//分类后排序,方便后面贪心;
sort(fb+1,fb+cntb);//将小的排在前面
suma[0] = 0;//初始化,保证第一天的是正确的
sumb[0] = 0;
for(int i = 1; i < cnta; i++)
suma[i] = suma[i-1]+(LL)fa[i].num;//suma[i]表示前i个需要美元买的商品一共需要多少美元;
for(int i = 1; i < cntb; i++)
sumb[i] = sumb[i-1]+(LL)fb[i].num;
int fs = bi(1,n);
if(fs > n)//表示不能购买
printf("-1\n");
else
{
printf("%d\n",fs);//第几天买完
LL sum=0;
int ans=0;
for(int i=0; i<cnta&&i<=k; i++)//找需要买多少种第一类花费的东西数量
{
if(k < i+cntb)
{
sum = suma[i]*a[ma[fs]]+sumb[k-i]*b[mb[fs]];
if(sum <= s)
{
ans = i;
break;
}
}
}
for(int i=1; i<=ans; i++)//第一种花费东西的种类和买的天数
printf("%d %d\n",fa[i].pos,ma[fs]);
for(int i=1; i<=k-ans; i++)
printf("%d %d\n",fb[i].pos,mb[fs]);
}
return 0;
}