Educational Codeforces Round 3 D
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.
题意: 一个人手上有 s 卢布,他要在 n 天内买 m 样东西中的 k 样。
每个物品有两种支付方式,要么用美元,要么用英镑。
每天有不同的支付方式代价,即换取一美元或英镑,需要付出 xi 卢布的代价。
求最早完成买 k 样东西的天数。如果无法完成任务,输出 -1。
一种商品只能购买一次,但是一天可以买多种商品。
思路:二分找天数,然后把每一天最低的英镑和美元更新,例如:第一天美元最低则后面的都是第一天的汇率,然后计 入对应的汇率是第几天,再把物品和对应的价格存起来,可以用优先队列维护
代码
const int N = 1e6 + 10;
int n,m,k,s;
int a[N],b[N],c[N],d[N];
vector<pair<int,int> > q;
int xx = 0;
vector<int> num;
map<int,int>mp,mp1;
bool check(int mid)
{
int dall = c[mid],pound = d[mid];
int cnt = k;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > ans;
for(int i = 0;i < m;i ++)
{
if(q[i].first == 1)
ans.push({q[i].second*dall,i});
else
ans.push({q[i].second*pound,i});
}
int sum = 0;
num.clear();
while(cnt--)
{
sum += ans.top().first;
num.push_back(ans.top().second);
ans.pop();
}
xx = sum;
if(sum <= s)
return true;
return false;
}
void solve()
{
cin >> n >> m >> k >> s;
for(int i = 1;i <= n;i ++)
{
cin >> a[i];
if(!mp[a[i]])
mp[a[i]] = i;
}
for(int i = 1;i <= n;i ++)
{
cin >> b[i];
if(!mp1[b[i]])
mp1[b[i]] = i;
}
c[1] = a[1],d[1] = b[1];
for(int i = 2;i <= n;i ++)
{
c[i] = min(c[i-1],a[i]);
d[i] = min(d[i-1],b[i]);
}
for(int i = 1;i <= m;i ++)
{
int x,y;
cin >> x >> y;
q.push_back({x,y});
}
if(k > m)
{
cout << -1 << '\n';
return ;
}
int l = 1,r = n;
while(l < r)
{
int mid = (l + r)/2;
if(check(mid))
r = mid;
else
l = mid + 1;
//cout << l <<' ' <<r << '\n';
}
check(l);
if(xx <= s)
{
cout << l << '\n';
for(int i = 0;i < num.size();i ++)
{
cout << num[i] + 1 << ' ';
//cout << q[num[i]].first << '\n';
if(q[num[i]].first == 2)
cout << mp1[d[l]] << '\n';
else
cout << mp[c[l]] << '\n';
//cout << num[i] + 1 << ' ';
}
}
else
cout << -1 << '\n';
}