Description
1-cent coin. Additionally, there’s a bill whose value of K cents is known toexceed any of the coins. There’s a coin collector who wants to collect a specimenof each denomination of coins. He already has a few coins at home, butcurrently he only carries one K-cent bill in his wallet. He’s in a shop wherethere are items sold at all prices less than K cents (1 cent, 2 cents, 3 cents,… , K-1 cents). In this shop, the change is given using the followingalgorithm:
1. Let the amount of change to give be A cents.
2. Find the highest denomination that does not exceed A. (Let it be the B-centcoin.)
3. Give the customer a B-cent coin and reduce A by B.
4. If A = 0, then end; otherwise return to step 2.
The coin collector buys one item, paying with his K-cent bill.
Your task is to write a program that determines:
How many different coins that the collector does not yet have in his collectioncan he acquire with this transaction? What is the most expensive item the storecan sell him in the process?
Input
The first line of the input contains the integers N (1 <= N <= 500 000)and K (2 <= K <= 1000000000). The following N lines describe the coins incirculation. The (i + 1)-th line contains the integers ci (1 <= ci < K)and di, where ci is the value (in cents) of the coin, and di is 1, if thecollector already has this coin, or 0, if he does not. The coins are given inthe increasing order of values, that is, c1 < c2 < … < cN. The firstcoin is known to be the 1-cent coin, that is, c1 = 1.
Output
The first line of the output should contain a single integer — the maximalnumber of denominations that the collector does not yet have, but could acquirewith a single purchase. The second line of the output should also contain asingle integer — the maximal price of the item to buy so that the change givenwould include the maximal number of new denominations, as declared n the firstline.
Sample Input
7 25
1 0
2 0
3 1
5 0
10 0
13 0
20 0
Sample Output
3
6
分析:
要使收集到的钱币总类尽量多,在k值一定的情况下,应尽量选当前没有的面值最小的钱币(且不重复)。但当此时的面额总值sum加上当前面额值a[i]>=a[i+1]时,则根据规则会使用a[i+1]替代部分之前选择的货币,导致之前已选货币面额总类不变或减少,故此时不能选择面额为a[i]的货币,同时sum<=k;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int a[500000],b[500000];
int main()
{
int n,k,i;
scanf("%d%d",&n,&k);
int sum=0,cnt=0;
for(i=0;i<n;++i)
{
scanf("%d%d",&a[i],&b[i]);
}
a[n]=k+1;
for(i=0;i<n;++i)
{
if(sum<=k && !b[i] && sum+a[i]<a[i+1])
{
sum+=a[i];
++cnt;
}
}
if(cnt==0 && sum==0)
sum=1;
cout<<cnt<<endl<<k-sum<<endl;
return 0;
}