Description
Farmer John is at the market to purchase supplies for his farm. He has
in his pocket K coins (1 <= K <= 16), each with value in the range
1..100,000,000. FJ would like to make a sequence of N purchases (1 <= N <= 100,000), where the ith purchase costs c(i) units of money (1 <=
c(i) <= 10,000). As he makes this sequence of purchases, he can
periodically stop and pay, with a single coin, for all the purchases
made since his last payment (of course, the single coin he uses must
be large enough to pay for all of these). Unfortunately, the vendors
at the market are completely out of change, so whenever FJ uses a coin
that is larger than the amount of money he owes, he sadly receives no
changes in return! Please compute the maximum amount of money FJ can
end up with after making his N purchases in sequence. Output -1 if it
is impossible for FJ to make all of his purchases.K A coin. Buy it. N Items.
Given the order of purchase, that is to say, the goods must be bought all the way in order. After selecting the sequence of items to buy, the owner will not change the money after paying. Now I hope that after buying what I need, the more money I leave behind, the better. If I can't finish the task of purchasing, I will export it.-1
Input
Line 1: Two integers, K and N.
Lines 2..1+K: Each line contains the amount of money of one of FJ's coins.
Lines 2+K..1+N+K: These N lines contain the costs of FJ's intended purchases.
Output
- Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.
Sample Input
3 6
12
15
1
6
3
3
2
3
7INPUT DETAILS: FJ has 3 coins of values 12, 15, and 10. He must make
purchases in sequence of value 6, 3, 3, 2, 3, and 7.
Sample Output
12
OUTPUT DETAILS: FJ spends his 10-unit coin on the first two purchases,
then the 15-unit coin on the remaining purchases. This leaves him with
the 12-unit coin.
Title Solution
The first thought is to press three-dimensional, f[i][j][k] denotes the bill of j~k paid by card I.
It turns out that this will MLE
Look at the data again and find a magical place. No more than 16 cards
Then you can press the card!
f[i] denotes the farthest bills that can be paid in state I. Preprocess the amount of bills that each card can pay continuously from each bill. After that, just move blindly.
Output, traverse the status of each payable bill equal to the total bill. Then just record the minimum.
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int f[140000];//The farthest bills you can pay with these cards
int m[20][110000];
int v[20],w[110000],bin[25];
int n,k;LL ret;
int main()
{
scanf("%d%d",&n,&k);ret=0;
bin[1]=1;for(int i=2;i<=n;i++)bin[i]=bin[i-1]*2;
for(int i=1;i<=n;i++){scanf("%d",&v[i]);ret+=v[i];}
for(int i=1;i<=k;i++)scanf("%d",&w[i]);
for(int i=1;i<=n;i++)
{
int cnt=0,t;
t=1;
while(cnt+w[t]<=v[i] && t<=k)cnt+=w[t++];
t--;
m[i][1]=t;
for(int j=2;j<=k;j++)
{
cnt-=w[j-1];
while(cnt>v[i])cnt-=w[t--];
t++;
while(cnt+w[t]<=v[i] && t<=k)cnt+=w[t++];
t--;
m[i][j]=t-j+1;
}
}
int ans=999999999;
for(int i=1;i<=n;i++)f[bin[i]]=m[i][1];
for(int i=1;i<=bin[n]*2-1;i++)
{
for(int j=1;j<=n;j++)
{
if((i&bin[j])==0)f[i|bin[j]]=max(f[i|bin[j]],f[i]+m[j][f[i]+1]);
}
}
bool bk=false;
for(int i=1;i<=bin[n]*2-1;i++)
{
if(f[i]>=k)
{
bk=true;
int cnt=0;
for(int j=1;j<=n;j++)if((i&bin[j])>0)cnt+=v[j];
ans=min(ans,cnt);
}
}
if(bk==false)printf("-1\n");
else printf("%d\n",ret-ans);
return 0;
}